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>  
[xml]  view plain copy
  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>  
[xml]  view plain copy
  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 plain copy
  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做了下简单了封装,再加上自己的连接池实现。

转载自:http://alexchan.iteye.com/blog/1485321

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值