Spring与Redis的整合
来源:互联网 发布:怎么查看当前linux版本 编辑:程序博客网 时间:2024/05/21 08:48
第一步:引入jar包
需要引入和Redis 相关的两个jar包,一个是spring-data-redis.jar,另一个是jedis.jar。
具体的jar包在maven中心库里面都能找到。
maven项目pom.xml的配置:
- <!-- 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>
第二步:新建RedisCache.class类,自定义Cache。
- package com.qcjy.common.cache;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import org.apache.log4j.Logger;
- 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;
- /**
- * 自定义cache,可以定义一个cache名称和一个缓存失效时间。时间到期会自动更新。在xml中配置。
- * <功能详细描述>
- *
- * @author lcma
- * @version [版本号, 2016年9月12日]
- * @see [相关类/方法]
- * @since [产品/模块版本]
- */
- public class RedisCache implements Cache {
- /**
- * LOG日志
- */
- private static final Logger LOGGER = Logger.getLogger(RedisCache.class);
- private RedisTemplate<String, Object> redisTemplate;
- /**
- * 缓存名称
- */
- private String name;
- /**
- * 缓存失效时间,时间到了会自动更新
- */
- private long liveTime = 0;
- 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() {
- return this.name;
- }
- @Override
- public Object getNativeCache() {
- return this.redisTemplate;
- }
- /**
- * 获取
- */
- @Override
- public ValueWrapper get(Object key) {
- 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) {
- final String keyf = (String)key;
- final Object valuef = value;
- 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 (getLiveTime() > 0) {
- connection.expire(keyb, getLiveTime());
- }
- return 1L;
- }
- });
- }
- /**
- * 删除
- */
- @Override
- public void clear() {
- redisTemplate.execute(new RedisCallback<String>() {
- public String doInRedis(RedisConnection connection)
- throws DataAccessException {
- connection.flushDb();
- return "ok";
- }
- });
- }
- /**
- * 描述 : <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) {
- LOGGER.error(ex.getMessage(),ex);
- }
- 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) {
- LOGGER.error(ex.getMessage(),ex);
- } catch (ClassNotFoundException ex) {
- LOGGER.error(ex.getMessage(),ex);
- }
- return obj;
- }
- @Override
- public void evict(Object key) {
- final String keyf = (String)key;
- redisTemplate.execute(new RedisCallback<Long>() {
- public Long doInRedis(RedisConnection connection)
- throws DataAccessException {
- return connection.del(keyf.getBytes());
- }
- });
- }
- @Override
- public <T> T get(Object key, Class<T> type) {
- return null;
- }
- @Override
- public ValueWrapper putIfAbsent(Object key, Object value) {
- return null;
- }
- public long getLiveTime() {
- return liveTime;
- }
- public void setLiveTime(long liveTime) {
- this.liveTime = liveTime;
- }
- }
第三步:新建spring-redis.xml,配置Redis相关信息。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:cache="http://www.springframework.org/schema/cache" xmlns:c="http://www.springframework.org/schema/c"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- 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">
- <!-- 开启缓存注解 -->
- <cache:annotation-driven cache-manager="cacheManager" />
- <!-- jedis客户端连接工厂 -->
- <bean id="jedisConnectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="127.0.0.1" p:port="6379" p:password="123456" />
- <!-- redisTemplate模板 -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
- p:connection-factory-ref="jedisConnectionFactory" />
- <!-- spring自己的管理器,这里定义了三个缓存位置名称 ,既注解中的value -->
- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
- <property name="caches">
- <set>
- <bean class="com.qcjy.common.cache.RedisCache"> <!-- 短期缓存 1个小时-->
- <property name="redisTemplate" ref="redisTemplate" />
- <property name="name" value="shortTimeCache" />
- <property name="liveTime" value="3600" />
- </bean>
- <bean class="com.qcjy.common.cache.RedisCache"> <!-- 长期缓存 4个小时-->
- <property name="redisTemplate" ref="redisTemplate" />
- <property name="name" value="longTimeCache" />
- <property name="liveTime" value="14400" />
- </bean>
- <bean class="com.qcjy.common.cache.RedisCache"> <!-- 永久缓存 -->
- <property name="redisTemplate" ref="redisTemplate" />
- <property name="name" value="nerverTimeCache" />
- <property name="liveTime" value="0" />
- </bean>
- </set>
- </property>
- </bean>
- </beans>
com.qcjy.common.cache.RedisCache 是我的项目自定义cache的类路径,可根据自己项目类路径变换。
- <!-- 集成Redis缓存框架 -->
- <import resource="spring-redis.xml" />
第五步:在方法上面添加注解
- /**
- * getProject:根据id获取数据. <br/>
- * @author lcma
- * @param id
- * @return
- * @since JDK 1.7
- */
- @Override
- @Cacheable(value = "shortTimeCache", key = "'getProject'+#id")
- public Project getProject(String id) {
- return projectDao.getById(id);
- }
阅读全文
0 0
- Spring与Redis的整合
- Redis与Spring整合
- redis与spring整合
- Redis与Spring整合
- Redis与Spring整合
- Spring与Redis整合
- Spring与Redis整合
- spring的cacheManager与redis的整合
- redis与Spring Cache的整合
- Redis与spring的整合遇到问题
- Redis集群与spring的整合
- spring与redis整合,redisTemplate的使用
- 简单的redis与spring整合
- redis与java spring的整合使用
- Redis的集群搭建与Redis与spring整合
- Spring+redis的整合
- spring与spring-data-redis整合redis
- redis与spring aop整合
- ADB命令汇总,比如adb devices之类的
- Oracle知识原理详解
- [知了堂学习笔记]_JavaScript之typeof运算符
- DBUtils
- map_set
- Spring与Redis的整合
- Android实现异步的几种方法
- 3-6 静态数据成员与静态成员函数
- VS2010出现重复的代码项原因及解决方式
- 便利宝商家APP官方版V2.3.0下载 | 银盒子便利宝商家版下载
- Logrotate日志切割报错 文件不再同一个用户组下
- [知了堂学习笔记] javascript 内置对象
- Kubernetes学习之 Hadoop cluster in Kubernetes
- JSP-动作指令