Spring StringRedisTemplate 配置

1 先看pom.xml

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>



2 创建 redis.properties

# Redis settings
redis.host=192.168.1.88
redis.port=6379
redis.timeOut=10000
# redis.pass=

redis.maxIdle=300  
redis.maxTotal=1024  
redis.maxWaitMillis=10000  
redis.testOnBorrow=true  

3 applicationContext.xml

两段配置都要

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations">
            <list>
                <value>classpath:redis/redis.properties</value>
            </list>
        </property>
    </bean>
<bean name="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <bean name="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1" value="${redis.host}" />
        <constructor-arg index="2" value="${redis.port}" />
        <constructor-arg index="3" value="${redis.timeOut}" />
    </bean>
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <!-- <property name="password" value="${redis.password}" />   -->
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"   p:connection-factory-ref="jedisConnectionFactory" />

4 创建Test.cs

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class RedisTest {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Test
    public void Test() throws Exception{
        redisTemplate.opsForValue().set("a","test");
        String q = redisTemplate.opsForValue().get("a")+" hello";
        System.out.println(q);
    }
}





比较 RedisTemplate 和 StringRedisTemplate的相关信息:

 RedisTemplate

方法 子API接口 描述
opsForValue() ValueOperations<K,V> 描述具有简单值的条目
opsForList() ListOperations<K,V> 操作具有list值的条目
opsForSet() SetOperations<K,V> 操作具有set值的条目
opsForZSet() ZSetOperations<K,V> 操作具有ZSet值(排序的set)的条目
opsForHash() HashOperations<K,HK,VH> 操作具有hash值的条目
boundValueOps(K) BoundValueOperations<K,V> 以绑定指定key的方式,操作具有简单值的条目
boundListOps(K) BoundListOperations<K,V> 以绑定指定key的方式,操作具有list的条目
boundSetOps(K) BoundSetOperations<K,V> 以绑定指定key的方式,操作具有set的条目
boundZSet(K) BoundZSetOperations<K,V> 以绑定指定key的方式,操作具有ZSet(排序的set)的条目
boundHashOps(K) BoundHashOperations<K,V> 以绑定指定key的方式,操作具有hash值的条目

StringRedisTemplate

方法 子API接口 描述
opsForValue() ValueOperations<String,String> 描述具有简单值的条目
opsForList() ListOperations<String,String> 操作具有list值的条目
opsForSet() SetOperations<String,String> 操作具有set值的条目
opsForZSet() ZSetOperations<String,String> 操作具有ZSet值(排序的set)的条目
opsForHash() HashOperations<String,Object,Object> 操作具有hash值的条目
boundValueOps(K) BoundValueOperations<String,String> 以绑定指定key的方式,操作具有简单值的条目
boundListOps(K) BoundListOperations<String,String> 以绑定指定key的方式,操作具有list的条目
boundSetOps(K) BoundSetOperations<String,String> 以绑定指定key的方式,操作具有set的条目
boundZSet(K) BoundZSetOperations<String,String> 以绑定指定key的方式,操作具有ZSet(排序的set)的条目
boundHashOps(K) BoundHashOperations<String,String> 以绑定指定key的方式,操作具有hash值的条目

常用方法:

转载:http://blog.csdn.net/u011911084/article/details/53435172

[java] view plain copy
 
  1. stringRedisTemplate.opsForValue().set("test""100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间  
[java] view plain copy
 
  1. stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作  
[java] view plain copy
 
  1. stringRedisTemplate.opsForValue().get("test")//根据key获取缓存中的val  
[java] view plain copy
 
  1. stringRedisTemplate.boundValueOps("test").increment(1);//val +1  
[java] view plain copy
 
  1. stringRedisTemplate.getExpire("test")//根据key获取过期时间  
[java] view plain copy
 
  1. stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位  
[java] view plain copy
 
  1. stringRedisTemplate.delete("test");//根据key删除缓存  
[java] view plain copy
 
  1. stringRedisTemplate.hasKey("546545");//检查key是否存在,返回boolean值  
[java] view plain copy
 
  1. stringRedisTemplate.opsForSet().add("red_123""1","2","3");//向指定key中存放set集合  
[java] view plain copy
 
  1. stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间  
[java] view plain copy
 
  1. stringRedisTemplate.opsForSet().isMember("red_123""1")//根据key查看集合中是否存在指定数据  
[java] view plain copy
 
  1. stringRedisTemplate.opsForSet().members("red_123");//根据key获取set集合  
原文地址:https://www.cnblogs.com/hanjun0612/p/9779772.html