`
layou
  • 浏览: 29970 次
文章分类
社区版块
存档分类
最新评论

spring整合redis缓存

阅读更多
如果对spring cache不了解,这边有一篇文章详细的介绍了
http://jinnianshilongnian.iteye.com/blog/2001040
改项目基于maven构建
相关依赖
		<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>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.13.RELEASE</version>
		</dependency>

		<!-- 日志记录依赖包,很多都依赖此包,像log4j,json-lib等等 -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging-api</artifactId>
			<version>1.1</version>
		</dependency>

		<!-- junit 测试包 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.13.RELEASE</version>
			<scope>test</scope>
		</dependency>

Spring相关配置
<?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:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/cache 
		http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
	default-lazy-init="true">

	<context:property-placeholder location="classpath:redis.properties" />
	
	<!-- 自动扫描包 -->
	<context:component-scan base-package="com.layou" />


	<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
	<cache:annotation-driven cache-manager="cacheManager" />

	<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
	<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
		<property name="caches">
			<set>
				<bean class="com.layou.RedisCache">
					<property name="redisTemplate" ref="redisTemplate" />
					<property name="name" value="default" />
				</bean>
			</set>
		</property>
	</bean>

	<!-- redis 相关配置 -->
	<bean id="poolConfig" 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}" />
	</bean>

	<bean id="connectionFactory"
		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="timeout" value="${redis.timeout}" />
		<property name="usePool" value="true" />
		<property name="poolConfig" ref="poolConfig" />
	</bean>

	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>
</beans>

redis相关配置
#redis.host=localhost
redis.host=192.168.71.145
redis.port=6379
redis.password=
#客户端超时时间单位是毫秒
redis.timeout=100000

#最大连接数
redis.pool.maxActive=500  
#最大能够保持idel状态的对象数  
redis.pool.maxIdle=200  
#最大建立连接等待时间  
redis.pool.maxWait=2000  
#当调用borrow Object方法时,是否进行有效性检查  
redis.pool.testOnBorrow=true  

首先,通过自己实现Cache接口来实现对redis的相关操作
package com.layou;

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() {
		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;
		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;
			}
		});
	}

	@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 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) {
			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;
	}
}

使用junit进行测试
package com.layou;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = {"classpath:applicationContext-cache-redis.xml"})
public class RedisTest extends AbstractJUnit4SpringContextTests {
	private RedisCache cache;
	@Autowired
	private CacheManager manager;
	
	@Before
	public void before() {
		cache = (RedisCache) manager.getCache("default");
	}
	
	@Test
	public void testAdd() throws Exception {
		cache.put("test", 1);
	}
	
	@Test
	public void testGet() throws Exception {
		System.out.println(cache.get("test").get());
	}
	
	@Test
	public void testEvict() throws Exception {
		cache.evict("test");
	}
	
	@Test
	public void testClear() throws Exception {
		cache.clear();
	}
}

在实际项目中,多是通过Spring的注解完成相关操作
package com.layou;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class RedisAnnotation {
	
	/**
	 * 第一次查询会到数据库中差寻,第二次就会从缓存中取
	 * #id代表使用参数id
	 * 一定要放在spring容器中管理,不然拦截不到
	 * @param id
	 * @return
	 */
	@Cacheable(key="'name' + #id", value="default")
	public String getNameById(int id) {
		//TODO 进行数据库查询并返回查询数据库内容
		System.out.println("查询数据库");
		return "" + id;
	}
	
	/**
	 * 从缓存中删除
	 * @param id
	 */
	@CacheEvict(key="'name' + #id", value="default")
	public void deleteById(int id) {
		System.out.println("数据库删除");
	}
}

使用junit进行相关测试
package com.layou;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = {"classpath:applicationContext-cache-redis.xml"})
public class RedisAnnotationTest extends AbstractJUnit4SpringContextTests {
	@Autowired
	private RedisAnnotation redis;
	
	@Test
	public void testQuery() {
		//第二次查询的时候就会从缓存中取
		System.out.println(redis.getNameById(1));
	}
	
	@Test
	public void testDelete() {
		redis.deleteById(1);
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics