Spring3.1 Cache注解
依赖jar包:
- <!-- redis -->
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.3.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.5.2</version>
- </dependency>
applicationContext-cache-redis.xml
- <context:property-placeholder
- location="classpath:/config/properties/redis.properties" />
- <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
- <cache:annotation-driven cache-manager="cacheManager" />
- <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
- <property name="caches">
- <set>
- <bean class="org.cpframework.cache.redis.RedisCache">
- <property name="redisTemplate" ref="redisTemplate" />
- <property name="name" value="default"/>
- </bean>
- <bean class="org.cpframework.cache.redis.RedisCache">
- <property name="redisTemplate" ref="redisTemplate02" />
- <property name="name" value="commonCache"/>
- </bean>
- </set>
- </property>
- </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="connectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"
- p:database="${redis.database}" />
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="connectionFactory" />
- </bean>
- <bean id="connectionFactory02"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"
- p:database="${redis.database}" />
- <bean id="redisTemplate02" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="connectionFactory02" />
- </bean>
redis.properties
- # Redis settings
- # server IP
- redis.host=192.168.xx.xx
- # server port
- redis.port=6379
- # use dbIndex
- redis.database=0
- # 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
- redis.maxIdle=300
- # 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;
- redis.maxWait=3000
- # 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
- redis.testOnBorrow=true
RedisCache.java
- package org.cpframework.cache.redis;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import org.springframework.cache.Cache;
- import org.springframework.cache.support.SimpleValueWrapper;
- import org.springframework.dao.DataAccessException;
- import org.springframework.data.redis.connection.RedisConnection;
- import org.springframework.data.redis.core.RedisCallback;
- import org.springframework.data.redis.core.RedisTemplate;
- public class RedisCache implements Cache {
- private RedisTemplate<String, Object> redisTemplate;
- private String name;
- public RedisTemplate<String, Object> getRedisTemplate() {
- return redisTemplate;
- }
- public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String getName() {
- // TODO Auto-generated method stub
- return this.name;
- }
- @Override
- public Object getNativeCache() {
- // TODO Auto-generated method stub
- return this.redisTemplate;
- }
- @Override
- public ValueWrapper get(Object key) {
- // TODO Auto-generated method stub
- final String keyf = (String) key;
- Object object = null;
- object = redisTemplate.execute(new RedisCallback<Object>() {
- public Object doInRedis(RedisConnection connection)
- throws DataAccessException {
- byte[] key = keyf.getBytes();
- byte[] value = connection.get(key);
- if (value == null) {
- return null;
- }
- return toObject(value);
- }
- });
- return (object != null ? new SimpleValueWrapper(object) : null);
- }
- @Override
- public void put(Object key, Object value) {
- // TODO Auto-generated method stub
- final String keyf = (String) key;
- final Object valuef = value;
- final long liveTime = 86400;
- redisTemplate.execute(new RedisCallback<Long>() {
- public Long doInRedis(RedisConnection connection)
- throws DataAccessException {
- byte[] keyb = keyf.getBytes();
- byte[] valueb = toByteArray(valuef);
- connection.set(keyb, valueb);
- if (liveTime > 0) {
- connection.expire(keyb, liveTime);
- }
- return 1L;
- }
- });
- }
- /**
- * 描述 : <Object转byte[]>. <br>
- * <p>
- * <使用方法说明>
- * </p>
- *
- * @param obj
- * @return
- */
- private byte[] toByteArray(Object obj) {
- byte[] bytes = null;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try {
- ObjectOutputStream oos = new ObjectOutputStream(bos);
- oos.writeObject(obj);
- oos.flush();
- bytes = bos.toByteArray();
- oos.close();
- bos.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- return bytes;
- }
- /**
- * 描述 : <byte[]转Object>. <br>
- * <p>
- * <使用方法说明>
- * </p>
- *
- * @param bytes
- * @return
- */
- private Object toObject(byte[] bytes) {
- Object obj = null;
- try {
- ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
- ObjectInputStream ois = new ObjectInputStream(bis);
- obj = ois.readObject();
- ois.close();
- bis.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- return obj;
- }
- @Override
- public void evict(Object key) {
- // TODO Auto-generated method stub
- final String keyf = (String) key;
- redisTemplate.execute(new RedisCallback<Long>() {
- public Long doInRedis(RedisConnection connection)
- throws DataAccessException {
- return connection.del(keyf.getBytes());
- }
- });
- }
- @Override
- public void clear() {
- // TODO Auto-generated method stub
- redisTemplate.execute(new RedisCallback<String>() {
- public String doInRedis(RedisConnection connection)
- throws DataAccessException {
- connection.flushDb();
- return "ok";
- }
- });
- }
- }
Spring3.1 Cache注解的更多相关文章
- [转载] Spring3.1 Cache注解
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管理cache注解,好在3.1.M1中增加了对cache注解的支持, ...
- SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...
- 十二:SpringBoot-基于Cache注解模式,管理Redis缓存
SpringBoot-基于Cache注解模式,管理Redis缓存 1.Cache缓存简介 2.核心API说明 3.SpringBoot整合Cache 3.1 核心依赖 3.2 Cache缓存配置 3. ...
- Spring3.2新注解@ControllerAdvice
Spring3.2新注解@ControllerAdvice @ControllerAdvice,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@Control ...
- springboot整合redis-sentinel支持Cache注解
一.前提 已经存在一个redis-sentinel集群,两个哨兵分别如下: /home/redis-sentinel-cluster/sentinel-1.conf port 26379 dir &q ...
- Spring的Cache注解
Spring的Cache注解如下所示: @CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置.在这里@CacheConfig(cacheNames = "users&quo ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解
随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...
- spring源码分析之cache注解
Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache),而是一个对缓存使用的抽象 ...
随机推荐
- Enum:Hopscotch(POJ 3050)
跳格子 题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串? 题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可 ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- angularjs 指令(directive)详解(1)
原文地址 什么是directive?我们先来看一下官方的解释: At a high level, directives are markers on a DOM element (such as an ...
- Catch That Cow(poj 3278)
给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 //广搜,a是队列,step记录步数,vis记录哪些数被搜到过 #include<cstdio& ...
- 【Chrome】手动下载和安装Adblock Plus的方法
由于强大的GFW,导致很多Google的站点没法访问,也就没法直接安装chrome的插件了,网页都打不卡, 那么几乎是必备的Adblock Plus如何下载安装呢? 1.访问官方网站: https:/ ...
- Ubuntu 15.10系统安装后要做的15件事
Ubuntu 15.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)
Problem A: The 3n + 1 problem Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 14 Solved: 6[Submit][St ...
- [杂]DeadLock, Isolation Level, EntityFramework
由于没有注意到EF事务的默认隔离级别是Serializable,(据说EF6.0以后默认隔离级别改成了Read_Commit_Snapshot)--这里有误,应该是加了TransactionScope ...
- HBase伪分布式环境下,HBase的API操作,遇到的问题
在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示 Exception in thread "main&q ...
- Xamarin.Android提示aapt退出,代码为255
Xamarin.Android提示aapt退出,代码为255 错误信息:”aapt.exe”已退出,代码为255.出现这种问题,通常是由于该项目所使用Android SDK不完整.通过SDK Mana ...