直接使用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

https://blog.csdn.net/lingshaoa/article/details/76999811

https://www.cnblogs.com/yhtboke/p/6429577.html

springmvc Cacheable (RedisTemplate / StringRedisTemplate)的更多相关文章

  1. Idea搭建SpringMVC框架(初次接触)

    公司转Java开发,做的第一个项目是SpringMVC框架,因为底层是同事封装,等完成整个项目,对SpringMVC框架的搭建还不是很了解,所以抽时间不忙的时候自己搭建了一个SpringMVC框架. ...

  2. SpringMVC系列(十四)Spring MVC的运行流程

    Spring MVC的运行流程图: 1.首先看能不能发送请求到Spring MVC的DispatcherServlet的url-pattern2.如果能发送请求,就看在Spring MVC中是否存在对 ...

  3. SpringMVC学习(二)——基于xml配置的springMVC项目(maven+spring4)

    可运行的附件地址:http://files.cnblogs.com/files/douJiangYouTiao888/springWithXML.zip 项目说明: 作者环境:maven3+jdk1. ...

  4. SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系

    一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...

  5. SpringMVC系列(十二)自定义拦截器

    Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口– preHandle():这个方法在业务处 ...

  6. SpringMVC系列(十六)Spring MVC与Struts2的对比

    • Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter• Spring MVC 会稍微比 Struts2 快些. Spring MVC 是基于方法设计, 而 Stu ...

  7. SpringMVC + ehcache( ehcache-spring-annotations)基于注解的服务器端数据缓存

    背景 声明,如果你不关心java缓存解决方案的全貌,只是急着解决问题,请略过背景部分. 在互联网应用中,由于并发量比传统的企业级应用会高出很多,所以处理大并发的问题就显得尤为重要.在硬件资源一定的情况 ...

  8. SpringMVC学习(二)

    SpringMVC入门(注解方式) 需求 实现商品查询列表功能. 第一步:创建Web项目 springmvc02 第二步:导入jar包 第三步:配置前端控制器 在WEB-INF\web.xml中配置前 ...

  9. Spring(或者SpringBoot)整合Spring-Session实现共享session

    传统Spring 先引入依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

随机推荐

  1. Mac中的brew

    最近要研究字体识别tesseract,才了解到brew,惭愧惭愧. 1.brew是一个软件包管理工具,类似于centos下的yum或者ubuntu下的apt-get,非常方便,免去了自己手动编译安装的 ...

  2. Android Paint类介绍以及浮雕和阴影效果的设置(转)

    转自:https://blog.csdn.net/lpjishu/article/details/45558375 Paint类介绍 Paint即画笔,在绘制文本和图形用它来设置图形颜色, 样式等绘制 ...

  3. Java高频经典面试题(第一季)四:方法的参数传递机制

    考点? 方法的参数传递机制 String,包装类等对象的不可变性 方法的参数传递机制: ①形参是基本数据类型 传递数据值 ②实参是引用数据类型 传递地址值 特殊的类型:String.包装类等对象不可变 ...

  4. C++ 之获取map元素[转]

    链接:https://www.cnblogs.com/jianfeifeng/p/11089799.html 对于map对象, count成员返回值只能是0或者1,map容器只允许一个键对应一个实例. ...

  5. github gist 查看html

    gist GitHub Gist 指南 https://blog.csdn.net/yz18931904/article/details/80482166 通过修改hosts解决gist.github ...

  6. sed编辑器基础

    一. 更多的替换选项 ①替换标记 root@localhost sed]# cat data4.txt This is a test of the test script. This is the s ...

  7. 洛谷P2015 二叉苹果树(树状dp)

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...

  8. 工程师技术(三):独立Web站点的快速部署、虚拟Web主机的部署、配置网页内容访问、使用自定Web根目录、配置安全Web服务、部署并测试WSGI站点

    一.独立Web站点的快速部署 目标: 本例要求为 http://server0.example.com 配置Web站点,要求如下: 1> 从http://classroom/pub/materi ...

  9. layui-treeTable v2.0添加搜索功能

    layui-treeTable 添加搜索功能 在树形表格头部加一个input框: <div class="layui-inline"> <input class= ...

  10. django 重写 mysql 连接库实现连接池

    django 重写 mysql 连接库实现连接池 问题 django 项目使用 gunicorn + gevent 部署,并设置 CONN_MAX_AGE 会导致 mysql 数据库连接数飙升,在高并 ...