技术派项目源码地址 :
- Gitee :技术派 - https://gitee.com/itwanger/paicoding
- Github :技术派 - https://github.com/itwanger/paicoding
RedisTemplate操作知识点
新增
/**
* 新增一个 sadd
*
* @param key
* @param value
*/
public void add(String key, String value) {
redisTemplate.opsForSet().add(key, value);
}
删除
/**
* 删除集合中的值 srem
*
* @param key
* @param value
*/
public void remove(String key, String value) {
redisTemplate.opsForSet().remove(key, value);
}
判断是否存在
/**
* 判断是否包含 sismember
*
* @param key
* @param value
*/
public void contains(String key, String value) {
redisTemplate.opsForSet().isMember(key, value);
}
获取所有value
/**
* 获取集合中所有的值 smembers
*
* @param key
* @return
*/
public Set<String> values(String key) {
return redisTemplate.opsForSet().members(key);
}
集合运算
/**
* 返回多个集合的并集 sunion
*
* @param key1
* @param key2
* @return
*/
public Set<String> union(String key1, String key2) {
return redisTemplate.opsForSet().union(key1, key2);
}
/**
* 返回多个集合的交集 sinter
*
* @param key1
* @param key2
* @return
*/
public Set<String> intersect(String key1, String key2) {
return redisTemplate.opsForSet().intersect(key1, key2);
}
/**
* 返回集合key1中存在,但是key2中不存在的数据集合 sdiff
*
* @param key1
* @param key2
* @return
*/
public Set<String> diff(String key1, String key2) {
return redisTemplate.opsForSet().difference(key1, key2);
}
白名单使用示例
public interface AuthorWhiteListService {
/**
* 判断作者是否再文章发布的白名单中;
* 这个白名单主要是用于控制作者发文章之后是否需要进行审核
*
* @param authorId
* @return
*/
boolean authorInArticleWhiteList(Long authorId);
/**
* 获取所有的白名单用户
*
* @return
*/
List<BaseUserInfoDTO> queryAllArticleWhiteListAuthors();
/**
* 将用户添加到白名单中
*
* @param userId
*/
void addAuthor2ArticleWhitList(Long userId);
/**
* 从白名单中移除用户
*
* @param userId
*/
void removeAuthorFromArticleWhiteList(Long userId);
}
核心是第一个判断是否存在白名单
/**
* 实用 redis - set 来存储允许直接发文章的白名单
*/
private static final String ARTICLE_WHITE_LIST = "auth_article_white_list";
@Autowired
private UserService userService;
@Override
public boolean authorInArticleWhiteList(Long authorId) {
return RedisClient.sIsMember(ARTICLE_WHITE_LIST, authorId);
}
对于set的操作进行了统一的封装,和上面介绍到的知识点差不多,不过这里选择的是另外一种实现策略
/*
* @param value
* @return
*/
public static <T> Boolean sIsMember(String key, T value) {
return template.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.sIsMember(keyBytes(key), valBytes(value));
}
});
}
同时还封装了几个公共方法
/**
* 判断value是否再set中
*
* @param key
* @param value
* @return
*/
public static <T> Boolean sIsMember(String key, T value) {
return template.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.sIsMember(keyBytes(key), valBytes(value));
}
});
}
/**
* 获取set中的所有内容
*
* @param key
* @param clz
* @param <T>
* @return
*/
public static <T> Set<T> sGetAll(String key, Class<T> clz) {
return template.execute(new RedisCallback<Set<T>>() {
@Override
public Set<T> doInRedis(RedisConnection connection) throws DataAccessException {
Set<byte[]> set = connection.sMembers(keyBytes(key));
if (CollectionUtils.isEmpty(set)) {
return Collections.emptySet();
}
return set.stream().map(s -> toObj(s, clz)).collect(Collectors.toSet());
}
});
}
/**
* 往set中添加内容
*
* @param key
* @param val
* @param <T>
* @return
*/
public static <T> boolean sPut(String key, T val) {
return template.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.sAdd(keyBytes(key), valBytes(val));
}
}) > 0;
}
/**
* 移除set中的内容
*
* @param key
* @param val
* @param <T>
*/
public static <T> void sDel(String key, T val) {
template.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
connection.sRem(keyBytes(key), valBytes(val));
return null;
}
});
}
判断是否需要审核
/**
* 非白名单的用户,发布的文章需要先进行审核
*
* @param article
* @return
*/
private boolean needToReview(ArticleDO article) {
// 把 admin 用户加入白名单
BaseUserInfoDTO user = ReqInfoContext.getReqInfo().getUser();
if (user.getRole() != null && user.getRole().equalsIgnoreCase(UserRole.ADMIN.name())) {
return false;
}
return article.getStatus() == PushStatusEnum.ONLINE.getCode() && !articleWhiteListService.authorInArticleWhiteList(article.getUserId());
}
- 对于非白名单的用户,若操作的是上线的文章,则需要进入审核
if (needToReview(article)) {
// 非白名单中的作者发布文章需要进行审核
article.setStatus(PushStatusEnum.REVIEW.getCode());
}
管理员还可以操作白名单
/**
* 作者白名单服务
*
* @author YiHui
* @date 2023/4/9
*/
@RestController
@Api(value = "发布文章作者白名单管理控制器", tags = "作者白名单")
@Permission(role = UserRole.ADMIN)
@RequestMapping(path = {"api/admin/author/whitelist"})
public class AuthorWhiteListController {
@Autowired
private AuthorWhiteListService articleWhiteListService;
@GetMapping(path = "get")
@ApiOperation(value = "白名单列表", notes = "返回作者白名单列表")
public ResVo<List<BaseUserInfoDTO>> whiteList() {
return ResVo.ok(articleWhiteListService.queryAllArticleWhiteListAuthors());
}
@GetMapping(path = "add")
@ApiOperation(value = "添加白名单", notes = "将指定作者加入作者白名单列表")
@ApiImplicitParam(name = "authorId", value = "传入需要添加白名单的作者UserId", required = true, allowEmptyValue = false, example = "1")
public ResVo<Boolean> addAuthor(@RequestParam("authorId") Long authorId) {
articleWhiteListService.addAuthor2ArticleWhitList(authorId);
return ResVo.ok(true);
}
@GetMapping(path = "remove")
@ApiOperation(value = "删除白名单", notes = "将作者从白名单列表")
@ApiImplicitParam(name = "authorId", value = "传入需要删除白名单的作者UserId", required = true, allowEmptyValue = false, example = "1")
public ResVo<Boolean> rmAuthor(@RequestParam("authorId") Long authorId) {
articleWhiteListService.removeAuthorFromArticleWhiteList(authorId);
return ResVo.ok(true);
}
}