PackageTest.iafclub.redis;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;ImportJava.util.UUID;ImportJava.util.concurrent.TimeUnit;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.BoundHashOperations;Importorg.springframework.data.redis.core.BoundListOperations;Importorg.springframework.data.redis.core.BoundSetOperations;Importorg.springframework.data.redis.core.BoundValueOperations;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.data.redis.core.StringRedisTemplate;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportCom.iafclub.baseTools.util.MyDateUtil;Importcom.iafclub.demo.domain.Dictionary; the @RunWith (Springjunit4classrunner.class)//after configuring the @contextconfiguration annotation and using the locations property of the annotation to indicate the spring and configuration file,//@ContextConfiguration (locations = {"Classpath:spring.xml", "Classpath:spring-activitymq-test.xml", "Classpath: Spring-mybatis.xml "," Classpath:dubbo.xml "})@ContextConfiguration (locations = {"Classpath:spring.xml", "Classpath:spring-activitymq-test.xml", "Classpath: Spring-mybatis.xml "," Classpath:dubbo.xml "," Classpath:spring-redis.xml " }) Public classredistest {@AutowiredPrivatestringredistemplate stringredistemplate; @AutowiredPrivateredistemplate redistemplate; PrivateString Reverse_key = "Batchjob:task_"; PrivateDictionary initdictionary (String i) {Dictionary Dictionary=NewDictionary (); Dictionary.setid (Uuid.randomuuid (). toString ()); Dictionary.settypeid ("002"); Dictionary.settypename ("Dictionary Classification"); Dictionary.setfieldkey ("Username" +i); Dictionary.setfieldvalue ("Passers-by"); Dictionary.setfieldback ("Back1"); Dictionary.setfieldback2 ("Back2"); Dictionary.setfieldback3 ("Back3"); Dictionary.setremark ("Remarks" +i); returndictionary; } /**Map Insertion*/@Test Public voidTestmapput () {String key= reverse_key+ "_testmap"; Map<string, string> newmap =NewHashmap<string, string>(); Stringredistemplate.boundhashops (Key). Putall (Newmap); Boundhashoperations<string, Object, object> TestMap =Stringredistemplate.boundhashops (key); Testmap.put ("User2", Jsonobject.fromobject ( This. Initdictionary ("55555AA") . toString ()); Testmap.persist (); } /**Map Fetch Value*/@Test Public voidTestmapget () {String key= "TestMap"; Boundhashoperations<string, Object, object> testMap2 =Stringredistemplate.boundhashops (key); System.out.println (Testmap2.getexpire ()); Dictionary Dictionary= (Dictionary) Jsonobject.tobean (Jsonobject.fromobject (Testmap2.get ("user")), Dictionary.class); System.out.println (Dictionary.getfieldkey ()); } /**String Type Insert*/@Test Public voidTeststringput () { for(inti=0;i<10;i++) {Dictionary Dictionary= This. Initdictionary (i+ ""); String messagecontent=Jsonobject.fromobject (dictionary). toString (); System.out.println ("Send message:" +messagecontent); String Key= "User_" +i; Stringredistemplate.boundvalueops (Key). Set (Messagecontent,110, timeunit.days); } List<Dictionary> Dictionarys =NewArraylist<dictionary>(); for(inti=0;i<10;i++) {Dictionary Dictionary= This. Initdictionary (i+ ""); Dictionarys.add (dictionary); } String messagecontent=Jsonarray.fromobject (Dictionarys). toString (); System.out.println ("Send message:" +messagecontent); Boundvalueoperations<string, string> opt = stringredistemplate.boundvalueops ("Dictionaryslist"); Opt.set (Messagecontent,110, timeunit.seconds);//Opt.set (messagecontent);System.out.println ("Send Complete"); } /**String Type value * Gets the specified value*/@Test Public voidTeststringget () { for(inti=0;i<1000;i++) {boundvalueoperations<string, string> opt = stringredistemplate.boundvalueops ("Dictionaryslist"); Try{Thread.Sleep (1000); } Catch(Interruptedexception e) {} System.out.println (Opt.getexpire ()); System.out.println (Opt.get ()); } } /**String Type value * gets all user_ values beginning with key **/@Test Public voidGetsetvalue () {Set<String> keys = Stringredistemplate.keys ("user_*"); System.out.println (keys); for(String key:keys) {boundvalueoperations<string, string> opt =Stringredistemplate.boundvalueops (key); System.out.println (Opt.getexpire ()); } } /**Delete **/@Test Public voidDeleteValue () {//redistemplate.delete ("user_*");Stringredistemplate.delete ("Dictionaryslist"); System.out.println ("0aaaaaaaaaaaaaaaaa"); } /**Delete **/@Test Public voidAddValue () {//redistemplate.delete ("user_*");Stringredistemplate.boundvalueops ("Dictionaryslist"). Set ("AA", 2, timeunit.minutes); System.out.println ("0aaaaaaaaaaaaaaaaa"); } /**Add list **/@Test Public voidtestredistemplate () {//redistemplate.delete ("user_*");String key = "Boundlistops"; Boundlistoperations boundlistoperations=Redistemplate.boundlistops (key); System.out.println ("=======a=====" +boundlistoperations); Boundlistoperations.leftpush ( This. Initdictionary (System.currenttimemillis () + "")); Object Object=Redistemplate.boundlistops (key). Leftpop (); System.out.println ("=======b=====" +Jsonobject.fromobject (object)); System.out.println ("\n\n\n\n\n\n\n\n\n\n"); } /**Add list **/@Test Public voidTestredistemplateb () {String key= "Mygod"; intI=0; while(true) {i++; Boundlistoperations boundlistoperations=Redistemplate.boundlistops (key); System.out.println ("=======a=====" +boundlistoperations.size ()); String Keya= Mydateutil.getcurrentdatetimestr () + "00000000000" +i; Boundlistoperations.leftpush ( This. Initdictionary (Keya)); Boundsetoperations boundsetoperations= Redistemplate.boundsetops (key+ "Set"); Boundsetoperations.add ( This. Initdictionary (Keya)); if(I > 100){ Break; }//Dictionary Dictionary = (Dictionary) boundlistoperations.rightpop ();//System.out.println (Dictionary.getfieldkey ());} System.out.println ("\n\n\n\n\n\n\n\n\n\n"); } @Test Public voidTESTREDISLISTC () {String key= "Mygod"; Boundlistoperations boundlistoperations=Redistemplate.boundlistops (key); System.out.println (Boundlistoperations.size ()); while(Boundlistoperations.size () > 0) {System.out.println (Boundlistoperations.rightpop ()); }} @Test Public voidTestredisset () {String key= "Mygodset"; Boundsetoperations boundsetoperations=Redistemplate.boundsetops (key); System.out.println (Boundsetoperations.size ()); Set Set=boundsetoperations.members (); for(Iterator itor =set.iterator (); Itor.hasnext ();) {Dictionary D=(Dictionary) itor.next (); System.out.println (Jsonobject.fromobject (d)); } }}
Examples of Redis use