赞
踩
解压后的文件目录
进入Redis-x64-3.2.100文件夹
(PS:如果springboot配置完redis后,如果要取出redis中放置的数据应该要开启客户端,不然会报错)
#缓存
redis:
database: 3
host: 127.0.0.1
jedis:
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
shutdown-timeout: 100
password: ''
port: 6379
timeout: 3600
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </exclusion> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
@Autowired private RedisTemplate<Object,Object> redisTemplate; @Override public List<User> getAllList() { //重新定义字符串系列化 RedisSerializer redisSerializer=new StringRedisSerializer(); //将字符串序列化加到模板key中 redisTemplate.setConnectionFactory(connectionFactory(host,port,password,database)); 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); // 设置值(value)的序列化采用Jackson2JsonRedisSerializer。 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // 设置键(key)的序列化采用StringRedisSerializer。 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); redisTemplate.setKeySerializer(redisSerializer); //查询缓存里是否有过记录 List<User> userList=(List<User>)redisTemplate.opsForValue().get("getList"); if(userList!=null){ //有就直接返回 无需查询数据库 return userList; }else{ //没有就加载到缓存里面 redisTemplate.opsForValue().set("getList",userMapper.getAllList()); return userMapper.getAllList(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。