基于springboot2 redis 实现 list 的应用场景非常多,比如微博的关注列表的一系列操作,代码实现

在Spring Boot 2和Redis中使用List结构可以实现很多应用场景,比如微博的关注列表。以下是一个简单的示例代码:

  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. 编写FollowService类实现关注列表的存储和获取:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.ListOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class FollowService {
    
        private final RedisTemplate<String, Object> redisTemplate;
    
        private final ListOperations<String, Object> listOperations;
    
        @Autowired
        public FollowService(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
            this.listOperations = redisTemplate.opsForList();
        }
    
        public void followUser(String followerId, String followeeId) {
            // 将关注者ID添加到关注列表
            listOperations.leftPush("following:" + followerId, followeeId);
            // 将粉丝ID添加到粉丝列表
            listOperations.leftPush("followers:" + followeeId, followerId);
        }
    
        public List<Object> getFollowingList(String userId) {
            // 获取关注列表
            return listOperations.range("following:" + userId, 0, -1);
        }
    
        public List<Object> getFollowersList(String userId) {
            // 获取粉丝列表
            return listOperations.range("followers:" + userId, 0, -1);
        }
    }
    

    上述代码中,使用ListOperations进行List类型的操作,followUser方法用于进行关注操作,getFollowingListgetFollowersList方法用于获取关注列表和粉丝列表。

  4. Controller层调用Service:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/follow")
    public class FollowController {
    
        private final FollowService followService;
    
        @Autowired
        public FollowController(FollowService followService) {
            this.followService = followService;
        }
    
        @PostMapping("/followUser")
        public void followUser(@RequestParam String followerId,
                               @RequestParam String followeeId) {
            followService.followUser(followerId, followeeId);
        }
    
        @GetMapping("/getFollowingList/{userId}")
        public List<Object> getFollowingList(@PathVariable String userId) {
            return followService.getFollowingList(userId);
        }
    
        @GetMapping("/getFollowersList/{userId}")
        public List<Object> getFollowersList(@PathVariable String userId) {
            return followService.getFollowersList(userId);
        }
    }
    

    上述代码中,使用@PostMapping@GetMapping定义了关注用户、获取关注列表和获取粉丝列表的接口。通过访问这些接口,可以实现对Redis中存储的关注列表的操作。

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

取关操作代码

以下是取关操作的代码,主要是从关注列表和粉丝列表中移除对应的用户 ID:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UnfollowService {

    private final RedisTemplate<String, Object> redisTemplate;

    private final ListOperations<String, Object> listOperations;

    @Autowired
    public UnfollowService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.listOperations = redisTemplate.opsForList();
    }

    public void unfollowUser(String followerId, String followeeId) {
        // 从关注列表中移除关注者ID
        listOperations.remove("following:" + followerId, 0, followeeId);
        // 从粉丝列表中移除粉丝ID
        listOperations.remove("followers:" + followeeId, 0, followerId);
    }
}

在上述代码中,unfollowUser 方法用于进行取关操作,从关注列表和粉丝列表中分别移除对应的用户 ID。

接着,在 FollowController 中添加取关的接口:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/follow")
public class FollowController {

    private final FollowService followService;
    private final UnfollowService unfollowService;

    @Autowired
    public FollowController(FollowService followService, UnfollowService unfollowService) {
        this.followService = followService;
        this.unfollowService = unfollowService;
    }

    @PostMapping("/followUser")
    public void followUser(@RequestParam String followerId,
                           @RequestParam String followeeId) {
        followService.followUser(followerId, followeeId);
    }

    @PostMapping("/unfollowUser")
    public void unfollowUser(@RequestParam String followerId,
                             @RequestParam String followeeId) {
        unfollowService.unfollowUser(followerId, followeeId);
    }

    @GetMapping("/getFollowingList/{userId}")
    public List<Object> getFollowingList(@PathVariable String userId) {
        return followService.getFollowingList(userId);
    }

    @GetMapping("/getFollowersList/{userId}")
    public List<Object> getFollowersList(@PathVariable String userId) {
        return followService.getFollowersList(userId);
    }
}

上述代码中,通过新增 unfollowUser 接口,实现了取关操作。通过调用这个接口,可以实现从关注列表和粉丝列表中移除对应的用户 ID,即取消关注。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值