redis的官网(http://www.redis.io/)

      

     编辑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>  
<repository>
 <id>spring-milestone</id>
 <name>Spring Maven MILESTONE Repository</name>
 <url>http://maven.springframework.org/milestone</url>
</repository>

<dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-redis</artifactId>
 <version>1.0.0.RC1</version>
</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>  
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6636"/>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
</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. }  

@Service
public class RedisService {
    @Resource
    private RedisTemplate<Serializable, Serializable> template;

 /**
     * 向redis里面添加key-value格式的数据
     *
     * @param key   key
     * @param value value
     */

    public void set(final Serializable key, final Serializable value) {
        template.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key_ = RedisUtil.getBytesFromObject(key);
                byte[] value_ = RedisUtil.getBytesFromObject(value);
                connection.set(key_, value_);
                return true;
            }
        });
    }

 /**
     * 根据key从redis里面取出value
     *
     * @param key   key
     */
 public Serializable get(final Serializable key) {
        return template.execute(new RedisCallback<Serializable>() {
            @Override
            public Serializable doInRedis(RedisConnection connection) throws DataAccessException {

                byte[] keyBytes = RedisUtil.getBytesFromObject(key);
                byte[] bytes = connection.get(keyBytes);
                return (Serializable) RedisUtil.getObjectFromBytes(bytes);
            }
        });
    }
}

   

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