殇帝刘玢你 发表于 2015-11-12 13:24:06

SPRING DATA

前段时间由于业务需要,研究了下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代码


[*]<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>

<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代码


[*]<bean id=&quot;jedisConnectionFactory&quot; class=&quot;org.springframework.data.redis.connection.jedis.JedisConnectionFactory&quot;>
[*]      <property name=&quot;hostName&quot; value=&quot;localhost&quot;/>
[*]      <property name=&quot;port&quot; value=&quot;6636&quot;/>
[*]</bean>
[*]
[*]<bean id=&quot;redisTemplate&quot; class=&quot;org.springframework.data.redis.core.RedisTemplate&quot;>
[*]      <property name=&quot;connectionFactory&quot; ref=&quot;jedisConnectionFactory&quot;/>
[*]</bean>

<bean id=&quot;jedisConnectionFactory&quot; class=&quot;org.springframework.data.redis.connection.jedis.JedisConnectionFactory&quot;>
<property name=&quot;hostName&quot; value=&quot;localhost&quot;/>
<property name=&quot;port&quot; value=&quot;6636&quot;/>
</bean>
<bean id=&quot;redisTemplate&quot; class=&quot;org.springframework.data.redis.core.RedisTemplate&quot;>
<property name=&quot;connectionFactory&quot; ref=&quot;jedisConnectionFactory&quot;/>
</bean>
    那就可以直接用redisTemplate,redisTemplate和HibernateTemplate很相似。


Java代码


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

@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做了下简单了封装,再加上自己的连接池实现。
转载自:http://alexchan.iyunv.com/blog/1485321
页: [1]
查看完整版本: SPRING DATA