不这里只是个人的redis使用笔记,对redis就不多做介绍了,直接开启传送门
菜鸟教程:http://www.runoob.com/redis/redis-install.html
按照提示就可以安装并启动成功,由于windows版本没有后台启动设置,所以
这个最好还是不要关掉。
那么既然redis安装成功了,接下来就干正事。首先包不能少:
jedis.2.4.1jar : http://download.csdn.net/download/viena/8143821
commons-pool2-2.0.jar : http://download.csdn.net/download/f765961322/7357681
spring-data-redis1.3.4.RELEASE.jar :
http://mvnrepository.com/artifact/org.springframework.data/spring-data-redis/1.3.4.RELEASE
以上传送若失效,请另寻资源.
接下来在java中配置:
1.配置文件:properties(当然也可以写在xml文件中,但单独配置以后方便修改)
#与redis.windows.conf中bind对应的ip,默认为127.0.0.1
redis.host=127.0.0.1
#与redis.windows.conf中port对应的端口,默认为6379
redis.port=6379
redis.pool.maxTotal=300
redis.pool.maxIdle=20
redis.pool.minIdle=5
redis.pool.testOnBorrow=true
redis.timeout=15000
redis.usePool=true
2.spring-redis.xml
<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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="minIdle" value="${redis.pool.minIdle}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
bean>
<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="timeout" value="${redis.timeout}" />
<property name="usePool" value="${redis.usePool}" />
<property name="poolConfig" ref="jedisPoolConfig" />
bean>
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
bean>
beans>
3.spring的配置文件中,加入以下代码(redis.properties放在工作目录下)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:redis.propertiesvalue>
property>
<property name="fileEncoding" value="utf-8" />
bean>
<import resource="spring-redis.xml"/>
4.redis操作工具类(百度有很多,如果不能满足要求可以另外找)
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
@Service
public class RedisCacheUtil<T>
{
@Autowired @Qualifier("redisTemplate")
public RedisTemplate redisTemplate;
/**
* 缓存基本的对象,Integer、String、实体类等
* @param key 缓存的键值
* @param value 缓存的值
* @return 缓存的对象
*/
public ValueOperations setCacheObject(String key,T value)
{
ValueOperations operation = redisTemplate.opsForValue();
operation.set(key,value);
return operation;
}
/**
* 获得缓存的基本对象。
* @param key 缓存键值
* @param operation
* @return 缓存键值对应的数据
*/
public T getCacheObject(String key/*,ValueOperations operation*/ )
{
ValueOperations operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* 缓存List数据
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 缓存的对象
*/
public ListOperations setCacheList(String key,List dataList)
{
ListOperations listOperation = redisTemplate.opsForList();
if(null != dataList)
{
int size = dataList.size();
for(int i = 0; i < size ; i ++)
{
listOperation.rightPush(key,dataList.get(i));
}
}
return listOperation;
}
/**
* 获得缓存的list对象
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
public List getCacheList(String key)
{
List dataList = new ArrayList();
ListOperations listOperation = redisTemplate.opsForList();
Long size = listOperation.size(key);
for(int i = 0 ; i < size ; i ++)
{
dataList.add((T) listOperation.leftPop(key));
}
return dataList;
}
/**
* 缓存Set
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public BoundSetOperations setCacheSet(String key,Set dataSet)
{
BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
/*T[] t = (T[]) dataSet.toArray();
setOperation.add(t);*/
Iterator it = dataSet.iterator();
while(it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
/**
* 获得缓存的set
* @param key
* @param operation
* @return
*/
public Set getCacheSet(String key/*,BoundSetOperations operation*/ )
{
Set dataSet = new HashSet();
BoundSetOperations operation = redisTemplate.boundSetOps(key);
Long size = operation.size();
for(int i = 0 ; i < size ; i++)
{
dataSet.add(operation.pop());
}
return dataSet;
}
/**
* 缓存Map
* @param key
* @param dataMap
* @return
*/
public HashOperations setCacheMap(String key,Map dataMap)
{
HashOperations hashOperations = redisTemplate.opsForHash();
if(null != dataMap)
{
for (Map.Entry entry : dataMap.entrySet()) {
/*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
hashOperations.put(key,entry.getKey(),entry.getValue());
}
}
return hashOperations;
}
/**
* 获得缓存的Map
* @param key
* @param hashOperation
* @return
*/
public Map getCacheMap(String key/*,HashOperations hashOperation*/ )
{
Map map = redisTemplate.opsForHash().entries(key);
/*Map map = hashOperation.entries(key);*/
return map;
}
/**
* 缓存Map
* @param key
* @param dataMap
* @return
*/
public HashOperations setCacheIntegerMap(String key,Map dataMap)
{
HashOperations hashOperations = redisTemplate.opsForHash();
if(null != dataMap)
{
for (Map.Entry entry : dataMap.entrySet()) {
/*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
hashOperations.put(key,entry.getKey(),entry.getValue());
}
}
return hashOperations;
}
/**
* 获得缓存的Map
* @param key
* @param hashOperation
* @return
*/
public Map getCacheIntegerMap(String key/*,HashOperations hashOperation*/ )
{
Map map = redisTemplate.opsForHash().entries(key);
/*Map map = hashOperation.entries(key);*/
return map;
}
}
5.简单的测试写入及获取缓存
@Autowired
public RedisUtil
通过redis缓存虽然没有加快数据库的查询速度,但是减少了数据库的访问,一定程度上解决了并发问题!