使用Jedis连接Redis
(1)在Maven工程中导入Jedis依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
代码的编写
public class Test01 {
public static void main(String[] args) {
//必须运行远程连接 必须防火墙放行该端口号
Jedis jedis=new Jedis("192.168.129.247",6379);
//关于字符串
jedis.set("k1","v1");
jedis.set("k12","18");
jedis.mset("k12","v2","k13","v3");
jedis.setnx("k1","12");
jedis.decr("k8");
//操作key
Set<String> keys = jedis.keys("*");
System.out.println("所有的key:"+keys);
jedis.del("k1");
jedis.expire("k9",10);
jedis.ttl("k9");
}
}
public class TestPoolJedis {
public static void main(String[] args) {
//连接池的配置
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(100);//设置连接池的最大连接数
config.setMaxIdle(10);//设置最大空闲的个数
config.setTestOnBorrow(true);//在从连接池这种获取连接对象前是否测试该对象可以。
//创建连接池对象
JedisPool jedisPool=new JedisPool(config,"192.168.192.247",6379);
//获取jedis对象
Jedis jedis = jedisPool.getResource();
System.out.println(jedis.get("k9"));
jedis.close();//释放资源
}
}
2. 使用Spring Boot连接Redis
(1)Spring Boot为操作redis准备了两个工具类StringRedisTemplate和RedisTemplate。StringRedisTemplate是RedisTemplate的子类。
(2)StringRedisTemplate它的泛型key和value都是String类型。
(3)RedisTemplate它的key value的泛型是Object,建议使用时指定key,value,以及hashkey的序列化方式。
2.1 使用StringRedisTemplate工具类
2.1.1 资源文件中进行配置
spring:
redis:
host: 192.168.192.247
jedis:
pool:
max-active: 20
max-idle: 8
min-idle: 0
max-wait: 20000
port:6379
连接Redis
@SpringBootTest
public class Demo {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void contextLoad(){
//返回所有key
Set<String> keys = stringRedisTemplate.keys("*");
System.out.println(keys);
//删除一个key
System.out.println(stringRedisTemplate.delete("k1"));
//获取key的过期时间
System.out.println(stringRedisTemplate.getExpire("k2"));
//设置key过期时间
System.out.println(stringRedisTemplate.expire("k2", 60, TimeUnit.SECONDS));
}
@Test
public void testString(){
ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
forValue.set("k1","v1");
System.out.println(forValue.get("k1"));
HashMap<String, String> map = new HashMap<>();
map.put("k2","v2");
map.put("k3","v3");
forValue.multiSet(map);
ArrayList<String> list = new ArrayList<>();
list.add("k1");
list.add("k3");
list.add("k2");
list.add("k4");
List<String> strings = forValue.multiGet(list);
System.out.println(strings);
System.out.println(forValue.setIfAbsent("k2","v2"));
}
}
2.3 使用RedisTemplate工具类
2.3.1 配置类(序列化)
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@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.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600)) //缓存过期10分钟 ---- 业务需求。
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//设置key的序列化方式
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //设置value的序列化
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
2.3.2 连接Redis集群
(1)资源文件配置
redis:
cluster:
nodes: 192.168.129.247:8001,192.168.129.247:8002,192.168.129.247:8003,192.168.129.247:8004,192.168.129.247:8005,192.168.129.247:8006
# 开启哨兵
sentinel:
master: mymaster
nodes: 192.168.127.247:26379