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)); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?