目录
- redis安装
- spring集成redis
- springmvc中使用redis
- 写进和读取redis
redis安装
首先安装redis。这个就不重点介绍了。windos下载redis就行。
我用的是mac
用命令行安装的。
安装命令
yum install redis
运行命令
sudo redis-server
这样就安装运行成功了。
spring集成redis
首先你需要下载驱动包,下载 jedis.jar,确保下载最新驱动包。然后导包。
在spring配置文件里我这是applicationcontext .xml文件添加
<!-- 引入同文件夹下的redis属性配置文件 --> <import resource="spring-redis.xml" />
然后用创建spring-redis.xml文件
写入
<?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"
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">
<!-- 缓存的层级-->
<context:component-scan base-package="com.niit.cache" />
<!-- 引入redis配置 -->
<context:property-placeholder location="/web-inf/classes/redis.properties" ignore-unresolvable="true"/>
<!-- redis 配置 -->
<bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
<property name="maxtotal" value="${redis.pool.maxtotal}" />
<property name="maxidle" value="${redis.pool.maxidle}" />
<property name="maxwaitmillis" value="${redis.pool.maxwaitmillis}" />
<property name="testonborrow" value="${redis.pool.testonborrow}" />
</bean>
<!-- jediscluster 集群高可用配置 -->
<!--<bean id="jediscluster" class="redis.clients.jedis.jediscluster">
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg index="0" value="${redis.ip1}" />
<constructor-arg index="1" value="${redis.port1}" type="int" />
</bean>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg index="0" value="${redis.ip2}" />
<constructor-arg index="1" value="${redis.port2}" type="int" />
</bean>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg index="0" value="${redis.ip3}" />
<constructor-arg index="1" value="${redis.port3}" type="int" />
</bean>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg index="0" value="${redis.ip4}" />
<constructor-arg index="1" value="${redis.port4}" type="int" />
</bean>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg index="0" value="${redis.ip5}" />
<constructor-arg index="1" value="${redis.port5}" type="int" />
</bean>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg index="0" value="${redis.ip6}" />
<constructor-arg index="1" value="${redis.port6}" type="int" />
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" value="2000" type="int"></constructor-arg>
<constructor-arg index="2" value="100" type="int"></constructor-arg>
<constructor-arg index="3" ref="jedispoolconfig"></constructor-arg>
</bean>-->
<!--redis sentinel主从高可用方案配置 -->
<!-- <bean id="sentinelconfiguration" class="org.springframework.data.redis.connection.redissentinelconfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.redisnode">
<property name="name" value="master-1"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.redisnode">
<constructor-arg name="host" value="${sentinel1.ip}"></constructor-arg>
<constructor-arg name="port" value="${sentinel1.port}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redisnode">
<constructor-arg name="host" value="${sentinel2.ip}"></constructor-arg>
<constructor-arg name="port" value="${sentinel2.port}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redisnode">
<constructor-arg name="host" value="${sentinel3.ip}"></constructor-arg>
<constructor-arg name="port" value="${sentinel3.port}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisconnectionfactory" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory" p:use-pool="true">
<property name="password" value="${redis.pass}" />
<property name="poolconfig">
<ref bean="jedispoolconfig" />
</property>
<constructor-arg name="sentinelconfig" ref="sentinelconfiguration" />
</bean> -->
<!-- redis单节点数据库连接配置 -->
<bean id="jedisconnectionfactory" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory">
<property name="hostname" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pass}" />
<property name="poolconfig" ref="jedispoolconfig" />
</bean>
<!-- redistemplate配置,redistemplate是对jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redistemplate" class="org.springframework.data.redis.core.stringredistemplate">
<property name="connectionfactory" ref="jedisconnectionfactory" />
</bean>
</beans>
public class rediscache {
public final static string cahcename = "niitcache";// 缓存名
public final static int cahcetime = 60;// 默认缓存时间 60s
public final static int cahcehour = 60 * 60;// 默认缓存时间 1hr
public final static int cahceday = 60 * 60 * 24;// 默认缓存时间 1day
public final static int cahceweek = 60 * 60 * 24 * 7;// 默认缓存时间 1week
public final static int cahcemonth = 60 * 60 * 24 * 7 * 30;// 默认缓存时间 1month
@autowired
private redistemplate<string, string> redistemplate;
public <t> boolean putcache(string key, t obj) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serialize(obj);
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
return connection.setnx(bkey, bvalue);
}
});
return result;
}
public <t> void putcachewithexpiretime(string key, t obj, final long expiretime) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serialize(obj);
redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
connection.setex(bkey, expiretime, bvalue);
return true;
}
});
}
public <t> boolean putlistcache(string key, list<t> objlist) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serializelist(objlist);
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
return connection.setnx(bkey, bvalue);
}
});
return result;
}
public <t> boolean putlistcachewithexpiretime(string key, list<t> objlist, final long expiretime) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serializelist(objlist);
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
connection.setex(bkey, expiretime, bvalue);
return true;
}
});
return result;
}
public <t> t getcache(final string key, class<t> targetclass) {
byte[] result = redistemplate.execute(new rediscallback<byte[]>() {
@override
public byte[] doinredis(redisconnection connection) throws dataaccessexception {
return connection.get(key.getbytes());
}
});
if (result == null) {
return null;
}
return protostuffserializerutil.deserialize(result, targetclass);
}
public <t> list<t> getlistcache(final string key, class<t> targetclass) {
byte[] result = redistemplate.execute(new rediscallback<byte[]>() {
@override
public byte[] doinredis(redisconnection connection) throws dataaccessexception {
return connection.get(key.getbytes());
}
});
if (result == null) {
return null;
}
return protostuffserializerutil.deserializelist(result, targetclass);
}
/**
* 精确删除key
*
* @param key
*/
public void deletecache(string key) {
redistemplate.delete(key);
}
/**
* 模糊删除key
*
* @param pattern
*/
public void deletecachewithpattern(string pattern) {
set<string> keys = redistemplate.keys(pattern);
redistemplate.delete(keys);
}
/**
* 清空所有缓存
*/
public void clearcache() {
deletecachewithpattern(rediscache.cahcename + "|*");
}
}
创建redis的配置文件 redis.properties。
写入
#redis config redis.pass= redis.pool.maxtotal=105 redis.pool.maxidle=10 redis.pool.maxwaitmillis=5000 redis.pool.testonborrow=true redis.ip=127.0.0.1 redis.port=6379
这些根据自己的需求自定义配置就好了
这样redis就继承好了
springmvc中使用redis
创建一个rediscache类
import org.springframework.beans.factory.annotation.autowired;
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;
import org.springframework.stereotype.component;
import com.niit.util.protostuffserializerutil;
import java.util.list;
import java.util.set;
/**
* redis缓存
*
* @author james
*
*/
@component
public class rediscache {
public final static string cahcename = "niitcache";// 缓存名
public final static int cahcetime = 60;// 默认缓存时间 60s
public final static int cahcehour = 60 * 60;// 默认缓存时间 1hr
public final static int cahceday = 60 * 60 * 24;// 默认缓存时间 1day
public final static int cahceweek = 60 * 60 * 24 * 7;// 默认缓存时间 1week
public final static int cahcemonth = 60 * 60 * 24 * 7 * 30;// 默认缓存时间 1month
@autowired
private redistemplate<string, string> redistemplate;
public <t> boolean putcache(string key, t obj) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serialize(obj);
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
return connection.setnx(bkey, bvalue);
}
});
return result;
}
public <t> void putcachewithexpiretime(string key, t obj, final long expiretime) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serialize(obj);
redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
connection.setex(bkey, expiretime, bvalue);
return true;
}
});
}
public <t> boolean putlistcache(string key, list<t> objlist) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serializelist(objlist);
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
return connection.setnx(bkey, bvalue);
}
});
return result;
}
public <t> boolean putlistcachewithexpiretime(string key, list<t> objlist, final long expiretime) {
final byte[] bkey = key.getbytes();
final byte[] bvalue = protostuffserializerutil.serializelist(objlist);
boolean result = redistemplate.execute(new rediscallback<boolean>() {
@override
public boolean doinredis(redisconnection connection) throws dataaccessexception {
connection.setex(bkey, expiretime, bvalue);
return true;
}
});
return result;
}
public <t> t getcache(final string key, class<t> targetclass) {
byte[] result = redistemplate.execute(new rediscallback<byte[]>() {
@override
public byte[] doinredis(redisconnection connection) throws dataaccessexception {
return connection.get(key.getbytes());
}
});
if (result == null) {
return null;
}
return protostuffserializerutil.deserialize(result, targetclass);
}
public <t> list<t> getlistcache(final string key, class<t> targetclass) {
byte[] result = redistemplate.execute(new rediscallback<byte[]>() {
@override
public byte[] doinredis(redisconnection connection) throws dataaccessexception {
return connection.get(key.getbytes());
}
});
if (result == null) {
return null;
}
return protostuffserializerutil.deserializelist(result, targetclass);
}
/**
* 精确删除key
*
* @param key
*/
public void deletecache(string key) {
redistemplate.delete(key);
}
/**
* 模糊删除key
*
* @param pattern
*/
public void deletecachewithpattern(string pattern) {
set<string> keys = redistemplate.keys(pattern);
redistemplate.delete(keys);
}
/**
* 清空所有缓存
*/
public void clearcache() {
deletecachewithpattern(rediscache.cahcename + "|*");
}
}
写进和读取redis
<span style="white-space:pre"> </span>string v = "test";
cache.putcachewithexpiretime("key", v, cache.cahcehour);
string value = cache.getcache("key", string.class);
system.out.println(value);
然后集成成功。redis是将数据放进内存里。所以需要考虑做redis服务器时候的内存性能,还有redis的缓存策略等等。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。