准備工作:
jedis連接
添加jar支持:
<!-- redis依賴 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
如果redis服務想被外部鏈接訪問,需要修改redis.conf配置 (69行)
69 行上面bind 相應本機的IP
136 行 daemonize yes
啟動服務:
./bin/redis-server bin/redis.conf
命令測試:
./bin/redis-cli -h 192.168.182.20
編碼測試:
Jedis jedis=new Jedis("192.168.23.111",6379);
//jedis.ping();
JedisPool jedisPool=new JedisPool("192.168.23.111",6379);
Jedis resource = jedisPool.getResource();
單機版
1,在web.xml里面添加
配置多個spring配置文件的路徑
2,添加spring-redis.xml
<!-- 開啟掃描 --> <context:component-scan base-package="com.duguangming.util"></context:component-scan> <!-- 初始化Jedis連接池--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大連接數, 默認8個--> <property name="maxTotal" value="50" /> <!--最大空閑連接數, 默認8--> <property name="maxIdle" value="10" /> <!--連接時的最大等待毫秒數--> <property name="maxWaitMillis" value="1000" /> <!--獲得一個jedis實例的時候是否檢查連接可用性--> <property name="testOnBorrow" value="true" /> </bean> <!-- 把jedisPool交給spring管理 --> <bean class="redis.clients.jedis.JedisPool" > <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> <constructor-arg name="host" value="192.168.239.20"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> </bean>
3,編寫JedisUtil及實現類,序列化工具
public interface JedisUtil { /** * 放入緩存 * @param key * @param value */ void putObject(Object key, Object value); /** * 清除緩存 * @param arg0 * @return */ Object removeObject(Object arg0); /** * 從緩存中獲取 * @param arg0 * @return */ Object getObject(Object arg0); }
@Component public class JedisUtilImpl implements JedisUtil{ @Autowired private JedisPool jedisPool; //放入緩存 public void putObject(Object key, Object value) { // TODO Auto-generated method stub Jedis resource = jedisPool.getResource(); resource.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value)); resource.close(); } //清楚緩存 public Object removeObject(Object arg0) { // TODO Auto-generated method stub Jedis resource = jedisPool.getResource(); Object expire = resource.expire( SerializeUtil.serialize(arg0.toString()), 0); resource.close(); return expire; } //從緩存中取 public Object getObject(Object arg0) { // TODO Auto-generated method stub Jedis resource = jedisPool.getResource(); Object value = SerializeUtil.unserialize(resource.get( SerializeUtil.serialize(arg0.toString()))); resource.close(); return value; } }
序列化工具:
public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } public static Object unserialize(byte[] bytes) { if (bytes == null) return null; ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } }
4,在服務層注入接口測試緩存功能
service層實現類具體代碼:
public List<Class> listAll() { List<Class> classListByParam = null; //從緩存中獲取班級列表 Object classList = jedisUtil.getObject("ddd"); //判斷緩存中是否存在 if(classList!=null){//不空,強轉返回 System.out.println("redis緩存中存在,直接返回"); classListByParam= (List<Class>)SerializeUtil.unserialize((byte[])classList); }else{ System.out.println("redis緩存中不存在,從數據庫中取出,並且放入緩存"); //查詢數據庫,取出 classListByParam = classDAO.listAll(); //放入redis緩存 jedisUtil.putObject("ddd", SerializeUtil.serialize(classListByParam)); } return classListByParam; }
redis集群版(實體類需要實現序列化接口)
注意:在項目中使用集群( 要求jedis2.8以上版本,更換pom版本)
ssm項目中,spring-redis-cluster.xml主配置文件的配置
<!--掃描包--> <context:component-scan base-package="com.duguangming.util"></context:component-scan> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <!--配置集群任意一台節點就可以--> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.20"/> <constructor-arg name="port" value="7001"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.20"/> <constructor-arg name="port" value="7002"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.20"/> <constructor-arg name="port" value="7003"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.21"/> <constructor-arg name="port" value="7004"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.21"/> <constructor-arg name="port" value="7005"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.21"/> <constructor-arg name="port" value="7006"/> </bean> </set> </constructor-arg> </bean> </beans>
ssm項目中,web.xml的路徑配置
ssm項目中,JedisUtilImpl實現類的
@Component public class JedisUtilImpl implements JedisUtil{ @Autowired private JedisCluster jedisCluster; public void putObject(Object key, Object value) { jedisCluster.set(SerializeUtil.serialize(key),SerializeUtil.serialize(value)); } public Object removeObject(Object arg0) { return jedisCluster.expire(SerializeUtil.serialize(arg0),1); } public Object getObject(Object arg0) { byte[] bytes = jedisCluster.get(SerializeUtil.serialize(arg0)); return SerializeUtil.unserialize(bytes); } }