在Spring Boot 2和Redis中使用List结构可以实现很多应用场景,比如微博的关注列表。以下是一个简单的示例代码:
-
添加Redis依赖:
在
pom.xml
中添加Spring Boot对Redis的支持:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置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
。 -
编写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
方法用于进行关注操作,getFollowingList
和getFollowersList
方法用于获取关注列表和粉丝列表。 -
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,即取消关注。