• Springboot中使用Redis


    Redis 是一个基于内存的key-value的结构数据库
    适合存储热点数据

    Macos安装Redis

    https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/
    安装redis

    brew install redis
    
    • 1

    查看安装信息:

    brew info redis
    
    • 1

    前台启动redis:

    redis-server
    
    • 1

    后台启动redis:

    brew services start redis
    
    • 1

    查看信息:

    brew services info redis
    
    • 1

    停止:

    brew services stop redis
    
    • 1

    配置

    打开/opt/homebrew/etc/redis.conf配置文件
    修改密码

    requirepass 123456
    
    • 1

    登录redis

    redis-cli -h localhost -p 6379 -a 123456
    
    • 1

    数据结构

    字符串

    SET key value #设置
    GET key #获取
    SETEX key seconds value #设置指定key过期时间,单位s
    SETNX key vvalue 只有在key不存在时才设置
    
    • 1
    • 2
    • 3
    • 4

    哈希

    HSET key fiele value  将key设置为value
    HGET key field 获取
    HDEL key field 删除 
    HKEYS key 获取所有字段
    HVALS key 获取所有值
    
    • 1
    • 2
    • 3
    • 4
    • 5

    列表

    LPUSH key value1[value2] 插入到头部 左侧
    LRANGE key start stop 获取指定范围达到元素
    RPOP key 移除并获取列表最后一个元素 右侧
    LLEN key 获取列表长度
    
    • 1
    • 2
    • 3
    • 4

    集合

    SADD key number1 [number2] 插入一个成员
    SMEMBERS key 返回集合中所有成员
    SCARD key 获取成员数
    SINTER key1[key2] 返回定义所哟集合的交集
    SUNION key1 [key2] 返回所有集合的并集
    SREM key number1[numer2] 删除集合中成员
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    有序集合

    ZADD key score1 member1 [score2 member2] 向有序集合添加一个成员
    ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合指定区间内的成员
    ZINCRBY key increment member 对指定成员分数加上增量increment
    ZREM key member [member..]  删除成员
    
    • 1
    • 2
    • 3
    • 4

    通用命令

    KEYS pattern 查找所有符合给定模式的key
    EXISTS key 检查给定key是否存在
    TYPE key 返回key 所存储的值的类型
    DEL key 在key存在时删除key
    
    • 1
    • 2
    • 3
    • 4

    SpringBoot中使用Redis

    导入依赖

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    配置Redis

    spring:
    	redis:
        host: localhost
        port: 6379
        password: 123456
        database: 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    编写配置类

    @Slf4j
    @Configuration
    public class RedisConfiguration {
        @Bean
        public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate redisTemplate = new RedisTemplate<>();
            // 设置redis连接工厂
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            // 设置redis的String/Value的默认序列化方式
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            log.info("RedisTemplate注入成功");
            return redisTemplate;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    使用Redis

    @SpringBootTest
    class RedisConfigurationTest {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testString() {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            valueOperations.set("name", "cxk");
            String name = (String) valueOperations.get("name");
            System.out.println(name);
            valueOperations.set("code", "1234", 3, TimeUnit.HOURS); //3小时后过期
            valueOperations.setIfAbsent("code1", "1234"); //如果不存在则设置
        }
    
        @Test
        public void testHash() {
            HashOperations hashOperations = redisTemplate.opsForHash();
            hashOperations.put("user", "name", "cxk");
            hashOperations.put("user", "age", "18");
            String name = (String) hashOperations.get("user", "name");
            System.out.println(name);
    
            Set user = hashOperations.keys("user");
            System.out.println(user);
    
            List user1 = hashOperations.values("user");
            System.out.println(user1);
    
            hashOperations.delete("user", "name");
        }
    
        @Test
        public void testList() {
            ListOperations listOperations = redisTemplate.opsForList();
            listOperations.leftPushAll("list", "a", "b", "c");
            listOperations.leftPush("list", "d");
    
            List list = listOperations.range("list", 0, -1);
            System.out.println(list);
    
            listOperations.rightPop("list");
    
            Long size = listOperations.size("list");
            System.out.println(size);
        }
    
        @Test
        public void testSet() {
            SetOperations setOperations = redisTemplate.opsForSet();
            setOperations.add("set", "a", "b", "c", "d", "e");
            Set set = setOperations.members("set");
            System.out.println(set);
    
            setOperations.remove("set", "a", "b");
            set = setOperations.members("set");
            System.out.println(set);
    
    
            setOperations.add("set2", "a", "b", "c", "d", "e");
            Set union = setOperations.union("set", "set2");
            Set intersect = setOperations.intersect("set", "set2");
            Set difference = setOperations.difference("set", "set2");
            System.out.println(union);
            System.out.println(intersect);
            System.out.println(difference);
        }
        
        @Test
        public void testZset(){
            ZSetOperations zSetOperations = redisTemplate.opsForZSet();
            zSetOperations.add("zset", "a", 1);
            zSetOperations.add("zset", "b", 2);
            zSetOperations.add("zset", "c", 3);
    
            Set zset = zSetOperations.range("zset", 0, -1);
            System.out.println(zset);
            
            zSetOperations.incrementScore("zset", "a", 1);
            zSetOperations.remove("zset", "a");
        }
        @Test
        public void testCommon(){
            Set keys = redisTemplate.keys("*");
            System.out.println(keys);
    
            Boolean name = redisTemplate.hasKey("name");
    
            for (Object key : keys) {
                DataType type = redisTemplate.type(key);
                System.out.println(type.name());
            }
            redisTemplate.delete("name");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
  • 相关阅读:
    Linux的基本命令
    基于Spring Boot的房屋租赁系统
    【JavaScript--React】本篇文章将带你体验不同于vue框架的eact框架
    普冉PY32系列(十四) 从XL2400迁移到XL2400P
    【C语言初级】0 准备工作
    sychronized如何实现的?锁升级的过程?
    vue3 封装千分位分隔符自定义指令
    C++字符串类 - std::string - 常用总结
    想转行学软件测试担心哪些问题?
    vscode中提升效率的插件扩展——待更新
  • 原文地址:https://blog.csdn.net/m0_74085417/article/details/133706411