SPRING DATA - REDIS配置

       前段时间由于业务需要,研究了下redis,第一次接触redis,没有详细的研究,只把它当着工具来用,以后有时间慢慢研究下,简单的看了下redis的官网(http://www.redis.io/),Commands Clients Documentation这3个方面看完已了解了大部分情况,看了java Clients,redis官网比较推荐Jedis,而spring对redis的客服端做了一个统一封装,支持(Jedis,  JRedis, and RJC),我只对SPRING DATA - REDIS研究了下,那就开始动手吧! 


      下载redis最新版本  2.4.10 下载地址:http://redis.googlecode.com/files/redis-2.4.10.tar.gz


      安装的详细过程就不在这写了,它现在只支持linux.

 

      然后创建项目......


      如果使用maven的话那就很简单了,直接加入依赖

 

     编辑pom.xml

Xml代码 复制代码 收藏代码
  1. <repository>  
  2.  <id>spring-milestone</id>  
  3.  <name>Spring Maven MILESTONE Repository</name>  
  4.  <url>http://maven.springframework.org/milestone</url>  
  5. </repository>  
  6.   
  7. <dependency>  
  8.  <groupId>org.springframework.data</groupId>  
  9.  <artifactId>spring-data-redis</artifactId>  
  10.  <version>1.0.0.RC1</version>  
  11. </dependency>  
  1. <repository>  
  2.  <id>spring-milestone</id>  
  3.  <name>Spring Maven MILESTONE Repository</name>  
  4.  <url>http://maven.springframework.org/milestone</url>  
  5. </repository>  
  6.   
  7. <dependency>  
  8.  <groupId>org.springframework.data</groupId>  
  9.  <artifactId>spring-data-redis</artifactId>  
  10.  <version>1.0.0.RC1</version>  
  11. </dependency>  

 

如果没有使用maven,那直接下载jar包http://s3.amazonaws.com/dist.springframework.org/release/DATAKV/spring-data-redis-1.0.0.RELEASE.zip

 

   在spring配置文件里添加

 

Xml代码 复制代码 收藏代码
  1. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  2.         <property name="hostName" value="localhost"/>  
  3.         <property name="port" value="6636"/>  
  4. </bean>  
  5.   
  6. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  7.         <property name="connectionFactory" ref="jedisConnectionFactory"/>  
  8. </bean>  
  1. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  2.         <property name="hostName" value="localhost"/>  
  3.         <property name="port" value="6636"/>  
  4. </bean>  
  5.   
  6. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  7.         <property name="connectionFactory" ref="jedisConnectionFactory"/>  
  8. </bean>  
 

    那就可以直接用redisTemplate,redisTemplate和HibernateTemplate很相似。

 

Java代码 复制代码 收藏代码
  1. @Service  
  2. public class RedisService {   
  3.     @Resource  
  4.     private RedisTemplate<Serializable, Serializable> template;   
  5.   
  6.  /**  
  7.      * 向redis里面添加key-value格式的数据  
  8.      *  
  9.      * @param key   key  
  10.      * @param value value  
  11.      */  
  12.   
  13.     public void set(final Serializable key, final Serializable value) {   
  14.         template.execute(new RedisCallback<Object>() {   
  15.             @Override  
  16.             public Object doInRedis(RedisConnection connection) throws DataAccessException {   
  17.                 byte[] key_ = RedisUtil.getBytesFromObject(key);   
  18.                 byte[] value_ = RedisUtil.getBytesFromObject(value);   
  19.                 connection.set(key_, value_);   
  20.                 return true;   
  21.             }   
  22.         });   
  23.     }   
  24.   
  25.  /**  
  26.      * 根据key从redis里面取出value  
  27.      *  
  28.      * @param key   key  
  29.      */  
  30.  public Serializable get(final Serializable key) {   
  31.         return template.execute(new RedisCallback<Serializable>() {   
  32.             @Override  
  33.             public Serializable doInRedis(RedisConnection connection) throws DataAccessException {   
  34.   
  35.                 byte[] keyBytes = RedisUtil.getBytesFromObject(key);   
  36.                 byte[] bytes = connection.get(keyBytes);   
  37.                 return (Serializable) RedisUtil.getObjectFromBytes(bytes);   
  38.             }   
  39.         });   
  40.     }   
  41. }  
[java] view plaincopy
  1. @Service  
  2. public class RedisService {  
  3.     @Resource  
  4.     private RedisTemplate<Serializable, Serializable> template;  
  5.   
  6.  /** 
  7.      * 向redis里面添加key-value格式的数据 
  8.      * 
  9.      * @param key   key 
  10.      * @param value value 
  11.      */  
  12.   
  13.     public void set(final Serializable key, final Serializable value) {  
  14.         template.execute(new RedisCallback<Object>() {  
  15.             @Override  
  16.             public Object doInRedis(RedisConnection connection) throws DataAccessException {  
  17.                 byte[] key_ = RedisUtil.getBytesFromObject(key);  
  18.                 byte[] value_ = RedisUtil.getBytesFromObject(value);  
  19.                 connection.set(key_, value_);  
  20.                 return true;  
  21.             }  
  22.         });  
  23.     }  
  24.   
  25.  /** 
  26.      * 根据key从redis里面取出value 
  27.      * 
  28.      * @param key   key 
  29.      */  
  30.  public Serializable get(final Serializable key) {  
  31.         return template.execute(new RedisCallback<Serializable>() {  
  32.             @Override  
  33.             public Serializable doInRedis(RedisConnection connection) throws DataAccessException {  
  34.   
  35.                 byte[] keyBytes = RedisUtil.getBytesFromObject(key);  
  36.                 byte[] bytes = connection.get(keyBytes);  
  37.                 return (Serializable) RedisUtil.getObjectFromBytes(bytes);  
  38.             }  
  39.         });  
  40.     }  
  41. }  
   

     看了一点JedisConnectionFactory,之际上它只是对Jedis做了下简单了封装,再加上自己的连接池实现。

发布了0 篇原创文章 · 获赞 2 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章