springmvc Cacheable (Redis)
直接使用spring缓存请见:https://www.cnblogs.com/hanjun0612/p/11661340.html
RedisTemplate和StringRedisTemplate配置方法基本一致
废话不多,直接上代码。
一,单独创建 spring-redis.xml
看一下我的redis.properties
# Redis settings redis.host=........ redis.port=6379 redis.password=123 redis.timeOut=10000 redis.pass= redis.maxTotal=200 redis.maxIdle=50 redis.minIdle=8 redis.maxWaitMillis=10000 redis.testOnBorrow=true redis.testOnReturn=true redis.testWhileIdle=true redis.timeBetweenEvictionRunsMillis=30000 redis.numTestsPerEvictionRun=10 redis.minEvictableIdleTimeMillis=60000 redis.database=14
spring-redis.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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd" default-lazy-init="true" > <!--redis配置 --> <bean name="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" lazy-init="true"> <property name="maxTotal" value="${redis.maxTotal}"/> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="minIdle" value="${redis.minIdle}"/> <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> <property name="testOnReturn" value="${redis.testOnReturn}"/> <property name="testWhileIdle" value="${redis.testWhileIdle}"/> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/> </bean> <!-- Jedis ConnectionFactory 数据库连接配置--> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="password" value="${redis.password}" /> <property name="database" value="${redis.database}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <!-- 将session放入redis --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="7200" /> <property name="cookieSerializer" ref="defaultCookieSerializer"/> </bean> <!-- StringRedisTemplate 序列化方式 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory" > <property name="keySerializer" ref="keySerializer"/> <property name="valueSerializer" ref="jackson2JsonRedisSerializer"/> </bean> <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="jackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> <!--@Cacheable使用Redis缓存 : spring自己的缓存管理器,这里定义了缓存位置名称 ,即注解中的value --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.data.redis.cache.RedisCache"> <constructor-arg name="redisOperations" ref="redisTemplate"></constructor-arg> <constructor-arg name="name" value="accessCode"></constructor-arg> <constructor-arg name="prefix" value="Access:"></constructor-arg> <constructor-arg name="expiration" value="600"></constructor-arg> </bean> </set> </property> </bean> </beans>
主要看 redisTemplate 和 cacheManager 这两块。
redisTemplate:我使用了StringRedisTemplate,你们也可以使用RedisTemplate,但是当中的序列化方式,需要自己配置。
而这一块配置的原因,在于@Cacheable使用时,告诉它将对象序列化成string存储。
cacheManager: 这里就是配置使用redis缓存了。除了accessCode作为@Cacheable(value="accessCode"),其他都没什么关系。
PS:
<constructor-arg name="prefix" value="Access:"></constructor-arg>
中的 Access: 代表生成一个Access文件夹
不带冒号“:”的话,就只是单纯的前缀
二,配置applicationContext.xml
<!-- 引入属性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/env.properties</value> <value>classpath:properties/jdbc.properties</value> <value>classpath:properties/redis.properties</value> </list> </property> </bean> <!-- 启动缓存 --> <cache:annotation-driven />
没什么花头,主要是引入配置文件,以及启动缓存。
三,使用@Cacheable
@Cacheable(value="accessCode",key="#userId + #menuUrl") public PageAccessCode getPageAccessCode(Integer userId, String menuUrl,String curLang){ return new PageAccessCode(); }
四,验证。
1 可以在上面方法打断点,刷新页面后,第二次略过。则代表缓存成功了。
2 可以直接看redis客户端。(记得选择数据库,我选的14)
PS:
错误:redis -> 元素 'bean' 必须不含字符 [子级]
按照配置文件,手打一遍!
参考文章:
https://blog.csdn.net/u013041642/article/details/80370156
相关文章
- Redis安装与配置
- Redis安装完后redis-cli无法使用(redis-cli: command not found)
- redis发布/订阅模式
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- Redis的Python客户端redis-py的初步使用
- centos8平台redis cluster集群添加/删除node节点(redis5.0.7)
- redis集群之REDIS CLUSTER
- 深入理解Spring Redis的使用 (五)、常见问题汇总
- centos8(linux):源码编译安装redis7(Redis server v=7.0.5)
- Redis 高性能IO模型:为什么单线程Redis能那么快?
- Redis 就是让你彻底搞懂redis集群
- Redis缓存穿透、缓存雪崩、redis并发问题 并发竞争key的解决方案 (阿里)
- 在 SAP Kyma 上使用 Redis 服务
- 〖Python 数据库开发实战 - Python与Redis交互篇④〗- 利用 redis-py 实现集合与有序集合的常用指令操作
- 〖Python 数据库开发实战 - Python与Redis交互篇⑤〗- 利用 redis-py 实现哈希数据类型的常用指令操作
- 深入理解 Redis Template及4种序列化方式__spring boot整合redis实现RedisTemplate三分钟快速入门
- Redis使用示例及在PHP环境中用redis存储session
- Redis源代码分析(十二)--- redis-check-dump本地数据库检測
- Redis 5.0 redis-cli --cluster help说明
- redis-trib.rb命令详解
- redis分布式锁深度剖析(超时情况)
- 遇到问题之-LNMP编译安装Redis构建高速Redis缓存
- ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
- 【redis】Redis缓存失效、雪崩、穿透、击穿、并发等案例分析难题解决方案
- Redis 导航
- Redis is configured to save RDB snapshots redis的 rdb持久化错误
- redis单线程原理___Redis为何那么快-----底层原理浅析