SpringBoot集成RedisTemplate
1. 导入依赖
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.yaml文件配置
先查部署服务器上redis是集群还是单机
(1)单机
spring:
redis:
host: 127.0.0.1
port: 7001
timeout=5000
#有密码加上密码
password:
(2)集群
redis:
cluster:
nodes: 127.0.0.1:7001,127.0.0.1:7002 #这里可以配置多个
3.建议配置上"序列化",方便后期操作使用
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// key序列化
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
// value序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
redisTemplate.setKeySerializer(stringSerializer);// key序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
没有序列化的数据存进去如下图所示,这是redis本身序列化后的数据
4.RedisTemplate使用和redis几种类型数据操作
(1)使用@Autowired注入RedisTemplate
@Autowired
RedisTemplate redisTemplate;
(2)String类型
//1、通过redisTemplate设置值和获取值
redisTemplate.boundValueOps("StringKey").set("StringValue");
//设置值的同时设置过期时间
redisTemplate.boundValueOps("StringKey").set("StringValue",30, TimeUnit.MINUTES);
//获取值
String str1 = (String) redisTemplate.boundValueOps("StringKey").get();
//2、通过BoundValueOperations设置值和获取值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
//设置值的同时设置过期时间
stringKey.set("StringValue",30, TimeUnit.MINUTES);
//获取值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
String str2 = (String) stringKey.get();
//3、通过ValueOperations设置值和获取值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
//设置值的同时设置过期时间
ops.set("StringValue","StringVaule",30, TimeUnit.MINUTES);
//获取值
ValueOperations ops = redisTemplate.opsForValue();
String str3 = (String) ops.get("StringKey");
//4、单独设置过期时间
redisTemplate.boundValueOps("StringKey").expire(30,TimeUnit.MINUTES); // 缓存30分钟
redisTemplate.expire("StringKey",30,TimeUnit.MINUTES);
//5、删除Key
Boolean result = redisTemplate.delete("StringKey");
//6、顺序递增和递减
redisTemplate.boundValueOps("StringKey").increment(3L);
redisTemplate.boundValueOps("StringKey").increment(-3L);
(3)List类型
//1、通过redisTemplate设置值
redisTemplate.boundListOps("listKey").leftPush("listLeftValue1"); //左进
redisTemplate.boundListOps("listKey").rightPush("listRightValue2"); //右进
//2、通过BoundValueOperations设置值
BoundListOperations listKey = redisTemplate.boundListOps("listKey");
listKey.leftPush("listLeftValue3"); //左进
listKey.rightPush("listRightValue4"); //右进
//3、通过ValueOperations设置值
ListOperations opsList = redisTemplate.opsForList();
opsList.leftPush("listKey", "listLeftValue5"); //左进
opsList.rightPush("listKey", "listRightValue6"); //右进
//4、设置过期时间
redisTemplate.boundValueOps("listKey").expire(30,TimeUnit.MINUTES); // 缓存30分钟
redisTemplate.expire("listKey",30,TimeUnit.MINUTES);
//5、获取List缓存全部内容
List listKey1 = redisTemplate.boundListOps("listKey").range(0, -1);
//6、从左或从右弹出一个元素
String listKey2 = (String) redisTemplate.boundListOps("listKey").leftPop(); //从左侧弹出一个元素
String listKey3 = (String) redisTemplate.boundListOps("listKey").rightPop(); //从右侧弹出一个元素
(4)Hash类型
//1、通过redisTemplate设置值
redisTemplate.boundHashOps("HashKey").put("HashKey1", "HashVaue1");
//获取值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();//获取Map的Key
List values1 = redisTemplate.boundHashOps("HashKey").values();//获取Map的values
Map entries = redisTemplate.boundHashOps("HashKey").entries();//map 键值对
//2、通过BoundValueOperations设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("HashKey2", "HashVaue2");
//获取值
Set keys2 = hashKey.keys();//获取Map的Key
List values2 = hashKey.values();//获取Map的values
Map entries1 = hashKey.entries();//map 键值对
//3、通过ValueOperations设置值和获取值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey","HashKey3", "HashVaue3");
//获取值
Set keys3 = hashOps.keys();//获取Map的Key
List values3 = hashOps.values();//获取Map的values
Map entries2 = hashOps.entries();//map 键值对
//4、设置过期时间
redisTemplate.boundValueOps("HashKey").expire(30,TimeUnit.MINUTES); // 缓存30分钟
redisTemplate.expire("HashKey",30,TimeUnit.MINUTES);
(5)set类型
//1、通过redisTemplate设置值
redisTemplate.boundSetOps("setKey").add("setValue1", "setValue2", "setValue3");
//获取值
Set set1 = redisTemplate.boundSetOps("setKey").members();
//2、通过BoundValueOperations设置值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
setKey.add("setValue1", "setValue2", "setValue3");
//获取值
Set set2 = setKey.members();
//3、通过ValueOperations设置值
SetOperations setOps = redisTemplate.opsForSet();
setOps.add("setKey", "SetValue1", "setValue2", "setValue3");
//获取值
Set set3 = setOps.members("setKey");
//4、设置过期时间
redisTemplate.boundValueOps("setKey").expire(30,TimeUnit.MINUTES); // 缓存30分钟
redisTemplate.expire("setKey",30,TimeUnit.MINUTES);
5.存储新建文件夹
一个 ‘ :’相当于一个文件夹
//在新加入数据的时候 设置Key
String key = "152::TILAKE::";
//以Srting的为例 其他类型一样 修改key就可以
redisTemplate.boundValueOps(key).set("StringValue");
版权声明:本文为abaibai12原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。