配置

 <?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 以前项目中的配置,注意需要添加Spring Data Redis等jar包 -->
<description>redis配置</description> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="maxTotal" value="${redis.pool.maxActive}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
</bean> <!-- JedisConnectionFactory -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.master.ip}"/>
<property name="port" value="${redis.master.port}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connectionFactory-ref="jedisConnectionFactory">
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean> <!--spring cache-->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
c:redisOperations-ref="redisTemplate">
<!-- 默认缓存10分钟 -->
<property name="defaultExpiration" value="600"/>
<property name="usePrefix" value="true"/>
<!-- cacheName 缓存超时配置,半小时,一小时,一天 -->
<property name="expires">
<map key-type="java.lang.String" value-type="java.lang.Long">
<entry key="halfHour" value="1800"/>
<entry key="hour" value="3600"/>
<entry key="oneDay" value="86400"/>
<!-- shiro cache keys -->
<entry key="authorizationCache" value="1800"/>
<entry key="authenticationCache" value="1800"/>
<entry key="activeSessionCache" value="1800"/>
</map>
</property>
</bean> <!-- cache注解,和spring-ehcache.xml中的只能使用一个 -->
<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>
</beans>

使用注解

 package com.wangzhixuan.service.impl;

 import java.io.Serializable;
import java.util.Date; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import com.wangzhixuan.mapper.UserMapper;
import com.wangzhixuan.model.User; @Service
public class TestService {
@Autowired
private UserMapper userMapper; @Cacheable(value = "hour", key = "#id")
public User selectById(Serializable id) {
return userMapper.selectById(id);
}
@Cacheable(value = "hours")
public String date() {
return new Date().toString();
}
}

spring+redis的更多相关文章

  1. spring+redis 集群下的操作

    文章就是记录一下工作当中的用到的点,与测试方法以备用,会不断更新. 配置文件spring-redis.xml: <?xml version="1.0" encoding=&q ...

  2. redis之(二十一)redis之深入理解Spring Redis的使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  3. 深入理解Spring Redis的使用 (一)、Spring Redis基本使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  4. spring+redis实现缓存

    spring + redis 实现数据的缓存 1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个j ...

  5. spring redis入门

    小二,上菜!!! 1. 虚拟机上安装redis服务 下载tar包,wget http://download.redis.io/releases/redis-2.8.19.tar.gz. 解压缩,tar ...

  6. 分布式缓存技术redis学习—— 深入理解Spring Redis的使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  7. 深入理解Spring Redis的使用 (七)、Spring Redis 使用 jackson序列化 以及 BaseDao代码

    之前在介绍Spring Redis进行存储的时候,都是通过RedisTemplate中的defaultSerializer,即JdkSerializationRedisSerializer.通过Jdk ...

  8. Spring+Redis集成+关系型数据库持久化

    本篇文章主要介绍了"Spring+Redis集成+关系型数据库持久化",主要涉及到Spring+Redis集成+关系型数据库持久化方面的内容,对于Spring+Redis集成+关系 ...

  9. spring redis @Cacheable注解使用部分错误及无效原因

    spring redis @Cacheable注解使用部分错误及无效原因 说明:     spring项目用到redis注解无效,解决问题中遇到一堆BUG,各种搜索,看了许多错误解决方案一一测试,对于 ...

  10. spring+redis+nginx 实现分布式session共享

    1,spring 必须是4.3以上版本的 2,maven配置 添加两个重要的依赖 <dependency> <groupId>org.springframework.sessi ...

随机推荐

  1. android五种存储方式

    http://www.cnblogs.com/smalltigerlee/archive/2011/11/10/2244143.html

  2. pod install 无限卡顿

    pod install  被墙了,请大家换成pod install --verbose --no-repo-update

  3. Extjs swfUpload 多图片上传

    一.属性介绍   类型 默认值 描述 upload_url String   处理上传文件的服务器端页面的url地址,可以是绝对地址,也可以是相对地址,当为相对地址时相对的是当前代码所在的文档地址 p ...

  4. C#语法糖,让编程更具乐趣

    一.什么是语法糖 语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法 ...

  5. jquery 重写 ajax提交并判断权限后 使用load方法报错解决方法

    jQuery(function ($) {    // 备份jquery的ajax方法        var _ajax = $.ajax;    // 重写ajax方法,先判断登录在执行succes ...

  6. Linux部署Apache Solr5.5.2+Apache Zookeeper3.4.6

    一.官网下载所需包. solr-5.5.2.tgz 下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/5.5.2/ zookee ...

  7. ODBC简介

    加载驱动 1 oracle Class.forName("oracle.JDBC.driver.OracleDriver") 2 DB2 Class.forName("c ...

  8. JS子父窗口互相操作取值赋值的方法介绍

    $("#父窗口元素ID",window.parent.document); 对应javascript版本为window.parent.document.getElementById ...

  9. Start with connect by prior 递归查询

    在SELECT命令中使用CONNECT BY和START WITH子句可以查询表中的树型结构关系.其命令格式如下: SELECT * from CONNECT BY {PRIOR列名1=列名2|列名1 ...

  10. EXCLE使用中常用函数和公式

    1.查找重复项 IF(COUNTIF(A:A,A1)>1"重复""") 或者 IF(COUNTIF($A$1:$A$100,A1)=1,"&qu ...