1 先看pom.xml
org.apache.commons
commons-pool2
2.0
redis.clients
jedis
2.9.0
org.springframework.data
spring-data-redis
1.6.2.RELEASE
commons-io
commons-io
2.4
junit
junit
4.12
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
两段配置都要
classpath:redis/redis.properties
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
描述具有简单值的条目
opsForList()
ListOperations
操作具有list值的条目
opsForSet()
SetOperations
操作具有set值的条目
opsForZSet()
ZSetOperations
操作具有ZSet值(排序的set)的条目
opsForHash()
HashOperations
操作具有hash值的条目
boundValueOps(K)
BoundValueOperations
以绑定指定key的方式,操作具有简单值的条目
boundListOps(K)
BoundListOperations
以绑定指定key的方式,操作具有list的条目
boundSetOps(K)
BoundSetOperations
以绑定指定key的方式,操作具有set的条目
boundZSet(K)
BoundZSetOperations
以绑定指定key的方式,操作具有ZSet(排序的set)的条目
boundHashOps(K)
BoundHashOperations
以绑定指定key的方式,操作具有hash值的条目
StringRedisTemplate
方法
子API接口
描述
opsForValue()
ValueOperationsString>
描述具有简单值的条目
opsForList()
ListOperations
操作具有list值的条目
opsForSet()
SetOperations
操作具有set值的条目
opsForZSet()
ZSetOperations
操作具有ZSet值(排序的set)的条目
opsForHash()
HashOperations
操作具有hash值的条目
boundValueOps(K)
BoundValueOperations
以绑定指定key的方式,操作具有简单值的条目
boundListOps(K)
BoundListOperations
以绑定指定key的方式,操作具有list的条目
boundSetOps(K)
BoundSetOperations
以绑定指定key的方式,操作具有set的条目
boundZSet(K)
BoundZSetOperations
以绑定指定key的方式,操作具有ZSet(排序的set)的条目
boundHashOps(K)
BoundHashOperations
以绑定指定key的方式,操作具有hash值的条目
常用方法:
转载:http://blog.csdn.net/u011911084/article/details/53435172
[java] view plain copy
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
[java] view plain copy
stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作
[java] view plain copy
stringRedisTemplate.opsForValue().get("test")//根据key获取缓存中的val
[java] view plain copy
stringRedisTemplate.boundValueOps("test").increment(1);//val +1
[java] view plain copy
stringRedisTemplate.getExpire("test")//根据key获取过期时间
[java] view plain copy
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位
[java] view plain copy
stringRedisTemplate.delete("test");//根据key删除缓存
[java] view plain copy
stringRedisTemplate.hasKey("546545");//检查key是否存在,返回boolean值
[java] view plain copy
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
[java] view plain copy
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间
[java] view plain copy
stringRedisTemplate.opsForSet().isMember("red_123", "1")//根据key查看集合中是否存在指定数据
[java] view plain copy
stringRedisTemplate.opsForSet().members("red_123");//根据key获取set集合