1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
public class Test1 {
public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml"); final RedisTemplate<String,Object> redisTemplate = ctx.getBean("redisTemplate",RedisTemplate.class); ValueOperations<String,Object> value = redisTemplate.opsForValue(); value.set("key1","HELLO!!"); System.out.println(value.get("key1")); HashOperations<String,Object,Object> hash = redisTemplate.opsForHash(); Map<String,Object> map1 = new HashMap<String,Object>(); Map<String,Object> map2 = new HashMap<String,Object>(); map1.put("name","Apple"); map1.put("age","40"); map2.put("name","Orange"); map2.put("age","60"); hash.putAll("map1",map1); hash.putAll("map2",map2); System.out.println("MAP1: "+hash.entries("map1")); System.out.println("MAP2: "+hash.entries("map2"));
ListOperations<String,Object> list = redisTemplate.opsForList(); list.rightPush("oplist1","1st"); list.rightPush("oplist1","2nd"); list.rightPush("oplist1","3rd"); System.out.println(list.range("oplist1",0,2)); list.leftPush("oplist2","1st"); list.leftPush("oplist2","2nd"); list.leftPush("oplist2","3rd"); System.out.println(list.range("oplist2",0,2));
list.rightPush("oplist3","1st"); list.rightPush("oplist3","2nd"); list.rightPush("oplist3","3rd"); System.out.println(list.range("oplist3",0,2)); list.leftPush("oplist3","1st"); list.leftPush("oplist3","2nd"); list.leftPush("oplist3","3rd"); System.out.println(list.range("oplist3",0,5));
SetOperations<String,Object> set = redisTemplate.opsForSet(); set.add("opSet","Apple"); set.add("opSet","13"); set.add("opSet","13"); System.out.println(set.members("opSet"));
ZSetOperations<String,Object> zset = redisTemplate.opsForZSet(); zset.add("opZset","Apple",2); zset.add("opZset","Orange",0); zset.add("opZset","Banana",1); System.out.println(zset.rangeByScore("opZset",0,2)); } }
|