使用Spring Data Redis操作Redis(单机版)

说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点。并且会与一些低版本的Spring有冲突,要看官方文档和不断的测试。

Jedis是一款Java连接Redis的客户端,Spring基于Jedis进行了封装,提供了简洁的操作Redis的方法。那就是Spring Data Redis。其实后来研究发现,Spring Data Redis集成不止Jedits这一款,还有很多款,这些都可以通过注入连接工厂来去指定。

要使用Spring Data Redis需要做如下步骤的操作思路:

1、先建立连接工厂,这个连接工厂是用来设置IP,端口,账号密码等。

2、通过连接工厂建立Session。

3、然后在代码上注入Session进行使用。

实现步骤:

1、POM

        <!-- Redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.2</version>
        </dependency>
这个版本是我在Spring Boot项目引入包得到的灵感,用1.4.7的版本,然后它就引入了上面的包,并且MVC版本为4.3.9。

2、JedisConnectionFactory建立Redis连接工厂

类似于数据库连接池一样,Redis客户端也建立一个连接工厂

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
@Bean  
public JedisConnectionFactory jedisConnectionFactory() {  
        JedisConnectionFactory connFactory = new JedisConnectionFactory();  
  
        connFactory.setHostName("127.0.0.1");  
        connFactory.setPort(6379);  
        connFactory.setUsePool(true);//使用连接池  
        return connFactory;  
}

4、Redis的RedisTemplate 

有了Redis连接工厂,就要具体的Redis Session了。 

import org.springframework.data.redis.core.RedisTemplate;  
import org.springframework.data.redis.serializer.StringRedisSerializer;  
@Bean  
public  RedisTemplate<String, String> redis() {  
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();  
  
        redisTemplate.setConnectionFactory(jedisConnectionFactory());  
        redisTemplate.setKeySerializer(new StringRedisSerializer());//key的序列化适配器  
        redisTemplate.setValueSerializer(new StringRedisSerializer());//value的序列化适配器,也可以自己编写,大部分场景StringRedisSerializer足以满足需求了。  
  
        return redisTemplate;  
}

4、操作Redis

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.data.redis.core.RedisTemplate;  
import org.springframework.stereotype.Component;  
  
@Component  
public class CacheDemo {  
    @Autowired 
   private RedisTemplate<String, String> redis; public void set(String key,String value){ redis.opsForValue().set(key, value); } }
5、操作

redis.opsForValue():封装操作String

redis.opsForList():封装操作List

redis.opsForSet():封装操作Set

redis.opsForZSet():封装操作Sorted Set

redis.opsForHash():封装操作Hash

6、基于XML的配置

上面是基于注解的方式注入连接工厂和Session的,如果是基于XML的配置,可以这样设置。

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis1.host}" />
        <property name="port" value="${redis1.port}" />
    </bean>
    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>
    
    <bean id="redisOps" class="com.xjj.spring.data.XjjStringRedisOps">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>

说明:XjjStringRedisOps是自己封装的Session

 

参考:

http://haoran-10.iteye.com/blog/2261703(以上内容转自此篇文章,观察最后一句话,貌似这个博主有些心事!)

http://www.itkeyword.com/doc/240592287730467262/redis-spring-data-jedisjavajunit

原文地址:https://www.cnblogs.com/EasonJim/p/7804545.html