Spring Data Redis中提供了一个高度封装的类:RedisTemplate,将同一类型操作封装为operation接口,具体分类如下:(1)ValueOperations:简单K-V操作;(2)HashOperations:针对map类型的数据操作;(3)ListOperations:针对list类型的数据操作;(4)SetOperations:针对set类型数据操作;(5)ZSetOperations:针对zset类型数据操作
一. 初始化配置
1. 在springboot工程中添加spring data redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 配置yml文件(连接的是windows中的redis)
spring:
redis:
host: localhost
port: 6379
#password: 123456
database: 0 #0号数据库(redis提供了16个数据库)
jedis: #redis连接池
pool:
max-active: 8 #最大连接数
max-wait: 1ms #连接池最大阻塞等待时间
max-idle: 4 #连接池中最大空闲连接
min-idle: 0 #连接池中最小空闲连接
3. 编写配置类
package com.example.redis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//设置key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
二. 操作Redis
1. 操作字符串String(ValueOperations)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
ValueOperations valueOperations = redisTemplate.opsForValue();
//等价于:SET key value —— 设置指定key的值
valueOperations.set("name", "Jack");
//等价于:GET key —— 获取指定key的值
String value = (String) valueOperations.get("name");
System.out.println(value); //Jack
//等价于:SETEX key seconds value —— 设置指定key的值,并将key的过期时间设为seconds秒
valueOperations.set("age", "20", 10l, TimeUnit.SECONDS); //10秒
//等价于:SETNX key value —— 只有在key不存在时才设置key的值
Boolean isSet = valueOperations.setIfAbsent("name", "Tom");
System.out.println(isSet); //false
}
}
2. 操作哈希hash(HashOperations)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
HashOperations hashOperations = redisTemplate.opsForHash();
//等价于:HSET key field value —— 将哈希表中key的字段field的值设置为value
hashOperations.put("user", "name", "Tom");
hashOperations.put("user", "age", "22");
hashOperations.put("book", "price", "56");
//等价于:HGET key field —— 获取存储在哈希表中指定key中的field的value
String username = (String) hashOperations.get("user", "name");
System.out.println(username); //Tom
String price = (String) hashOperations.get("book", "price");
System.out.println(price); //56
//等价于:HKEYS key —— 获取哈希表中指定key中的所有field
Set keys = hashOperations.keys("user");
System.out.println(keys); //[name, age]
//等价于:HVALS key —— 获取哈希表中指定key中的所有value
List values = hashOperations.values("user");
System.out.println(values); //[Tom, 22]
//等价于:HGETALL key —— 获取在哈希表中指定key中的所有field和value
Map map = hashOperations.entries("user");
System.out.println(map); //{name=Tom, age=22}
}
}
3. 操作列表List(ListOperations)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
ListOperations listOperations = redisTemplate.opsForList();
//等价于:LPUSH key value1 [value2] —— 将一个或多个值插入到列表头部
listOperations.leftPush("myList", "a"); //一次插入一个
listOperations.leftPushAll("myList", "b", "c", "d"); //一次插入多个
//等价于:LRANGE key start stop:获取列表指定范围内的元素(查询所有,lrange key 0 -1)
List<String> list = listOperations.range("myList", 0, -1);
System.out.println(list); //[d, c, b, a]
//等价于:RPOP key —— 移除并获取列表最后一个元素
Object element = listOperations.rightPop("myList");
System.out.println(element); //a
//等价于:LLEN key —— 获取列表长度
Long listSize = listOperations.size("myList");
System.out.println(listSize); //3
}
}
4. 操作集合set(SetOperations)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
SetOperations setOperations = redisTemplate.opsForSet();
//等价于:SADD key member1 [member2] —— 向集合添加一个或多个成员
setOperations.add("mySet", "a", "a", "c", "d");
setOperations.add("mySet2", "c", "e", "f");
//等价于:SMEMBERS key —— 返回集合中的所有成员
Set<String> set = setOperations.members("mySet");
System.out.println(set); //[c, a, d]
//等价于:SCARD key —— 获取集合的成员数
Long setSize = setOperations.size("mySet");
System.out.println(setSize); //3
//等价于:SINTER key1 [key2] —— 返回给定所有集合的交集
Set<String> interSet = setOperations.intersect("mySet", "mySet2");
System.out.println(interSet); //[c]
//等价于:SUNION key1 [key2] —— 返回所有给定集合的并集
Set<String> unionSet = setOperations.union("mySet", "mySet2");
System.out.println(unionSet); //[a, c, d, e, f]
//等价于:SDIFF key1 [key2] —— 返回给定所有集合的差集
Set<String> diffSet = setOperations.difference("mySet", "mySet2");
System.out.println(diffSet); //[a, d]
//等价于:SREM key member1 [member2] —— 移除集合中一个或多个成员
setOperations.remove("mySet","a", "c");
Set<String> set2 = setOperations.members("mySet");
System.out.println(set2); //[d]
}
}
5. 操作有序集合zset(ZSetOperations)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//等价于:ZADD key score1 member1 [score2 member2] —— 向有序集合添加一个或多个成员,或者更新已存在成员的分数
zSetOperations.add("myZSet", "a", 2.3);
zSetOperations.add("myZSet", "b", 7.5);
zSetOperations.add("myZSet", "c", 4.2);
zSetOperations.add("myZSet", "d", 8.9);
//等价于:ZRANGE key start stop [WITHSCORES] —— 通过索引区间返回有序集合中指定区间内的成员
Set<String> set = zSetOperations.range("myZSet", 0, -1);
System.out.println(set); //[a, c, b, d]
//等价于:ZINCRBY key increment member —— 有序集合中对指定成员的分数加上增量increment(修改分数)
zSetOperations.incrementScore("myZSet", "c", 3.5);
Set<String> set2 = zSetOperations.range("myZSet", 0, -1);
System.out.println(set2); //[a, b, c, d]
//等价于:ZREM key member [member .….] —— 移除有序集合中的一个或多个成员
zSetOperations.remove("myZSet", "a", "b");
Set<String> set3 = zSetOperations.range("myZSet", 0, -1);
System.out.println(set3); //[c, d]
}
}
6. 通用操作
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
//等价于:KEYS pattern —— 查找所有符合给定模式的key(keys *查找所有key)
Set<String> keys = redisTemplate.keys("*");
System.out.println(keys);
//等价于:EXISTS key —— 检查给定key是否存在
Boolean isExist = redisTemplate.hasKey("name");
System.out.println(isExist);
//等价于:TYPE key —— 返回key所储存的值的类型
DataType dataType = redisTemplate.type("name");
System.out.println(dataType);
//等价于:DEL key —— 在key存在时删除key
redisTemplate.delete("name");
}
}