赞
踩
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
vim /etc/redis.conf
redis-cli shutdown nosave
package com.sun.jedis; import org.junit.Test; import redis.clients.jedis.Jedis; /** * Description: * * @Author sun * @Create 2024/4/29 15:53 * @Version 1.0 */ public class Jedis_ { // 连接redis @Test public void con() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxx", xxx); // 如果redis设置了密码要先进行验证 jedis.auth("root"); String ping = jedis.ping(); System.out.println("ping = " + ping); // 关闭命令行连接 jedis.close(); } }
@Test public void key() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxxxx", xxxx); // 如果redis设置了密码要先进行验证 jedis.auth("root"); // 设置key jedis.set("k1", "v1"); jedis.set("k2", "v2"); jedis.set("k3", "v3"); // 获取所有的key Set<String> keys = jedis.keys("*"); // 遍历输出 for (String key : keys) { System.out.println("key=" + key); } // 判断key是否存在 Boolean k1 = jedis.exists("k1"); System.out.println("k1是否存在?" + k1); // 查看key的ttl System.out.println("k2的ttl=" + jedis.ttl("k2")); // 获取值 String k3 = jedis.get("k3"); System.out.println("k3=" + k3); jedis.close(); }
@Test public void string() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxxxx", xxxx); // 如果redis设置了密码要先进行验证 jedis.auth("root"); // 先清除一下库 jedis.flushDB(); // 批量设置string jedis.mset("k1", "v1", "k2", "v2", "k3", "v3"); // 批量获取string List<String> mget = jedis.mget("k1", "k2", "k3"); for (String s : mget) { System.out.println(s); } jedis.close(); }
@Test public void list() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxxxx", xxxx); // 如果redis设置了密码要先进行验证 jedis.auth("root"); // 先清除一下库 jedis.flushDB(); // 向list的左边添加三条记录 jedis.lpush("key", "v1", "v2", "v3"); // 显示数据,应该是v3,v2,v1 List<String> key = jedis.lrange("key", 0, -1); for (String s : key) { System.out.println(s); } jedis.close(); } }
@Test public void set() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxxxx", xxxx); // 如果redis设置了密码要先进行验证 jedis.auth("root"); // 先清除一下库 jedis.flushDB(); // 向set中添加元素 jedis.sadd("key", "val1", "val2", "val3"); // 取出set中的所有值 Set<String> key = jedis.smembers("key"); // 遍历输出 for (String s : key) { System.out.println(s); } jedis.close(); }
@Test public void hash() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxxxx", xxxx); // 如果redis设置了密码要先进行验证 jedis.auth("xxxxxxx"); // 先清除一下库 jedis.flushDB(); // 向hash中设置值 Map<String, String> map = new HashMap<String, String>(); map.put("field1", "value1"); map.put("field2", "value2"); map.put("field3", "value3"); jedis.hset("key", map); List<String> hmget = jedis.hmget("key", "field1", "field2", "field3"); for (String s : hmget) { System.out.println(s); } jedis.close(); }
@Test public void zset() { // 连接服务器的redis命令行 Jedis jedis = new Jedis("xxxx", xxxx); // 如果redis设置了密码要先进行验证 jedis.auth("xxxx"); // 先清除一下库 jedis.flushDB(); // 向zset中添加数据 jedis.zadd("key", 1, "zhangsan"); jedis.zadd("key", 2, "lisi"); jedis.zadd("key", 3, "wangwu"); // 取出数据 Set<String> key = jedis.zrange("key", 0, -1); for (String s : key) { System.out.println(s); } jedis.close(); }
/usr/local/bin/redis-server /etc/redis.conf
ps -aux | grep redis
/usr/local/bin/redis-cli -p 7489
systemctl start firewalld && firewall-cmd --permanent --add-port=7489/tcp && firewall-cmd --reload && firewall-cmd --query-port=7489/tcp
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <!-- 说 明 : 如 果 这 里 是 spring-boot-start 就 改 成 如 下 spring-boot-start-web--> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X 集成 redis 所需 common-pool--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <!--不要带版本号,防止冲突--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
spring:
redis:
host: xxxxxx # Redis服务器地址
port: xxxx# Redis服务器端口
password: ****** # Redis服务器密码
database: 0 # 默认数据库为0号
timeout: 1800000 # 连接超时时间是1800秒
lettuce:
pool:
max-active: 20 # 最大活跃连接数,使用负值表示没有限制
max-wait: -1 # 最大等待时间,单位为毫秒,使用负值表示没有限制
max-idle: 10 # 最大空闲连接数
min-idle: 0 # 最小空闲连接数
package com.sun.redis.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * Description: * * @Author sun * @Create 2024/4/29 21:29 * @Version 1.0 */ @EnableCaching @Configuration public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); System.out.println("template=>" + template); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping( LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); // key 序列化方式 template.setKeySerializer(redisSerializer); // value 序列化 template.setValueSerializer(jackson2JsonRedisSerializer); // value hashmap 序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); // 解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping( LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题),过期时间 600 秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }
package com.sun.redis.controller; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * Description: * * @Author sun * @Create 2024/4/29 21:36 * @Version 1.0 */ @RestController @RequestMapping("/redisTest") public class TestController { @Resource private RedisTemplate<String, Object> redisTemplate; @RequestMapping("/set") public String set() { redisTemplate.opsForValue().set("name", "孙显圣"); return redisTemplate.opsForValue().get("name").toString(); } }
package com.sun.redis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Description: * * @Author sun * @Create 2024/4/29 21:39 * @Version 1.0 */ @SpringBootApplication public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } }
@RequestMapping("/list")
public String list() {
// 清除一下库
redisTemplate.delete("key");
// 批量添加
redisTemplate.opsForList().leftPushAll("key", "v1", "v2", "v3");
// 遍历输出
List<Object> range = redisTemplate.opsForList().range("key", 0, -1);
return range.toString();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。