springboot缓存篇(二)-redis 做缓存
来源:互联网 发布:nginx 默认根目录有 编辑:程序博客网 时间:2024/06/07 07:58
本文基于上一篇博客springboot的缓存技术,将缓存技术修改为使用redis数据库作为缓存技术。
本文结构:
- redis 介绍
- redis基于docker安装
- 添加pom 依赖
- 添加 redisconfig 配置
- 添加bean的序列化
redis 介绍
Redis 是一个开源的 使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志 型、Key-Value 数据库。
Redis 的 key 是字符串类型,但是 key 中不能包括边界字符 ,由于 key 不是 binary safe 的字符串,所以像”my key”和”mykey\n”这样包含空格和换行的 key 是不允许的。
Redis的vaule redis提供五种数据类型:string,hash,list,set及sorted set。
redis基于docker安装
docker 安装redis,安装命令如下
docker pull redis:3.2
启动命令
启动前需要建立映射目录“redis/data“
docker run -p 6379:6379 -v $PWD/redis/data:/data -d redis:3.2 redis-server --appendonly yes
代码修改
本例的代码是基于上一篇博客springboot的缓存技术的例子对代码进行新增。
添加pom 依赖
在pom.xml文件中新增redis的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
添加 redisconfig 配置
新建RedisConfig.java 配置文件,内容如下:
package com.us.example.config;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.Cache;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.interceptor.CacheErrorHandler;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;/** * Created by yangyibo on 17/1/16. */@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport{ private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class); @Autowired private Environment env; @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(env.getProperty("redis.hostname")); redisConnectionFactory.setPort(Integer.parseInt(env.getProperty("redis.port"))); return redisConnectionFactory; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(cf); return redisTemplate; } @Bean public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(600); return cacheManager; } public CacheErrorHandler errorHandler() { return new CacheErrorHandler(){ @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) { logger.warn("handleCacheGetError in redis: {}", exception.getMessage()); } @Override public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) { logger.warn("handleCachePutError in redis: {}", exception.getMessage()); } @Override public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) { logger.warn("handleCacheEvictError in redis: {}", exception.getMessage()); } @Override public void handleCacheClearError(RuntimeException exception, Cache cache) { logger.warn("handleCacheClearError in redis: {}", exception.getMessage()); }}; }}
在src/main/resources 目录下的application.properties文件中新增redis 的配置项
redis.hostname=localhostredis.port=6379
添加bean的序列化
Person 类实现Serializable 接口
public class Person implements Serializable { private static final long serialVersionUID = 133938246231808718L;
测试 同上一篇博客。
本文完整代码:https://github.com/527515025/springBoot.git
1 0
- springboot缓存篇(二)-redis 做缓存
- SpringBoot--使用redis做缓存(2)
- springboot+redis作缓存
- springboot整合redis缓存
- Springboot整合Redis缓存
- SpringBoot--使用Redis缓存
- springboot redis 数据缓存
- SpringBoot--使用Redis缓存
- SpringBoot--使用redis缓存(1)
- SpringBoot入门(五)Redis数据缓存
- spring缓存(二)--redis缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- Spring Boot中的缓存支持(二)使用Redis做集中式缓存
- 【二】Springboot+Mybatis+Redis实现用户信息查询缓存
- mybatis缓存(二)+redis
- springboot【16】缓存之Redis
- android KeyboardView使用
- 高速通用密码卡之西电捷通综合性测试分析
- ORA-24757 错误处理
- [00304]二八十六进制整数
- Android 静音模式、振动模式、普通模式判断
- springboot缓存篇(二)-redis 做缓存
- HTML5开发(一)
- 洛谷 P1098 字符串的展开
- JavaScript学习总结(13)——JavaScript常用正则表达式
- Qt之QAbstractItemView右键菜单
- 观察者模式的浅析
- Android 解决因未捕获异常而崩溃的问题
- 最小距离分类法
- UIFragment的简单使用