@SpringBootTest class Demo1ApplicationTests { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; @Test void contextLoads() { redisTemplate.opsForValue().set("key1","value1"); System.out.println("redisTemplate存redisTemplate取:"+redisTemplate.opsForValue().get("key1")); System.out.println("redisTemplate存stringRedisTemplate取:"+stringRedisTemplate.opsForValue().get("key1")); stringRedisTemplate.opsForValue().set("key2","value2"); System.out.println("stringRedisTemplate存redisTemplate取:"+redisTemplate.opsForValue().get("key2")); System.out.println("stringRedisTemplate存stringRedisTemplate取:"+stringRedisTemplate.opsForValue().get("key2")); } // String @Test public void StringDemo1(){ //存k,v redisTemplate.opsForValue().set("key1","value1"); //取v String s = (String)redisTemplate.opsForValue().get("key1"); System.out.println(s); } @Test public void StringDemo2() throws Exception{ //设定有效时长,TimeUnit.SECONDS是单位 redisTemplate.opsForValue().set("key2","value2",1, TimeUnit.SECONDS); Thread.sleep(1100); System.out.println(redisTemplate.opsForValue().get("key2")); } @Test public void StringDemo3() throws Exception{ stringRedisTemplate.opsForValue().set("key", "hello world"); //从偏移量开始对给定key的value进行覆写,若key不存在,则前面偏移量为空 stringRedisTemplate.opsForValue().set("key", "redis", 10); // stringRedisTemplate.opsForValue().set("key", "redis", 6); System.out.println(stringRedisTemplate.opsForValue().get("key")); Thread.sleep(1100); System.out.println(stringRedisTemplate.opsForValue().get("key")); } @Test public void StringDemo4() { redisTemplate.opsForValue().set("name", "test"); //若key不存在,设值 redisTemplate.opsForValue().setIfAbsent("name", "test2"); System.out.println(redisTemplate.opsForValue().get("name"));//还是test } @Test public void StringDemo5() { //批量k,v设值 Map<String, String> map = new HashMap<String, String>(); map.put("k1", "v1"); map.put("k2", "v2"); map.put("k3", "v3"); redisTemplate.opsForValue().multiSet(map); //批量取值 List<String> keys = new ArrayList<String>(); keys.add("k1"); keys.add("k2"); keys.add("k3"); List<String> values = redisTemplate.opsForValue().multiGet(keys); System.out.println(values); //批量设值,若key不存在,设值 //redisTemplate.opsForValue().multiSetIfAbsent(map); } @Test public void StringDemo6() { redisTemplate.opsForValue().set("name", "bpf"); //设值并返回旧值,无旧值返回null System.out.println(redisTemplate.opsForValue().getAndSet("ttt", "calvin")); } @Test public void StringDemo7() { //自增操作,原子性。可以对long进行double自增,但不能对double进行long自增,会抛出异常 redisTemplate.opsForValue().increment("count", 1L);//增量为long System.out.println(redisTemplate.opsForValue().get("count")); redisTemplate.opsForValue().increment("count", 1.1);//增量为double System.out.println(redisTemplate.opsForValue().get("count")); } @Test public void StringDemo8() { //key不存在,设值 redisTemplate.opsForValue().append("str", "hello"); System.out.println(redisTemplate.opsForValue().get("str")); //key存在,追加 redisTemplate.opsForValue().append("str", " world"); System.out.println(redisTemplate.opsForValue().get("str")); } @Test public void StringDemo9() { redisTemplate.opsForValue().set("key", "hello world"); //value的长度 System.out.println(redisTemplate.opsForValue().size("key"));//11 } @Test public void StringDemo10() { redisTemplate.opsForValue().set("bitTest","a"); // 'a' 的ASCII码是 97 转换为二进制是:01100001 // 'b' 的ASCII码是 98 转换为二进制是:01100010 // 'c' 的ASCII码是 99 转换为二进制是:01100011 //因为二进制只有0和1,在setbit中true为1,false为0,因此我要变为'b'的话第六位设置为1,第七位设置为0 redisTemplate.opsForValue().setBit("bitTest",6, true); redisTemplate.opsForValue().setBit("bitTest",7, false); System.out.println(redisTemplate.opsForValue().get("bitTest")); //判断offset处是true1还是false0 System.out.println(redisTemplate.opsForValue().getBit("bitTest",7)); } //Hash @Test public void hashDemo1(){ redisTemplate.opsForHash().put("p1","name","zhangsan"); redisTemplate.opsForHash().put("p1","age","21"); redisTemplate.opsForHash().put("p1","gender","true"); } @Test public void demo1() { //put方法 redisTemplate.opsForHash().put("k1", "name", "zhangshan"); redisTemplate.opsForHash().put("k1", "age", "20"); redisTemplate.opsForHash().put("k1", "height", "170"); //hashKey不存在时,才设值 //redisTemplate.opsForHash().putIfAbsent(key, hashKey, value) } @Test public void demo2() { //putAll方法 Map<String, String> data = new HashMap<>(); data.put("name", "jack ma"); data.put("company", "alibaba"); data.put("age", "500"); redisTemplate.opsForHash().putAll("k2", data); } @Test public void demo3() { //delete方法,删除key对应的hash的hashkey及其value redisTemplate.opsForHash().delete("k2", "name"); } @Test public void demo4() { //hasKey方法,确定hashkey是否存在 System.out.println(redisTemplate.opsForHash().hasKey("k2", "name")); } @Test public void demo5() { //get方法,根据key和hashkey找出对应的值 System.out.println(redisTemplate.opsForHash().get("k1", "name")); } @Test public void demo6() { //multiGet方法,根据key和多个hashkey找出对应的多个值 Collection<Object> keys = new ArrayList<>(); keys.add("name"); keys.add("age"); System.out.println(redisTemplate.opsForHash().multiGet("k1", keys)); } @Test public void demo7() { //increment方法,对key和hashkey对应的值进行增加操作 //增加长整形(无法对浮点数据使用本方法) System.out.println(redisTemplate.opsForHash().increment("k1", "age", 1)); //增加浮点型(可以对整形数据使用本方法) System.out.println(redisTemplate.opsForHash().increment("k1", "age", 1.0)); } @Test public void demo8() { //keys方法,获取key对应的hash表的所有key Set<Object> keys = redisTemplate.opsForHash().keys("k1"); System.out.println(keys); //values方法,获取key对应的hash表的所有value List<Object> values = redisTemplate.opsForHash().values("k1"); System.out.println(values); } @Test public void demo9() { //keys方法,获取key对应的hash表的大小 long size = redisTemplate.opsForHash().size("k1"); System.out.println(size); } @Test public void demo10() { //keys方法,获取key对应的hash表的所有键值对 Map<Object, Object> entries = redisTemplate.opsForHash().entries("k1"); System.out.println(entries); } //set @Test public void setDemo1() { //集合中添加元素,返回添加个数 redisTemplate.opsForSet().add("games", "鬼泣", "古墓丽影", "仙剑奇侠传", "LOL", "DOTA自走棋"); System.out.println(redisTemplate.opsForSet().members("games")); } @Test public void setDemo2() { //从集合中删除指定元素 redisTemplate.opsForSet().remove("games", "鬼泣"); printSet("games"); } @Test public void setDemo3() { //从集合中随机删除一个元素,并返回该元素 String item = (String)redisTemplate.opsForSet().pop("games"); System.out.println(item); printSet("games"); } @Test public void setDemo4() { //将集合1中的指定元素移到集合2,移动后原集合中该数据不存在了 redisTemplate.opsForSet().move("games", "仙剑奇侠传", "chinese-games"); //打印集合1 printSet("games"); //打印集合2 printSet("chinese-games"); } @Test public void setDemo5() { //获得集合大小 Long size = redisTemplate.opsForSet().size("games"); printSet("games"); System.out.println(size); } @Test public void setDemo6() { //判断集合中是否存在某元素 Boolean ifExist = redisTemplate.opsForSet().isMember("games", "LOL"); System.out.println(ifExist); } @Test public void setDemo7() { //添加集合a redisTemplate.opsForSet().add("set-a", "a", "b", "c", "d"); //添加集合b redisTemplate.opsForSet().add("set-b", "a", "b"); //求交集 Set<String> intersection = redisTemplate.opsForSet().intersect("set-a", "set-b"); //也可以和多个key对应的集合求交集 Set<V> intersect(K key, K otherKey); System.out.println(intersection); } @Test public void setDemo8() { //添加集合a redisTemplate.opsForSet().add("set-a", "a", "b", "c", "d"); //添加集合b redisTemplate.opsForSet().add("set-b", "a", "b"); //求交集并放入集合c redisTemplate.opsForSet().intersectAndStore("set-a", "set-b", "set-c"); //也可以和多个key对应的集合求交集 Long intersectAndStore(K key, Collection<K> otherKeys, K destKey); //打印集合c printSet("set-c"); } @Test public void setDemo9() { //添加集合m redisTemplate.opsForSet().add("set-m", "a", "b", "c", "d"); //添加集合n redisTemplate.opsForSet().add("set-n", "c", "d", "e", "f"); //求并集 Set<String> union = redisTemplate.opsForSet().union("set-m", "set-n"); Set c = redisTemplate.opsForSet().union("c", union); System.out.println(union); System.out.println(c); //其他操作与intersect类似 如和多个key求并集,求并集并放入集合等 } @Test public void setDemo10() { //添加集合d redisTemplate.opsForSet().add("set-d", "a", "b", "c", "d"); //添加集合e redisTemplate.opsForSet().add("set-e", "c", "d", "e", "f"); //添加集合f redisTemplate.opsForSet().add("set-f", "e", "f", "g", "h"); //求差集(属于d不属于e和f) Set<String> difference = redisTemplate.opsForSet().difference("set-d", Arrays.asList("set-e", "set-f")); System.out.println(difference); //其他操作与交集并集类似 } @Test public void setDemo11() { //随机获取集合中的一个元素 System.out.println(redisTemplate.opsForSet().randomMember("games")); //随机获取集合中指定个数的元素(有可能重复) System.out.println(redisTemplate.opsForSet().randomMembers("games", 10)); //随机获取集合中指定个数的元素(不重复) System.out.println(redisTemplate.opsForSet().distinctRandomMembers("games", 10)); } private void printSet(String key) { //获取所有成员 Set<String> members = redisTemplate.opsForSet().members(key); System.out.println(members); } }