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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章