1.springboot整合redis
springboot在整合redis时提供两个模板类,StringRedisTemplate和RedisTemplate.以后对redis的操作都在该模板类中。StringRedisTemplate是RedisTemplate的子类。
(1)添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
(2)修改配置文件
spring.redis.host=192.168.19.166
spring.redis.port=6379
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
(3)测试
@SpringBootTest
class Qy163jedis2ApplicationTests {
//因为springboot整合redis时会把StringRedisTemplate创建并交与Spring容器管理
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
//查看redis中所有的key
Set<String> keys = redisTemplate.keys("*");
System.out.println("keys==="+keys);
//判断key是否存在 返回0表示不存在,反之就是存在
Boolean k6 = redisTemplate.hasKey("k6");
System.out.println(k6);
//删除指定key的内容
Boolean k61 = redisTemplate.delete("k6");
System.out.println(k61);
ValueOperations<String, String> forValue = redisTemplate.opsForValue();
//设置指定key的值
forValue.set("k1","cmx");
//获取指定key的值
String k1 = forValue.get("k1");
System.out.println("k1====="+k1);
//如果key不存在时,存入key
Boolean aBoolean = forValue.setIfAbsent("k2", "zmf");
System.out.println("是否存入成功"+aBoolean);
//递增
Long k3 = forValue.increment("k3", 20);
System.out.println(k3);
//hash操作
HashOperations<String, Object, Object> forHash = redisTemplate.opsForHash();
//将哈希表key中的字段field的值设为value
forHash.put("k9","name","cmx");
forHash.put("k9","age","16");
forHash.put("k9","sex","女");
HashMap<String, String> map = new HashMap<>();
//同时设置一个或多个kv对
map.put("name","zmf");
map.put("age","15");
map.put("sex","男");
forHash.putAll("k7",map);
//获得map中所有键值对
Map<Object, Object> k9 = forHash.entries("k9");
System.out.println(k9);
//查询map中的key值
Set<Object> k91 = forHash.keys("k9");
System.out.println(k91);
//查询map中的value
System.out.println(forHash.values("k9"));
//set相关 无须且不可重复的
SetOperations<String, String> forSet = redisTemplate.opsForSet();
//向集合添加一个或多个成员
Long add = forSet.add("sk1", "xyz", "xxx", "yyy", "zzz", "qqq");
System.out.println(add);
Long add1 = forSet.add("sk2", "xyz", "xxx", "fff", "hhh", "jjj");
System.out.println(add1);
//返回集合中的所有成员
System.out.println(forSet.members("sk1"));
//随机获取一个或多个元素
System.out.println(forSet.randomMember("sk1"));
//返回指定所有集合的交集
System.out.println(forSet.intersect("sk1", "sk2"));
//sort set有序集合
//向有序集合添加一个或多个成员,或者个更新已存在成员的分数
ZSetOperations zSet = redisTemplate.opsForZSet();
zSet.add("zk1","数学",88.00d);
zSet.add("zk1","语文",88.50d);
zSet.add("zk1","英语",55.00d);
zSet.add("zk1","历史",99.00d);
//通过索引区间返回有序集合指定区间内的成员
System.out.println(zSet.range("zk1", 0, -1));
}
}
2.RedisTemplate
它是StringRedisTemplate的父类,它类可以存储任意数据类型,但是任意类型必须序列化,默认采用的是jdk的序列化方式。jdk序列化方式阅读能力差,而且占用空间大. 我们在使用是一般需要人为指定序列化方式。
@SpringBootTest
class Qy163jedis2ApplicationTests2 {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
ValueOperations forValue = redisTemplate.opsForValue();
forValue.set("user",new User(1,"cmx","gu"));
Object user = forValue.get("user");
System.out.println(user);
}
}
这里存入对象 redis数据会乱码 需要加一个配置类 ,人为指定序列化方式。
@Configuration
public class RedisConfig {
//比如验证码
@Bean //该方法的返回对象交于spring容器管理
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
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);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(redisSerializer);
return template;
}
}
上面的连接都是连接的单机版的redis,真实项目它们的redis都是集群模式.