Redis 黑马skyout

sql 复制代码
> localhost connected!
> get name
jack
> setex code 30 1234
OK
> get code
1234
> setnx code1 114514//不可修改
sql 复制代码
> hset 001 name xm
1
> hset 001 age 22
1
> hkeys 001
name
age
> hvals 001
xm
22
sql 复制代码
package com.sky.test;


import org.apache.el.parser.BooleanNode;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class SpringDataRedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedis(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        HashOperations hashOperations = redisTemplate.opsForHash();
        ListOperations listOperations = redisTemplate.opsForList();
        SetOperations setOperations = redisTemplate.opsForSet();
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    }

    /**
     * 操作字符串类型
     */
    @Test
    public void testString(){
        redisTemplate.opsForValue().set("city","北京");
        Object city = redisTemplate.opsForValue().get("city");
        System.out.println(city);
        redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);
        redisTemplate.opsForValue().setIfAbsent("lock","1");
        redisTemplate.opsForValue().setIfAbsent("lock","2");
    }

    /**
     * 操作hash类型
     */
    @Test
    public void testHash(){
        HashOperations hashOperations = redisTemplate.opsForHash();

        hashOperations.put("100","name","tom");
        hashOperations.put("100","age","20");

        String name = (String) hashOperations.get("100","name");
        System.out.println(name);

        Set keys = hashOperations.keys("100");
        System.out.println(keys);

        List values = hashOperations.values("100");
        System.out.println(values);

        hashOperations.delete("100","age");

    }

    /**
     * 操作list类型
     */
    @Test
    public void testList(){
        ListOperations listOperations = redisTemplate.opsForList();

        listOperations.leftPushAll("mylist","a","b","c");
        listOperations.leftPush("mylist","d");

        List mylist = listOperations.range("mylist",0,-1);
        System.out.println(mylist);
        listOperations.rightPop("mylist");
        Long size = listOperations.size("mylist");
        System.out.println(size);
    }
    /**
     * 操作set类型
     */
    //sadd smembers scard sinter sunion srem
    @Test
    public void testSet(){
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("mySet1","a","b","c");
        setOperations.add("mySet2","a","b","d");

        Set members = setOperations.members("myset1");
        System.out.println(members);

        Long size = setOperations.size("mySet1");
        System.out.println(size);

        Set intersect = setOperations.intersect("mySet1","mySet2");
        System.out.println(intersect);

        Set union = setOperations.union("mySet1","mySet2");
        System.out.println(union);

        setOperations.remove("mySet1","a","b");

    }

    /**
     * 操作zset类型
     */
    @Test
    public void testZSet(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();

        zSetOperations.add("myzset", "tom", 10);
        zSetOperations.add("myzset", "jerry", 20);
        zSetOperations.add("myzset", "lucy", 30);

        Set zset1 = zSetOperations.range("myzset",0,-1);
        System.out.println(zset1);
    }

    /**
     * 通用命令
     */
    @Test
    public void testCommon(){
    //keys exists type del
        Set keys = redisTemplate.keys("*");
        System.out.println(keys);

        Boolean name = redisTemplate.hasKey("*");
        Boolean set1 = redisTemplate.hasKey("set1");

        for (Object key : keys){
            DataType type = redisTemplate.type(key);
            System.out.println(type.name());

        }
    }
}
相关推荐
一点程序1 天前
基于SpringBoot的选课调查系统
java·spring boot·后端·选课调查系统
C雨后彩虹1 天前
计算疫情扩散时间
java·数据结构·算法·华为·面试
2401_832131951 天前
Python单元测试(unittest)实战指南
jvm·数据库·python
2601_949809591 天前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
vx_BS813301 天前
【直接可用源码免费送】计算机毕业设计精选项目03574基于Python的网上商城管理系统设计与实现:Java/PHP/Python/C#小程序、单片机、成品+文档源码支持定制
java·python·课程设计
2601_949868361 天前
Flutter for OpenHarmony 电子合同签署App实战 - 已签合同实现
java·开发语言·flutter
打工的小王1 天前
redis(四)搭建哨兵模式:一主二从三哨兵
数据库·redis·缓存
Anarkh_Lee1 天前
【小白也能实现智能问数智能体】使用开源的universal-db-mcp在coze中实现问数 AskDB智能体
数据库·人工智能·ai·开源·ai编程
达文汐1 天前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
培风图南以星河揽胜1 天前
Java版LeetCode热题100之零钱兑换:动态规划经典问题深度解析
java·leetcode·动态规划