redis使用例子
Posted a393060727
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis使用例子相关的知识,希望对你有一定的参考价值。
package test.iafclub.redis; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.TimeUnit; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.BoundListOperations; import org.springframework.data.redis.core.BoundSetOperations; import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.iafclub.baseTools.util.MyDateUtil; import com.iafclub.demo.domain.Dictionary; @RunWith(SpringJUnit4ClassRunner.class) //配置了@ContextConfiguration注解并使用该注解的locations属性指明spring和配置文件之后, //@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 class RedisTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; private String REVERSE_KEY = "batchJob:task_"; private Dictionary initDictionary(String i){ Dictionary dictionary = new Dictionary(); dictionary.setId(UUID.randomUUID().toString()); dictionary.setTypeId("002"); dictionary.setTypeName("字典分类"); dictionary.setFieldKey("username"+i); dictionary.setFieldValue("路人甲"); dictionary.setFieldBack("back1"); dictionary.setFieldBack2("back2"); dictionary.setFieldBack3("back3"); dictionary.setRemark("备注"+i); return dictionary; } /**Map插入*/ @Test public void testMapPut(){ String key = REVERSE_KEY+"_testMap"; Map<String, String> newMap = new HashMap<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取值*/ @Test public void testMapGet(){ 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 类型插入*/ @Test public void testStringPut(){ for (int i=0;i<10;i++){ Dictionary dictionary = this.initDictionary(i+""); String messageContent = JSONObject.fromObject(dictionary).toString(); System.out.println("发送消息:" + messageContent); String key = "USER_"+i; stringRedisTemplate.boundValueOps(key).set(messageContent, 110,TimeUnit.DAYS); } List<Dictionary> dictionarys = new ArrayList<Dictionary>(); for (int i=0;i<10;i++){ Dictionary dictionary = this.initDictionary(i+""); dictionarys.add(dictionary); } String messageContent = JSONArray.fromObject(dictionarys).toString(); System.out.println("发送消息:" + messageContent); BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps("dictionarysList"); opt.set(messageContent, 110,TimeUnit.SECONDS); // opt.set(messageContent); System.out.println("发送完成"); } /**String 类型取值 * 获取指定的值*/ @Test public void testStringGet(){ for (int i=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 类型取值 *获取所有USER_开头为key的值 * */ @Test public void getSetValue(){ 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()); } } /**删除 * */ @Test public void deleteValue(){ // redisTemplate.delete("USER_*"); stringRedisTemplate.delete("dictionarysList"); System.out.println("0aaaaaaaaaaaaaaaaa"); } /**删除 * */ @Test public void addValue(){ // redisTemplate.delete("USER_*"); stringRedisTemplate.boundValueOps("dictionarysList").set("aa", 2, TimeUnit.MINUTES); System.out.println("0aaaaaaaaaaaaaaaaa"); } /**添加list * */ @Test public void testRedisTemplate(){ // 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"); } /**添加list * */ @Test public void testRedisTemplateB(){ String key = "mygod"; int i=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 void testRedisListC(){ String key = "mygod"; BoundListOperations boundListOperations = redisTemplate.boundListOps(key); System.out.println(boundListOperations.size()); while (boundListOperations.size() > 0){ System.out.println(boundListOperations.rightPop()); } } @Test public void testRedisSet(){ 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)); } } }
以上是关于redis使用例子的主要内容,如果未能解决你的问题,请参考以下文章