SpringBoot--使用redis做缓存(2)
2017-05-17 11:45
633 查看
一、redis做缓存的优缺点
(1)优点:简单灵活、数据结构丰富、高速读写。
(2)缺点:持久化、要尽量避免或者设计机制保障。存储成本高,存储在内存中,从而要尽量避免使用redis进行大量数据存储。
二、示例
本文使用Spring的RedisTemplate来操作Redis。采用SpringBoot对redis的自动配置。
(1)配置redis的连接信息。
(2)Redis配置类添加RedisTemplate。
(3)编写redis客户端。
(4)编写测试Controller
(5)运行测试结果
2017-05-17 11:38:57.513 [http-nio-8080-exec-1] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-05-17 11:38:57.608 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据:redis测试内容
2017-05-17 11:39:00.608 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 等待3秒后从Redis中读取数据:redis测试内容
2017-05-17 11:39:03.609 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 等待5秒后尝试读取过期的数据:null
2017-05-17 11:39:03.612 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据User:User{name='张三', password='zs123'}
2017-05-17 11:39:06.614 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据User:[张飞, 刘备, 关于]
2017-05-17 11:39:09.614 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据list:null
三、总结
从以上测试结果可以看出目前缓存已经生效。采用此方法不仅仅可以缓存字符串而且针对对象和集合都可以进行缓存。项目代码参考地址SpringBoot中使用redis缓存
(1)优点:简单灵活、数据结构丰富、高速读写。
(2)缺点:持久化、要尽量避免或者设计机制保障。存储成本高,存储在内存中,从而要尽量避免使用redis进行大量数据存储。
二、示例
本文使用Spring的RedisTemplate来操作Redis。采用SpringBoot对redis的自动配置。
(1)配置redis的连接信息。
spring.redis.database=0 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.timeout=0
(2)Redis配置类添加RedisTemplate。
package com.liutao.config; import com.liutao.utils.RedisObjectSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置类 * * @author LIUTAO * @version 2017/5/16 * @see */ @Configuration public class RedisConfig { @Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); } @Bean public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new RedisObjectSerializer()); template.afterPropertiesSet(); return template; } }
(3)编写redis客户端。
package com.liutao.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * redis客户端工具类 * * @author LIUTAO * @version 2017/5/15 * @see */ @Component public class RedisClient { private static Logger logger = LoggerFactory.getLogger(RedisClient.class); @Autowired private RedisTemplate redisTemplate; /** * 保存数据进入缓存 * * @param key 键 * @param value 值 * @param cacheSeconds 缓存时间 */ public void putObject(final String key, final Object value, final int cacheSeconds) { redisTemplate.opsForValue().set(key, value, cacheSeconds, TimeUnit.SECONDS); } /** * get操作 * * @param key 键 * @return */ public Object getObject(final String key) { return redisTemplate.opsForValue().get(key); } /** * 从缓存中删除对象 * * @param key 键 * @return */ public int del(final String key) { try { redisTemplate.delete(key); return 1; } catch (Exception e) { return 0; } } /** * 判断缓存数据是否存在 * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } }
(4)编写测试Controller
package com.liutao.controller; import com.liutao.entity.User; import com.wordnik.swagger.annotations.Api; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.liutao.utils.RedisClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * 用户控制层 * * @author LIUTAO * @version 2017/3/29 * @see */ @RestController @Api(value = "test") @RequestMapping("/liutao/v1") public class UserController { private Logger logger = LoggerFactory.getLogger(UserController.class); @Autowired private RedisClient redisClient; /** * 缓存测试 * * @return * @author SHANHY * @create 2016年9月12日 */ @GetMapping("/redisTest") public String redisTest() { try { redisClient.putObject("test-key", "redis测试内容", 5);// 缓存有效期5秒 logger.info("从Redis中读取数据:" + redisClient.getObject("test-key").toString()); TimeUnit.SECONDS.sleep(3); logger.info("等待3秒后从Redis中读取数据:" + redisClient.getObject("test-key").toString()); TimeUnit.SECONDS.sleep(3); logger.info("等待5秒后尝试读取过期的数据:" + redisClient.getObject("test-key")); redisClient.putObject("user",new User("张三","zs123"),10); logger.info("从Redis中读取数据User:" + redisClient.getObject("user")); List<String> list = new ArrayList<>(); list.add("张飞"); list.add("刘备"); list.add("关于"); redisClient.putObject("list",list,5); TimeUnit.SECONDS.sleep(3); logger.info("从Redis中读取数据User:" + redisClient.getObject("list")); TimeUnit.SECONDS.sleep(3); logger.info("从Redis中读取数据list:" + redisClient.getObject("list")); } catch (InterruptedException e) { e.printStackTrace(); } return "OK"; } }
(5)运行测试结果
2017-05-17 11:38:57.513 [http-nio-8080-exec-1] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-05-17 11:38:57.608 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据:redis测试内容
2017-05-17 11:39:00.608 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 等待3秒后从Redis中读取数据:redis测试内容
2017-05-17 11:39:03.609 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 等待5秒后尝试读取过期的数据:null
2017-05-17 11:39:03.612 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据User:User{name='张三', password='zs123'}
2017-05-17 11:39:06.614 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据User:[张飞, 刘备, 关于]
2017-05-17 11:39:09.614 [http-nio-8080-exec-1] INFO com.liutao.controller.UserController - 从Redis中读取数据list:null
三、总结
从以上测试结果可以看出目前缓存已经生效。采用此方法不仅仅可以缓存字符串而且针对对象和集合都可以进行缓存。项目代码参考地址SpringBoot中使用redis缓存
相关文章推荐
- Spring Boot 基于注解的 Redis 缓存使用
- Redis系列三 - Spring boot如何使用redis做缓存及缓存注解的用法总结
- Spring Boot使用redis做数据缓存
- REDIS学习(3.2)spring boot 使用redis作为缓存
- spring boot2.0使用redis做缓存
- [置顶] 在Springboot上使用jedis来操作缓存redis --jedis的配置
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十六)使用Redis做集中式缓存(2)
- springboot项目使用redis数据库作缓存
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- Spring Boot 基于注解的 Redis 缓存使用详解
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- spring boot使用redis做数据缓存
- Redis系列三 - Spring boot如何使用redis做缓存及缓存注解的用法总结
- SpringBoot中使用Redis实现缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot使用redis做数据缓存
- 详解Spring boot使用Redis集群替换mybatis二级缓存
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十五)使用Redis做集中式缓存(1)
- (十五)SpringBoot之使用Redis做缓存数据