一、选择Redis的Java客户端
Spring Data Redis 是 Spring 的一部分,对 Redis 底层开发包进行了高度封装。在 Spring 项目中,可以使用Spring Data Redis来简化操作。
二、Spring Data Redis的使用方式
1、 导入Spring Data Redis的maven坐标
2、配置Redis数据源
3、编写配置类,创建RedisTemplate对象
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板类...");
RedisTemplate redisTemplate = new RedisTemplate();
// 设置Key的序列化器,默认为JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
4、通过RedisTemplate 对象操作Redis
//字符串
ValueOperations valueOperations = redisTemplate.opsForValue();
//无序Set
SetOperations setOperations = redisTemplate.opsForSet();
//列表
ListOperations listOperations = redisTemplate.opsForList();
//有序Set
ZSetOperations zsetOperations = redisTemplate.opsForZSet();
//哈希表
HashOperations hashOperations = redisTemplate.opsForHash();
public void testString(){
ValueOperations valueOperations = redisTemplate.opsForValue();
//set
valueOperations.set("city","Beijing");
//get
String city = (String) valueOperations.get("city");
System.out.println(city);
//setex
valueOperations.set("code","123456",30, TimeUnit.SECONDS);
//setnx
valueOperations.setIfAbsent("locak","1");
}
@Test
public void testHash(){
//哈希表
HashOperations hashOperations = redisTemplate.opsForHash();
//hset
hashOperations.put("hash1","name","tom");
hashOperations.put("hash1","age","20");
//hget
String name = (String) hashOperations.get("hash1", "name");
System.out.println(name);
//hkeys
Set keys = hashOperations.keys("hash1");
System.out.println(keys);
//hvals
List values = hashOperations.values("hash1");
System.out.println(values);
//hdel
// hashOperations.delete("hash1","age");
}
@Test
public void testList(){
//列表
ListOperations listOperations = redisTemplate.opsForList();
//lpush
listOperations.leftPushAll("mylist","a","b","c");
listOperations.leftPush("mylist","d");
//lrange
List list = listOperations.range("mylist", 0, -1);
System.out.println(list);
//rpop
listOperations.rightPop("mylist");
//llen
listOperations.size("mylist");
}
public void testSet(){
//无序Set
SetOperations setOperations = redisTemplate.opsForSet();
//sadd
setOperations.add("set1","a","b","c","d");
setOperations.add("set2","a","b","r","i");
//smembers
Set members = setOperations.members("set1");
System.out.println(members);
//scard
Long size = setOperations.size("set1");
System.out.println(size);
//sinter
Set intersect = setOperations.intersect("set1", "set2");
System.out.println(intersect);
//sunion
Set union = setOperations.union("set1", "set2");
System.out.println(union);
//srem
setOperations.remove("set1","a","b");
}
public void testZset(){
//有序Set
ZSetOperations zsetOperations = redisTemplate.opsForZSet();
//zadd
zsetOperations.add("zset1","a",10);
zsetOperations.add("zset1","b",12);
zsetOperations.add("zset1","c",9);
//zrange
Set range = zsetOperations.range("zset1", 0, -1);
System.out.println(range);
//zincrby
zsetOperations.incrementScore("zset1","c",10);
//zrem
zsetOperations.remove("zset1","a","b");
}
public void testCommon(){
//keys
Set keys = redisTemplate.keys("*");
System.out.println(keys);
//exists
Boolean name = redisTemplate.hasKey("name");
//type
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
//del
redisTemplate.delete("mylist");
}