use of com.zyd.blog.business.annotation.RedisCache in project OneBlog by zhangyd-c.
the class BizCommentServiceImpl method comment.
/**
* 发表评论
*
* @param comment
* @return
*/
@Override
@RedisCache(flush = true)
public Comment comment(Comment comment) throws ZhydCommentException {
SysConfig sysConfig = configService.getByKey(ConfigKeyEnum.ANONYMOUS.getKey());
boolean anonymous = true;
if (null != sysConfig) {
anonymous = "1".equals(sysConfig.getConfigValue());
}
// 非匿名且未登录
if (!anonymous && !SessionUtil.isLogin()) {
throw new ZhydCommentException("站长已关闭匿名评论,请先登录!");
}
// 过滤文本内容,防止xss
this.filterContent(comment);
// 已登录且非匿名,使用当前登录用户的信息评论
if (SessionUtil.isLogin()) {
this.setCurrentLoginUserInfo(comment);
} else {
this.setCurrentAnonymousUserInfo(comment);
}
// 用户没有头像时, 使用随机默认的头像
if (StringUtils.isEmpty(comment.getAvatar())) {
List<String> avatars = configService.getRandomUserAvatar();
if (!CollectionUtils.isEmpty(avatars)) {
Collections.shuffle(avatars);
int randomIndex = new Random().nextInt(avatars.size());
comment.setAvatar(avatars.get(randomIndex));
}
}
if (StringUtils.isEmpty(comment.getStatus())) {
comment.setStatus(CommentStatusEnum.VERIFYING.toString());
}
// set当前评论者的设备信息
this.setCurrentDeviceInfo(comment);
// set当前评论者的位置信息
this.setCurrentLocation(comment);
// 保存
this.insert(comment);
// 发送邮件通知
this.sendEmail(comment);
return comment;
}
use of com.zyd.blog.business.annotation.RedisCache in project OneBlog by zhangyd-c.
the class BizCommentServiceImpl method listRecentComment.
/**
* 查询近期评论
*
* @param pageSize
* @return
*/
@Override
@RedisCache
public List<Comment> listRecentComment(int pageSize) {
CommentConditionVO vo = new CommentConditionVO();
vo.setPageSize(pageSize);
vo.setStatus(CommentStatusEnum.APPROVED.toString());
PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
List<BizComment> list = bizCommentMapper.findPageBreakByCondition(vo);
return getComments(list);
}
use of com.zyd.blog.business.annotation.RedisCache in project OneBlog by zhangyd-c.
the class BizTagsServiceImpl method removeByPrimaryKey.
@Override
@Transactional(rollbackFor = Exception.class)
@RedisCache(flush = true)
public boolean removeByPrimaryKey(Long primaryKey) {
BizArticleTags articleTag = new BizArticleTags();
articleTag.setTagId(primaryKey);
List<BizArticleTags> articleTags = bizArticleTagsMapper.select(articleTag);
if (!CollectionUtils.isEmpty(articleTags)) {
throw new ZhydException("当前标签下存在文章信息,禁止删除!");
}
return bizTagsMapper.deleteByPrimaryKey(primaryKey) > 0;
}
use of com.zyd.blog.business.annotation.RedisCache in project OneBlog by zhangyd-c.
the class BizTagsServiceImpl method insert.
@Override
@Transactional(rollbackFor = Exception.class)
@RedisCache(flush = true)
public Tags insert(Tags entity) {
Assert.notNull(entity, "Tags不可为空!");
if (this.getByName(entity.getName()) != null) {
throw new ZhydException("标签添加失败,标签已存在![" + entity.getName() + "]");
}
entity.setUpdateTime(new Date());
entity.setCreateTime(new Date());
bizTagsMapper.insertSelective(entity.getBizTags());
return entity;
}
use of com.zyd.blog.business.annotation.RedisCache in project OneBlog by zhangyd-c.
the class RedisCacheAspect method handle.
@Around("pointcut()")
public Object handle(ProceedingJoinPoint point) throws Throwable {
if (!appProperties.isEnableRedisCache()) {
return point.proceed();
}
Method currentMethod = AspectUtil.INSTANCE.getMethod(point);
// 获取操作名称
RedisCache cache = currentMethod.getAnnotation(RedisCache.class);
boolean enable = cache.enable();
if (!enable) {
return point.proceed();
}
boolean flush = cache.flush();
if (flush) {
String classPrefix = AspectUtil.INSTANCE.getKey(point, BIZ_CACHE_PREFIX);
log.info("清空缓存 - {}*", classPrefix);
redisService.delBatch(classPrefix);
return point.proceed();
}
String key = AspectUtil.INSTANCE.getKey(point, cache.key(), BIZ_CACHE_PREFIX);
boolean hasKey = redisService.hasKey(key);
if (hasKey) {
try {
log.info("{}从缓存中获取数据", key);
return redisService.get(key);
} catch (Exception e) {
log.error("从缓存中获取数据失败!", e);
}
}
// 先执行业务
Object result = point.proceed();
redisService.set(key, result, cache.expire(), cache.unit());
log.info("{}从数据库中获取数据", key);
return result;
}
Aggregations