@Test
public void testSpringRedis() {
ConfigurableApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("spring.xml");
StringRedisTemplate stringRedisTemplate = ctx.getBean("stringRedisTemplate", StringRedisTemplate.class);
// String读写
stringRedisTemplate.delete("myStr");
stringRedisTemplate.opsForValue().set("myStr", "http://yjmyzz.cnblogs.com/");
System.out.println(stringRedisTemplate.opsForValue().get("myStr"));
System.out.println("---------------");
// List读写
stringRedisTemplate.delete("myList");
stringRedisTemplate.opsForList().rightPush("myList", "A");
stringRedisTemplate.opsForList().rightPush("myList", "B");
stringRedisTemplate.opsForList().leftPush("myList", "0");
List<String> listCache = stringRedisTemplate.opsForList().range(
"myList", 0, -1);
for (String s : listCache) {
System.out.println(s);
}
System.out.println("---------------");
// Set读写
stringRedisTemplate.delete("mySet");
stringRedisTemplate.opsForSet().add("mySet", "A");
stringRedisTemplate.opsForSet().add("mySet", "B");
stringRedisTemplate.opsForSet().add("mySet", "C");
Set<String> setCache = stringRedisTemplate.opsForSet().members(
"mySet");
for (String s : setCache) {
System.out.println(s);
}
System.out.println("---------------");
// Hash读写
stringRedisTemplate.delete("myHash");
stringRedisTemplate.opsForHash().put("myHash", "PEK", "北京");
stringRedisTemplate.opsForHash().put("myHash", "SHA", "上海虹桥");
stringRedisTemplate.opsForHash().put("myHash", "PVG", "浦东");
Map<Object, Object> hashCache = stringRedisTemplate.opsForHash()
.entries("myHash");
for (Map.Entry<Object, Object> entry : hashCache.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
System.out.println("---------------");
} finally {
if (ctx != null && ctx.isActive()) {
ctx.close();
}
}
}
http://www.cnblogs.com/yjmyzz/p/4113019.html