基于springboot2 redis 实现 String是最简单的数据类型,一般用于复杂的计数功能的缓存:粉丝数 增加和减少 ,代码实现

在Spring Boot 2中使用Redis实现String数据类型的缓存,特别是用于复杂计数功能,比如粉丝数的增加和减少,可以使用StringRedisTemplate。下面是一个简单的示例代码:

  1. 添加Redis依赖:

    pom.xml中添加Spring Boot对Redis的支持:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  2. 配置Redis连接信息:

    application.properties(或application.yml)中配置Redis连接信息:

    spring.redis.host=your-redis-host
    spring.redis.port=your-redis-port
    spring.redis.password=your-redis-password
    

    如果Redis没有密码,可以不配置spring.redis.password

  3. 编写Service类实现粉丝数的增加和减少功能:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    @Service
    public class FanService {
    
        private final StringRedisTemplate stringRedisTemplate;
    
        @Autowired
        public FanService(StringRedisTemplate stringRedisTemplate) {
            this.stringRedisTemplate = stringRedisTemplate;
        }
    
        public Long increaseFansCount(String userId) {
            // 递增粉丝数
            return stringRedisTemplate.opsForValue().increment("fans:" + userId);
        }
    
        public Long decreaseFansCount(String userId) {
            // 递减粉丝数
            return stringRedisTemplate.opsForValue().decrement("fans:" + userId);
        }
    
        public Long getFansCount(String userId) {
            // 获取粉丝数
            String fansCount = stringRedisTemplate.opsForValue().get("fans:" + userId);
            return fansCount != null ? Long.parseLong(fansCount) : 0;
        }
    }
    

    上述代码中,使用StringRedisTemplate进行Redis操作,通过opsForValue()获取String类型的操作对象,然后可以使用incrementdecrement进行递增和递减操作,同时通过get获取当前粉丝数。

  4. Controller层调用Service:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/fans")
    public class FanController {
    
        private final FanService fanService;
    
        @Autowired
        public FanController(FanService fanService) {
            this.fanService = fanService;
        }
    
        @PostMapping("/increase/{userId}")
        public Long increaseFansCount(@PathVariable String userId) {
            return fanService.increaseFansCount(userId);
        }
    
        @PostMapping("/decrease/{userId}")
        public Long decreaseFansCount(@PathVariable String userId) {
            return fanService.decreaseFansCount(userId);
        }
    
        @GetMapping("/get/{userId}")
        public Long getFansCount(@PathVariable String userId) {
            return fanService.getFansCount(userId);
        }
    }
    

    上述代码中,使用@PostMapping@GetMapping定义了增加、减少和获取粉丝数的接口。通过访问这些接口,可以实现对Redis中存储的粉丝数进行操作。

确保Redis服务器已启动,并根据实际情况修改配置信息和代码中的key,就可以运行这个简单的示例了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值