Spring Cache集成Redis-演道网

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错
redis大家都耳熟能详了,spring context默认没有实现redis的cache相关接口,我们需要自己实现,并借助org.springframework.cache.support.SimpleCacheManager进行redis缓存管理。

添加jar包:

   redis.clients
   jedis
   2.5.2
  cache接口实现:

package org.springframework.cache.demo.redis;

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 redisTemplate;
 private String name;

 public RedisTemplate getRedisTemplate() {
  return redisTemplate;
 }

 public void setRedisTemplate(RedisTemplate 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() {
   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() {
   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;
   }
  });
 }

 /**
  * 描述 : .

  *
  *
  *

  *
  * @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;
 }

 /**
  * 描述 : .

  *

  *
  *

  *
  * @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;
 }

 @Override
 public void evict(Object key) {
  final String keyf = (String) key;
  redisTemplate.execute(new RedisCallback() {
   public Long doInRedis(RedisConnection connection) throws DataAccessException {
    return connection.del(keyf.getBytes());
   }
  });
 }

 @Override
 public void clear() {
  redisTemplate.execute(new RedisCallback() {
   public String doInRedis(RedisConnection connection) throws DataAccessException {
    connection.flushDb();
    return “ok”;
   }
  });
 }

 @SuppressWarnings(“unchecked”)
 @Override
 public T get(Object key, Class type) {
  final String keyf = (String) key;
  Object object = null;
  object = redisTemplate.execute(new RedisCallback() {
   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 (T) object;
 }

 @Override
 public ValueWrapper putIfAbsent(Object key, Object value) {
  put(key, value);
  return new SimpleValueWrapper(value);
 }
}
spring配置:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:cache=”http://www.springframework.org/schema/cache”
 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”>

 
 

 
 
     
     
         
             
                 
                     
                     
                 
             
         
   
   
     
     
               
         
         
     
    <bean id="connectionFactory" 
        class=”org.springframework.data.redis.connection.jedis.JedisConnectionFactory” 
        p:host-name=”192.168.1.90″ p:port=”6379″ p:pool-config-ref=”poolConfig” 
        p:database=”0″ />
     
         
     

除了以上方式,在spring-data项目中增加了对redis的支持spring-data-redis,单独引入依赖:

   org.springframework.data
   spring-data-redis
   1.4.2.RELEASE
  使用spring-data-redis提供的RedisTemplate对redis操作,spring配置如下:

<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:c=’http://www.springframework.org/schema/c’
 xmlns:p=’http://www.springframework.org/schema/p’
 xmlns:cache=’http://www.springframework.org/schema/cache’
 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
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        ‘>

 
 

 

 
 <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
  c:template-ref=”redisTemplate” />

 
 
 
 
 
 
 <bean id="jedisConnectionFactory"
  class=”org.springframework.data.redis.connection.jedis.JedisConnectionFactory”
  p:host-name=”192.168.1.90″ p:port=”6379″ p:pool-config-ref=”jedisPoolConfig”
  p:database=”0″ p:use-pool=”true” />

 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
  p:connection-factory-ref=”jedisConnectionFactory” />

下面关于Redis的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis系列-安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3安装Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis安装部署学习笔记 http://www.linuxidc.com/Linux/2014-07/104306.htm

Redis配置文件redis.conf 详解 http://www.linuxidc.com/Linux/2013-11/92524.htm

Redis 的详细介绍请点这里
Redis 的下载地址请点这里

转载自演道,想查看更及时的互联网产品技术热点文章请点击http://go2live.cn

未经允许不得转载:演道网 » Spring Cache集成Redis-演道网

赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册