之前有写过一篇Spring Cache注解+Redis
今天对Cache+Redis配置的优化。
首页还是Jar的依赖,请看之前的文章,这里不做赘述。
然后后XML的配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" default-autowire="byName"> <context:property-placeholder location="classpath*:config/redis.properties" ignore-unresolvable="true"/> <!-- 启用缓存注解功能 --> <cache:annotation-driven cache-manager="cacheManager"/> <!--Redis Cache 配置--> <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg name="redisOperations" ref="redisTemplate"/> </bean> <!-- redis 相关配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="maxWaitMillis" value="${redis.maxWait}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="poolConfig"/> <property name="port" value="${redis.port}"/> <property name="hostName" value="${redis.host}"/> <property name="timeout" value="${redis.timeout}"/> <property name="password" value="${redis.pwd}"/> <property name="database" value="${redis.database}"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <!--<property name="defaultSerializer" ref="stringRedisSerializer"/>--> <property name="keySerializer" ref="stringRedisSerializer"/> <!--<property name="valueSerializer" ref="stringRedisSerializer"/>--> <property name="hashKeySerializer" ref="stringRedisSerializer"/> <!--<property name="hashValueSerializer" ref="stringRedisSerializer"/>--> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </beans>
redis.properties请看上一篇文章;
缓存注解使用:
在service实现方法上加注解
@Override @Cacheable(value = "ShardingTableCache", key = "'shardingTableName:' + #appId + ':' + #appUserId") public Integer getShardingTableName(String appId, String appUserId) { return mapper.getShardingTableName(appId, appUserId); } @Override @CacheEvict(value = "ShardingTableCache", key = "'shardingTableName:' + #appId + ':' + #appUserId") public int updateShardingTable(String appId, String appUserId, int shardingTable) { return mapper.updateShardingTable(appId, appUserId, shardingTable); }
使用redis客户端RedisDesktopManager查看效果如下图:
中间遇到的问题: 插入的hash类型的key前面会有一堆的\xac\xed\x00\x05t\x00\tb 这种东西
处理方法:redisTemplate中添加<property name="keySerializer" ref="stringRedisSerializer"/>等
相关推荐
之前整合springcache+redis的时候到网上搜了很多代码方案,但是注解都不起作用,搞了很长时间,也是很郁闷。所以,把成果分享一下,避免更多人踩坑,哈哈
本篇文章主要介绍了详解配置Spring4.0注解Cache+Redis缓存的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
博客http://blog.csdn.net/poorcoder_/article/details/59541710的代码。主要描述spring通过注解整合redis用作缓存的实例。
redis-cluster完整项目,下载导入eclipse即可
使用spring集成redis基于注解的方式来实现数据的缓存
Springboot中Spring-cache与redis整合。这也是一个不错的框架,与spring的事务使用类似,只要添加一些注解方法,就可以动态的去操作缓存了,减少代码的操作。
SpringBoot2.0整合SpringCache和Redis 1 1. SpringBoot对应(自带)RedisClient是不同的 1 2. 实战 1 2.1. maven配置 1 2.2. yml配置 1 2.3. RedisConfig配置 1 2.4. RedisController 2 3. Spring缓存注解@...
springboot1.x基于spring注解实现J2Cache两级缓存集成,一级缓存:ehcache,二级缓存:redis,代码可直接运行(根据项目中的README.md提示)
spring boot —— redis 缓存注解使用教程 示例项目地址: 依赖 在pom文件添加如下依赖 <groupId>org.springframework.boot <artifactId>spring-boot-starter-data-redis 配置 在application.yml配置文件添加...
3.剖析SpringCache常用注解 4.总结springcache在开发中遇到的大坑小坑 微信文章的阅读量 1.微信文章的阅读量PV场景介绍 2.高并发的微信文章的阅读量PV技术原理 3.案例实战:SpringBoot+Redis实现微信文章的PV统计 ...
基于Spring cache 封装,可以使用spring 注解 @Cacheable 支持Spring 4(guava) 和Spring 5(caffeine) 二级缓存都使用redis 相关配置 注解支持 /** redis过期时间 */ redisExpires; /** 访问后过期时间,...
Spring+Redis实现无缝布拉格插件(com.shawntime.utils.rwdb) Redis操作封装(com.shawntime.utils.cache.redis) Redis环球锁实现(com.shawntime.utils.lock) 读写锁控制强制读取存储同步 线程池管理类 配置文件...
JetCache提供了比SpringCache更强大的注解,可以原生的支持TTL、两级缓存、分布式自动刷新,提供了Cache接口用于手工缓存操作。当前有四个实现,RedisCache、TairCache、CaffeineCache和一个简易的...
66 第二部分 点睛Spring MVC 4.x 第4 章 Spring MVC 基础 .... .......................... 72 第三部分 实战Spring Boot 第5 章 Spring Boot 基础 .... ......................... 122 第6 章 Spring Boot 核心 .....
# demoWeb 一个基于SpringMVC的web框架 1.0.5 从web项目迁移成maven项目 ...Spring Cache增加redis缓存实现 Mybatis使用二级缓存,增加redis实现 增加reactJs 增加Mybatis插件pageHelper,Mapper doc内有相关文档
1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 Spring Boot的启动器Starter详解 1.38 Spring Boot集成Redis实现缓存机制 1.39 Spring Boot Cache理论篇 1.40 Spring ...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...