• springboot集成redis


    简单明了,快速上手,添加redis配置,注入StringRedisTemplate即可。
    参考 mall-learning

    前置准备,搭建redis、springboot

    添加依赖

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

    添加redis配置

    在application.yml中添加Redis的配置

    spring:
        redis:
          host: xxxx # Redis服务器地址
          database: 0 # Redis数据库索引(默认为0)
          port: 6379 # Redis服务器连接端口
          password: xxxx # Redis服务器连接密码(默认为空)
          jedis:
            pool:
              max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
              max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
              max-idle: 8 # 连接池中的最大空闲连接
              min-idle: 0 # 连接池中的最小空闲连接
          timeout: 3000ms # 连接超时时间(毫秒)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    添加RedisService

    定义redis操作

    public interface RedisService {
        /**
         * key 前缀
         */
        String REDIS_KEY_PREFIX = "test:key:";
        /**
         * 默认过期时间
         */
        int REDIS_KEY_EXPIRE = 120;
    
        /**
         * 存储数据
         */
        void set(String key, String value);
    
        /**
         * 获取数据
         */
        String get(String key);
    
        /**
         * 设置超期时间
         */
        boolean expire(String key, long expire);
    
        /**
         * 删除数据
         */
        void remove(String key);
    
        /**
         * 自增操作
         * @param delta 自增步长
         */
        Long increment(String key, long delta);
    }
    
    • 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

    注入StringRedisTemplate,实现RedisService接口

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class RedisServiceImpl implements RedisService {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @Override
        public void set(String key, String value) {
            stringRedisTemplate.opsForValue().set(key, value);
        }
    
        @Override
        public String get(String key) {
            return stringRedisTemplate.opsForValue().get(key);
        }
    
        @Override
        public boolean expire(String key, long expire) {
            return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    
        @Override
        public void remove(String key) {
            stringRedisTemplate.delete(key);
        }
    
        @Override
        public Long increment(String key, long delta) {
            return stringRedisTemplate.opsForValue().increment(key,delta);
        }
    }
    
    • 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

    使用

    @Service
    public class TestServiceImpl implements TestService {
        @Autowired
        private RedisService redisService;
    
        @Override
        public void testRedisSet(String key, String value) {
            redisService.set(RedisService.REDIS_KEY_PREFIX + key, value);
            redisService.expire(RedisService.REDIS_KEY_PREFIX + key, RedisService.REDIS_KEY_EXPIRE);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    176. 第二高的薪水
    万象奥科参展“2023 STM32全国巡回研讨会”—武汉站
    IEEE Standard for SystemVerilog—Chapter14. Clocking blocks
    【uniapp小程序】视图容器cover-view
    吴恩达深度学习笔记——神经网络与深度学习(Neural Networks and Deep Learning)
    【Python实战】零基础实战教程(一) Hello World!
    C语言牛客网(NowCoder)刷题篇
    视频号小店是个风口吗?今年去做是明智的选择吗?一篇详解!
    牛客 题解
    【进击的JavaScript|高薪面试必看】JS基础-异步
  • 原文地址:https://blog.csdn.net/lu13093323120/article/details/132742186