redis操作Template测试

  1. package com.transn;  
  2.   
  3.   
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.HashSet;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9. import java.util.Set;  
  10. import java.util.concurrent.TimeUnit;  
  11. import java.util.concurrent.locks.Lock;  
  12.   
  13. import javax.annotation.Resource;  
  14.   
  15. import org.junit.Test;  
  16. import org.junit.runner.RunWith;  
  17. import org.springframework.beans.factory.annotation.Autowired;  
  18. import org.springframework.data.redis.core.HashOperations;  
  19. import org.springframework.data.redis.core.ListOperations;  
  20. import org.springframework.data.redis.core.RedisOperations;  
  21. import org.springframework.data.redis.core.SetOperations;  
  22. import org.springframework.data.redis.core.StringRedisTemplate;  
  23. import org.springframework.data.redis.core.ValueOperations;  
  24. import org.springframework.data.redis.core.ZSetOperations;  
  25. import org.springframework.data.redis.core.ZSetOperations.TypedTuple;  
  26. import org.springframework.test.context.ContextConfiguration;  
  27. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  28. import org.springframework.test.context.transaction.TransactionConfiguration;  
  29. import org.springframework.transaction.annotation.Transactional;  
  30.   
  31. import com.alibaba.fastjson.JSON;  
  32. import com.transn.common.rediscache.CacheOperator;  
  33. import com.transn.common.redislock.SimpleRedisLockFactoryBean;  
  34. import com.transn.customer.dao.CustomerMapper;  
  35. import com.transn.customer.model.Customer;  
  36. import com.transn.customer.model.Invoice;  
  37. import com.transn.message.model.MessageContent;  
  38. import com.transn.message.model.MessageInfo;  
  39. import com.transn.tr.sys.model.SysLan;  
  40. import com.transn.tr.sys.model.SysPriceRange;  
  41. import com.transn.tr.user.model.TruserInfo;  
  42.   
  43. @SuppressWarnings("deprecation")  
  44. @RunWith(SpringJUnit4ClassRunner.class// 整合  
  45. @ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})  
  46. @TransactionConfiguration(defaultRollback = false)  
  47. @Transactional  
  48. public class RedisTest {  
  49.     @Autowired  
  50.     private StringRedisTemplate stringRedisTemplate;  
  51.       
  52.     //试试注入这个lockFactory  
  53. /*  @Resource 
  54.     private IRedisLockFactoryBean lockFactory;*/  
  55.     @Resource  
  56.     private SimpleRedisLockFactoryBean lockFactory;  
  57.       
  58.     @Resource(name= "stringRedisTemplate")  
  59.     private ValueOperations opsForValue;  
  60.       
  61.     @Resource(name= "stringRedisTemplate")  
  62.     private HashOperations opsForHash;  
  63.       
  64.     @Resource(name= "stringRedisTemplate")  
  65.     private ListOperations opsForList;  
  66.   
  67.     @Resource(name= "stringRedisTemplate")  
  68.     private SetOperations opsForSet;  
  69.   
  70.     @Resource(name= "stringRedisTemplate")  
  71.     private ZSetOperations opsForZSet;  
  72.   
  73.      
  74.     @Resource  
  75.     private CustomerMapper mapper;  
  76.       
  77.     @Resource  
  78.     private CacheOperator cache;  
  79.       
  80.     @Test  
  81.     public void cacheInitTest() throws Exception {  
  82.         cache.initPriceRangeToRedisCashe();  
  83.         cache.initSysLanToRedisCashe();  
  84.         cache.initTrAbilityToRedisCashe();  
  85.     }  
  86.       
  87.     @Test  
  88.     public void cacheGetTest() throws Exception {  
  89.         SysLan sysLanFromRedisCashe = cache.getSysLanFromRedisCashe("en-GB");  
  90.         System.out.println(JSON.toJSONString(sysLanFromRedisCashe));  
  91.         String count = cache.getTrAbilityFromRedisCashe("zh-CN""ja");  
  92.         System.out.println(count);  
  93.         Set priceRangeFromRedisCashe = cache.getPriceRangeFromRedisCashe("CNY");  
  94.         System.out.println(JSON.toJSONString(priceRangeFromRedisCashe));  
  95.     }  
  96.       
  97.     @Test  
  98.     public void test() throws Exception {  
  99. /*      ValueOperations opsForValue = stringRedisTemplate.opsForValue(); 
  100.         HashOperations opsForHash = stringRedisTemplate.opsForHash(); 
  101.         ListOperations opsForList = stringRedisTemplate.opsForList(); 
  102.         SetOperations opsForSet = stringRedisTemplate.opsForSet(); 
  103.         ZSetOperations opsForZSet = stringRedisTemplate.opsForZSet();*/  
  104.           
  105.         Invoice invoice3 = new Invoice();  
  106.         invoice3.setCid("WE60000000CU");  
  107.         invoice3.setAmount(798d);  
  108.         invoice3.setCurrency("USD");  
  109.         invoice3.setTag("烽火集团");  
  110.         invoice3.setType("1");  
  111.         invoice3.setContent("2");  
  112.         invoice3.setAddress("高新四路");  
  113.         String invoice3String = JSON.toJSONString(invoice3);      
  114.         Boolean putIfAbsent = opsForHash.putIfAbsent("WETRANSN:INVOICE:CID""WE60000000CU", invoice3String);  
  115.         System.out.println(putIfAbsent);  
  116.         String invoice = opsForHash.get("WETRANSN:INVOICE:CID""WE60000000CU");  
  117.         Invoice parseObject = JSON.parseObject(invoice, Invoice.class);  
  118.         System.out.println(parseObject.getTag());  
  119.     }  
  120.       
  121.     @Test  
  122.     public void testOpsForValue() throws Exception {  
  123.         //测试分布式锁和opsForValue  
  124.         Lock lock = lockFactory.createLock("WETRANSN:LOCK:TEST");  
  125.         try {  
  126.             lock.lock();  
  127.             Customer customer = new Customer();  
  128.             customer.setCid("WE20000000CU");  
  129.             customer.setNickname("龙井");  
  130.             customer.setPwd("123456");  
  131.             customer.setPhone("15527411803");  
  132.             customer.setEmail("[email protected]");  
  133.             customer.setCompanyName("传神语联网");  
  134.             customer.setBusiness("翻译软件");  
  135.             customer.setContacts("何恩培");  
  136.             customer.setInformation("027-59738888");  
  137.             customer.setcreateTime(1473734828l);  
  138.             customer.setLoginTime(1474943729l);  
  139.             customer.setSellId("WE16100036SA");  
  140.             customer.setCfrom("CHN");  
  141.             String customerString = JSON.toJSONString(customer);  
  142.             //mapper.insert(customer);  
  143.               
  144.             opsForValue.set("WETRANSN:CUSTOMER", customerString);  
  145.             //设置"WETRANSN:CUSTOMER:CONTACTS"的值为"何恩培",30秒后失效  
  146.             opsForValue.set("WETRANSN:CUSTOMER:CONTACTS""何恩培"30, TimeUnit.SECONDS);  
  147.             //=======暂时不知道含义=======  
  148.             opsForValue.set("WETRANSN:CUSTOMER:PHONE""15527411803", 5l);  
  149.             //=======暂时不知道含义=======  
  150.             Boolean setBit1 = opsForValue.setBit("WETRANSN:CUSTOMER:PWD1", 0L, true);  
  151.             System.out.println("setBit1:"+setBit1);  
  152.             //=======暂时不知道含义=======  
  153.             Boolean setBit2 = opsForValue.setBit("WETRANSN:CUSTOMER:PWD2", 0L, false);  
  154.             System.out.println("setBit2:"+setBit2);  
  155.             //=======暂时不知道含义=======  
  156.             Boolean getbit = opsForValue.getBit("WETRANSN:CUSTOMER:PWD2", 0l);  
  157.             System.out.println("getbit:"+getbit);  
  158.               
  159.             //如果"WETRANSN:CUSTOMER"存在返回false;如果"WETRANSN:CUSTOMER"不存在,就存入reidis返回true  
  160.             Boolean setIfAbsent = opsForValue.setIfAbsent("WETRANSN:CUSTOMER2", customerString);  
  161.             System.out.println("setIfAbsent:"+setIfAbsent);       
  162.             //获取"WETRANSN:CUSTOMER"的值  
  163.             String get = opsForValue.get("WETRANSN:CUSTOMER");  
  164.             System.out.println("get:"+get);  
  165.             //获取"WETRANSN:CUSTOMER:CID"的值串的[2,5]  
  166.             String get2 = opsForValue.get("WETRANSN:CUSTOMER:CID", 2l, 5l);  
  167.             System.out.println("get2:"+get2);  
  168.             //如果有“WE20000000CU”就返回“WE20000000CU”,并且赋值为“15527411803”  
  169.             String getAndSet1 = opsForValue.getAndSet("WETRANSN:CUSTOMER:CID""15527411803");  
  170.             System.out.println("getAndSet1:"+getAndSet1);  
  171.             //如果没有“张三”就返回null,并且赋值为“张三”  
  172.             String getAndSet2 = opsForValue.getAndSet("WETRANSN:CUSTOMER:NICKNAME""张三");  
  173.             System.out.println("getAndSet2:"+getAndSet2);  
  174.             //Long类型的数值累加  
  175.             Long increment1 = opsForValue.increment("WETRANSN:CUSTOMER:CREATETIME", 1473734828l);  
  176.             System.out.println("increment1:"+increment1);  
  177.             //Double类型的数值累加  
  178.             Double increment2 = opsForValue.increment("WETRANSN:CUSTOMER:MONEY", 5600d);  
  179.             System.out.println("increment2:"+increment2);  
  180.             //字符串连接,返回增加到第几个字节  
  181.             Integer append1 = opsForValue.append("WETRANSN:CUSTOMER:COMPANYNAME""chuanshen");  
  182.             System.out.println("append1:"+append1);  
  183.             Integer append2 = opsForValue.append("WETRANSN:CUSTOMER:COMPANYNAME""yulianwang");  
  184.             System.out.println("append2:"+append2);  
  185.               
  186.         } finally {  
  187.             lock.unlock();  
  188.         }  
  189.     }  
  190.       
  191.     @Test  
  192.     public void testOpsForHash() throws Exception {  
  193.         //hashMap放入键值对  
  194.         Invoice invoice1 = new Invoice();  
  195.         invoice1.setCid("WE30000000CU");  
  196.         invoice1.setAmount(488d);  
  197.         invoice1.setCurrency("CHN");  
  198.         invoice1.setTag("语联网(武汉)信息技术有限公司");  
  199.         invoice1.setType("2");  
  200.         invoice1.setContent("1");  
  201.         invoice1.setAddress("光谷E城,E2栋5楼");  
  202.         String invoice1String = JSON.toJSONString(invoice1);  
  203.         Invoice invoice2 = new Invoice();  
  204.         invoice2.setCid("WE40000000CU");  
  205.         invoice2.setAmount(599d);  
  206.         invoice2.setCurrency("SGP");  
  207.         invoice2.setTag("天津蓝晶光电技术股份有限公司");  
  208.         invoice2.setType("1");  
  209.         invoice2.setContent("2");  
  210.         invoice2.setAddress("光谷大道");  
  211.         String invoice2String = JSON.toJSONString(invoice2);  
  212.         Invoice invoice3 = new Invoice();  
  213.         invoice3.setCid("WE50000000CU");  
  214.         invoice3.setAmount(788d);  
  215.         invoice3.setCurrency("USD");  
  216.         invoice3.setTag("湖北东风汽车有限公司");  
  217.         invoice3.setType("1");  
  218.         invoice3.setContent("2");  
  219.         invoice3.setAddress("神龙大道");  
  220.         String invoice3String = JSON.toJSONString(invoice2);      
  221.         opsForHash.put("WETRANSN:INVOICE:CID""WE30000000CU", invoice1String);  
  222.         opsForHash.put("WETRANSN:INVOICE:CID""WE40000000CU", invoice2String);  
  223.         //如果不存在则返回true,并且存入redis;如果存在则返回false  
  224.         Boolean putIfAbsent = opsForHash.putIfAbsent("WETRANSN:INVOICE:TAG""WE30000000CU""语联网(武汉)信息技术有限公司");  
  225.         System.out.println(putIfAbsent);  
  226.         Boolean putIfAbsent2 = opsForHash.putIfAbsent("WETRANSN:INVOICE:CID""WE50000000CU", invoice3String);  
  227.         System.out.println(putIfAbsent2);  
  228.         //将map的键值对依次放入"WETRANSN:INVOICE:INVOICE"  
  229.         Map allMap = new HashMap();  
  230.         allMap.put("cid""WE50000000CU");  
  231.         allMap.put("amount""699");  
  232.         allMap.put("currency""USD");  
  233.         allMap.put("tag""华为科技");  
  234.         allMap.put("type""1");  
  235.         allMap.put("content""1");  
  236.         allMap.put("address""未来城");     
  237.         opsForHash.putAll("WETRANSN:INVOICE:INVOICE", allMap);  
  238.         //根据redis标识和主键取值  
  239.         String get = opsForHash.get("WETRANSN:INVOICE:INVOICE""address");  
  240.         System.out.println(get);  
  241.         //判断"WETRANSN:INVOICE:CID"中是否有存在的key  
  242.         Boolean hasKey = opsForHash.hasKey("WETRANSN:INVOICE:CID""WE90000000CU");  
  243.         System.out.println(hasKey);  
  244.         //删除"WETRANSN:INVOICE:INVOICE"中Key为"content"的记录  
  245.         opsForHash.delete("WETRANSN:INVOICE:INVOICE""content");  
  246.         //根据Collection类型的多个key,获取一个List集合的值  
  247.         List cid = new ArrayList();   
  248.         cid.add("WE30000000CU");  
  249.         cid.add("WE40000000CU");  
  250.         List multiGet = opsForHash.multiGet("WETRANSN:INVOICE:CID", cid);  
  251.         System.out.println(multiGet);  
  252.         Set cid2 = new HashSet();  
  253.         cid2.add("WE30000000CU");  
  254.         cid2.add("WE40000000CU");  
  255.         List multiGet2 = opsForHash.multiGet("WETRANSN:INVOICE:CID", cid2);  
  256.         System.out.println(multiGet2);  
  257.         //获取"WETRANSN:INVOICE:CID"下所有的key  
  258.         Set keys = opsForHash.keys("WETRANSN:INVOICE:CID");  
  259.         System.out.println(keys);  
  260.         //获取"WETRANSN:INVOICE:CID"下所有的value  
  261.         List values = opsForHash.values("WETRANSN:INVOICE:CID");  
  262.         System.out.println(values);  
  263.         //获取"WETRANSN:INVOICE:CID"的元素长度  
  264.         Long size = opsForHash.size("WETRANSN:INVOICE:CID");  
  265.         System.out.println(size);  
  266.         //获取形如:{WE30000000CU={"address":"光谷E城,E2栋5楼","amount":488,"cid":"WE30000000CU","content":"1","currency":"CHN","tag":"语联网(武汉)信息技术有限公司","type":"2"}, WE50000000CU={"address":"光谷大道","amount":599,"cid":"WE40000000CU","content":"2","currency":"SGP","tag":"天津蓝晶光电技术股份有限公司","type":"1"}, WE40000000CU={"address":"光谷大道","amount":599,"cid":"WE40000000CU","content":"2","currency":"SGP","tag":"天津蓝晶光电技术股份有限公司","type":"1"}}  
  267.         Map entries = opsForHash.entries("WETRANSN:INVOICE:CID");  
  268.         System.out.println(entries);  
  269.         //累计long和double类型的数字  
  270.         Double increment = opsForHash.increment("WETRANSN:INVOICE:AMOUNT""WE30000000CU", 699d);  
  271.         System.out.println(increment);  
  272.         Long increment2 = opsForHash.increment("WETRANSN:INVOICE:AMOUNT""WE40000000CU", 788l);  
  273.         System.out.println(increment2);  
  274.         //org.springframework.data.redis.core.StringRedisTemplate@53dad875  
  275.         RedisOperations operations = opsForHash.getOperations();  
  276.         System.out.println(operations);  
  277.           
  278.     }  
  279.       
  280.       
  281.     @Test  
  282.     public void testOpsForList() throws Exception {  
  283.         TruserInfo user = new TruserInfo();  
  284.         user.setTr_id("WE16000159TR");  
  285.         user.setNickname("祝思");  
  286.         user.setPwd("e10adc3949ba59abbe56e057f20f883e");  
  287.         user.setPhone("18900000000");  
  288.         user.setEmail("[email protected]");  
  289.         user.setCreate_time(1471105486l);  
  290.         user.setCfrom("CHN");  
  291.         user.setCurrency("CNY");  
  292.         String userString = JSON.toJSONString(user);      
  293.         TruserInfo user2 = new TruserInfo();  
  294.         user2.setTr_id("WE16000160TR");  
  295.         user2.setNickname("熊晶");  
  296.         user2.setPwd("e10adc3949ba59abbe56e057f20f883e");  
  297.         user2.setPhone("19000000000");  
  298.         user2.setEmail("[email protected]");  
  299.         user2.setCreate_time(1471105486l);  
  300.         user2.setCfrom("CHN");  
  301.         user2.setCurrency("CNY");  
  302.         String user2String = JSON.toJSONString(user2);    
  303.         //放入"WETRANSN:TRUSER:TRID"集合,返回行数(不排重)  
  304.         Long leftPush = opsForList.leftPush("WETRANSN:TRUSER:TRID", userString);  
  305.         Long leftPush2 = opsForList.leftPush("WETRANSN:TRUSER:TRID", user2String);  
  306.         System.out.println(leftPush);  
  307.         System.out.println(leftPush2);  
  308.         //将N个值全部放入"WETRANSN:TRUSER:NICKNAME",返回总个数(不排重)  
  309.         Long leftPushAll = opsForList.leftPushAll("WETRANSN:TRUSER:NICKNAME""祝思","熊晶","董畅");  
  310.         System.out.println(leftPushAll);  
  311.         List phone = new ArrayList();   
  312.         phone.add("18900000000");  
  313.         phone.add("19000000000");  
  314.         phone.add("19100000000");  
  315.         phone.add("19200000000");  
  316.         //将集合全部放入"WETRANSN:TRUSER:PHONE",返回总个数(不排重)  
  317.         Long leftPushAll2 = opsForList.leftPushAll("WETRANSN:TRUSER:PHONE", phone);  
  318.         System.out.println(leftPushAll2);  
  319.         //如果"WETRANSN:TRUSER:NICKNAME2"存在,则放入"WETRANSN:TRUSER:NICKNAME2"中,返回行数  
  320.         //如果"WETRANSN:TRUSER:NICKNAME2"不存在,则不操作,返回0  
  321.         Long leftPushIfPresent = opsForList.leftPushIfPresent("WETRANSN:TRUSER:NICKNAME2""蒋汉华");  
  322.         System.out.println(leftPushIfPresent);  
  323.         //从左边开始删除值,返回删除的值  
  324.         String leftPop = opsForList.leftPop("WETRANSN:TRUSER:PHONE");  
  325.         System.out.println(leftPop);  
  326.         //删除超时弄不清楚  
  327.         String leftPop2 = opsForList.leftPop("WETRANSN:TRUSER:PHONE"1, TimeUnit.NANOSECONDS);  
  328.         System.out.println(leftPop2);  
  329.         //取从左往右[0,1]的所有值的集合  
  330.         List range = opsForList.range("WETRANSN:TRUSER:NICKNAME"01);  
  331.         System.out.println(range);  
  332.         //获取"WETRANSN:TRUSER:NICKNAME"的个数  
  333.         Long size = opsForList.size("WETRANSN:TRUSER:NICKNAME");  
  334.         System.out.println(size);  
  335.         //删除除了[0,1]以外的所有元素  
  336.         opsForList.trim("WETRANSN:TRUSER:NICKNAME"01);  
  337.         //更新已有的元素(从0开始),如果没有则会出现越界异常,如果没有这个元素的key,也会出现找不到key的异常  
  338.         opsForList.set("WETRANSN:TRUSER:NICKNAME"0"龙津");  
  339.         opsForList.set("WETRANSN:TRUSER:NICKNAME2"1"明正");  
  340.         //删除N个元素值为“XXXX”,返回删除的个数;如果没有这个元素则返回0  
  341.         Long remove = opsForList.remove("WETRANSN:TRUSER:NICKNAME"3"龙津");  
  342.         System.out.println(remove);  
  343.         //列出index行的值,从0开始  
  344.         String index = opsForList.index("WETRANSN:TRUSER:PHONE"0);  
  345.         System.out.println(index);  
  346.         //将源key的队列的右边的一个值删除,然后塞入目标key的队列的左边,返回这个值  
  347.         String rightPopAndLeftPush = opsForList.rightPopAndLeftPush("WETRANSN:TRUSER:PHONE""WETRANSN:TRUSER:NICKNAME");  
  348.         System.out.println(rightPopAndLeftPush);  
  349.     }  
  350.       
  351.     @Test  
  352.     public void testOpsForSet() throws Exception {  
  353.         SysLan sysLan = new SysLan();  
  354.         sysLan.setCnname("中文");  
  355.         sysLan.setCode("zh-CN");  
  356.         sysLan.setEnname("Simplified Chinese");  
  357.         sysLan.setId("100000");  
  358.         String sysLanString = JSON.toJSONString(sysLan);  
  359.         SysLan sysLan1 = new SysLan();  
  360.         sysLan1.setCnname("英语");  
  361.         sysLan1.setCode("en");  
  362.         sysLan1.setEnname("English");  
  363.         sysLan1.setId("100001");  
  364.         String sysLan1String = JSON.toJSONString(sysLan1);  
  365.         SysLan sysLan2 = new SysLan();  
  366.         sysLan2.setCnname("法语");  
  367.         sysLan2.setCode("fr");  
  368.         sysLan2.setEnname("French");  
  369.         sysLan2.setId("100002");  
  370.         String sysLan2String = JSON.toJSONString(sysLan2);  
  371.         SysLan sysLan3 = new SysLan();  
  372.         sysLan3.setCnname("德语");  
  373.         sysLan3.setCode("de");  
  374.         sysLan3.setEnname("German");  
  375.         sysLan3.setId("100003");  
  376.         String sysLan3String = JSON.toJSONString(sysLan3);  
  377.         //如果不存在这个字符串,将字符串存入set集合,返回存入元素的个数;如果存在这个字符串就不操作,返回0;  
  378.         Long add = opsForSet.add("WETRANSN:SYSLAN2", sysLan3String);  
  379.         Long addmore = opsForSet.add("WETRANSN:SYSLAN", sysLanString, sysLan1String, sysLan2String,sysLan3String);  
  380.         System.out.println(add);  
  381.         System.out.println(addmore);  
  382.         //列出key为"WETRANSN:SYSLAN"的所有set集合  
  383.         Set members = opsForSet.members("WETRANSN:SYSLAN");  
  384.         System.out.println(members);  
  385.         //随机取key为"WETRANSN:SYSLAN"的一个set元素  
  386.         String randomMember = opsForSet.randomMember("WETRANSN:SYSLAN");  
  387.         System.out.println(randomMember);  
  388.         //随机取N次key为"WETRANSN:SYSLAN",组成一个list集合,可以重复取出  
  389.         List randomMembers = opsForSet.randomMembers("WETRANSN:SYSLAN"3);  
  390.         System.out.println(randomMembers);  
  391.         //随机取N次key为"WETRANSN:SYSLAN",组成一个set集合,不可以重复取出  
  392.         Set distinctRandomMembers = opsForSet.distinctRandomMembers("WETRANSN:SYSLAN"2);  
  393.         System.out.println(distinctRandomMembers);  
  394.         //sysLan1String字符串是否是key为"WETRANSN:SYSLAN"set集合的元素  
  395.         Boolean member = opsForSet.isMember("WETRANSN:SYSLAN", sysLan1String);  
  396.         System.out.println(member);  
  397.         //将字符串sysLanString从key为"WETRANSN:SYSLAN"的集合,移动到key为"WETRANSN:SYSLAN2"的集合,返回是否移动成功  
  398. //      Boolean move = opsForSet.move("WETRANSN:SYSLAN", sysLanString, "WETRANSN:SYSLAN2");  
  399. //      System.out.println(move);  
  400.         //删除key为"WETRANSN:SYSLAN"的元素sysLan1String、sysLan2String的字符串,返回删除的个数  
  401. //      Long remove = opsForSet.remove("WETRANSN:SYSLAN", sysLan1String, sysLan2String);  
  402. //      System.out.println(remove);  
  403.         //从左侧依次删除元素  
  404. //      String pop = opsForSet.pop("WETRANSN:SYSLAN");  
  405. //      System.out.println(pop);  
  406.         //返回集合长度  
  407.         Long size = opsForSet.size("WETRANSN:SYSLAN");  
  408.         System.out.println(size);  
  409.         //difference(K key, K otherKey);  
  410.         //比较key与otherKey的set集合,返回与otherKey集合不一样的set集合  
  411.         Set difference = opsForSet.difference("WETRANSN:SYSLAN","WETRANSN:SYSLAN2");  
  412.         System.out.println(difference);  
  413.         Set difference2 = opsForSet.difference("WETRANSN:SYSLAN2","WETRANSN:SYSLAN");  
  414.         System.out.println(difference2);  
  415.         //differenceAndStore(K key, K otherKey, K destKey);  
  416.         //比较key与otherKey的set集合,取出与otherKey的set集合不一样的set集合,并存入destKey的set集合中,返回存入个数  
  417.         Long differenceAndStore = opsForSet.differenceAndStore("WETRANSN:SYSLAN","WETRANSN:SYSLAN2""WETRANSN:SYSLAN3");  
  418.         System.out.println(differenceAndStore);  
  419.         //intersect(K key, K otherKey);  
  420.         //比较key与otherKey的set集合,取出二者交集,返回set交集合  
  421.         Set intersect = opsForSet.intersect("WETRANSN:SYSLAN","WETRANSN:SYSLAN2");  
  422.         System.out.println(intersect);  
  423.         Set intersect2 = opsForSet.intersect("WETRANSN:SYSLAN2","WETRANSN:SYSLAN");  
  424.         System.out.println(intersect2);  
  425.         //intersectAndStore(K key, K otherKey, K destKey);  
  426.         //比较key与otherKey的set集合,取出二者交集,并存入destKey集合,返回null  
  427.         Long intersectAndStore = opsForSet.intersectAndStore("WETRANSN:SYSLAN","WETRANSN:SYSLAN3""WETRANSN:SYSLAN4");  
  428.         System.out.println(intersectAndStore);  
  429.         //union(K key, K otherKey)  
  430.         //比较key与otherKey的set集合,取出二者并集,返回set并集合  
  431.         Set union = opsForSet.union("WETRANSN:SYSLAN","WETRANSN:SYSLAN2");  
  432.         System.out.println(union);  
  433.         //unionAndStore(K key, K otherKey, K destKey)  
  434.         //比较key与otherKey的set集合,取出二者并集,并存入destKey集合,返回destKey集合个数  
  435.         Long unionAndStore = opsForSet.unionAndStore("WETRANSN:SYSLAN""WETRANSN:SYSLAN2""WETRANSN:SYSLAN5");  
  436.         System.out.println(unionAndStore);  
  437.     }  
  438.       
  439.     @Test  
  440.     public void testOpsForZSet() throws Exception {  
  441.         MessageContent message1 = new MessageContent();  
  442.         message1.setMsgId(1);  
  443.         message1.setMsgContent("内容1");  
  444.         message1.setReceiverType("TR");  
  445.         message1.setCfrom("CHN");  
  446.         String message1String = JSON.toJSONString(message1);  
  447.         MessageContent message2 = new MessageContent();  
  448.         message2.setMsgId(2);  
  449.         message2.setMsgContent("内容2");  
  450.         message2.setReceiverType("TQ");  
  451.         message2.setCfrom("USD");  
  452.         String message2String = JSON.toJSONString(message2);  
  453.         MessageContent message3 = new MessageContent();  
  454.         message3.setMsgId(3);  
  455.         message3.setMsgContent("内容3");  
  456.         message3.setReceiverType("ZJ");  
  457.         message3.setCfrom("SGP");  
  458.         String message3String = JSON.toJSONString(message3);  
  459.         MessageContent message4 = new MessageContent();  
  460.         message4.setMsgId(4);  
  461.         message4.setMsgContent("内容4");  
  462.         message4.setReceiverType("RT");  
  463.         message4.setCfrom("CVB");  
  464.         String message4String = JSON.toJSONString(message4);  
  465. //      Set messageSet = new HashSet();  
  466. //      messageSet.add(message1String);  
  467.         //存入字符串和排序号到zset  
  468.         Boolean add = opsForZSet.add("WETRANSN:MESSAGE2", message1String, 1);  
  469.         Boolean add2 = opsForZSet.add("WETRANSN:MESSAGE2", message2String, 3);  
  470.         Boolean add3 = opsForZSet.add("WETRANSN:MESSAGE2", message3String, 5);  
  471.         Boolean add4 = opsForZSet.add("WETRANSN:MESSAGE2", message4String, 9);  
  472.         System.out.println(add);  
  473.         //取key为"WETRANSN:MESSAGE1"集合,从0到1,返回取出来的集合;0到-1是取全部;  
  474.         Set range = opsForZSet.range("WETRANSN:MESSAGE1",0,1);  
  475.         System.out.println(range);  
  476.         //删除一个或多个值,返回删除个数  
  477. //      Long remove = opsForZSet.remove("WETRANSN:MESSAGE1", message3String);  
  478. //      System.out.println(remove);  
  479.         //删除[M,N]行号的值,返回删除个数  
  480. //      Long removeRange = opsForZSet.removeRange("WETRANSN:MESSAGE2", 0, 1);  
  481. //      System.out.println("removeRange:"+removeRange);  
  482.         //删除[M,N]序列号的值,返回删除个数  
  483. //      Long removeRangeByScore = opsForZSet.removeRangeByScore("WETRANSN:MESSAGE2", 0, 4);  
  484. //      System.out.println("removeRangeByScore:"+removeRangeByScore);  
  485.         //取行号,从0开始  
  486.         Long rank = opsForZSet.rank("WETRANSN:MESSAGE1", message3String);  
  487.         System.out.println(rank);  
  488.         //取反转行号,与rank相反  
  489.         Long reverseRank = opsForZSet.reverseRank("WETRANSN:MESSAGE1", message3String);  
  490.         System.out.println(reverseRank);  
  491.         //取double类型的序列号  
  492.         Double score = opsForZSet.score("WETRANSN:MESSAGE1", message3String);  
  493.         System.out.println(score);  
  494.         //列出序列号从[N,M]的元素个数  
  495.         Long count = opsForZSet.count("WETRANSN:MESSAGE1"03);  
  496.         System.out.println(count);  
  497.         //返回集合的长度  
  498.         Long size = opsForZSet.size("WETRANSN:MESSAGE2");  
  499.         System.out.println(size);  
  500.         //返回集合的长度  
  501.         Long zCard = opsForZSet.zCard("WETRANSN:MESSAGE2");  
  502.         System.out.println(zCard);  
  503.       
  504.       
  505.       
  506.       
  507.     }  
  508.       
  509. }

你可能感兴趣的:(redis操作Template测试)