Search in sources :

Example 11 with RedisCache

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;
}
Also used : SysConfig(com.zyd.blog.persistence.beans.SysConfig) ZhydCommentException(com.zyd.blog.framework.exception.ZhydCommentException) RedisCache(com.zyd.blog.business.annotation.RedisCache)

Example 12 with RedisCache

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);
}
Also used : CommentConditionVO(com.zyd.blog.business.vo.CommentConditionVO) BizComment(com.zyd.blog.persistence.beans.BizComment) RedisCache(com.zyd.blog.business.annotation.RedisCache)

Example 13 with RedisCache

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;
}
Also used : ZhydException(com.zyd.blog.framework.exception.ZhydException) BizArticleTags(com.zyd.blog.persistence.beans.BizArticleTags) RedisCache(com.zyd.blog.business.annotation.RedisCache) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with RedisCache

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;
}
Also used : ZhydException(com.zyd.blog.framework.exception.ZhydException) Date(java.util.Date) RedisCache(com.zyd.blog.business.annotation.RedisCache) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with RedisCache

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;
}
Also used : RedisCache(com.zyd.blog.business.annotation.RedisCache) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Aggregations

RedisCache (com.zyd.blog.business.annotation.RedisCache)15 Link (com.zyd.blog.business.entity.Link)4 SysLink (com.zyd.blog.persistence.beans.SysLink)4 LinkConditionVO (com.zyd.blog.business.vo.LinkConditionVO)3 ZhydException (com.zyd.blog.framework.exception.ZhydException)3 SysConfig (com.zyd.blog.persistence.beans.SysConfig)3 Date (java.util.Date)3 Transactional (org.springframework.transaction.annotation.Transactional)3 User (com.zyd.blog.business.entity.User)2 BizArticleTags (com.zyd.blog.persistence.beans.BizArticleTags)2 Tags (com.zyd.blog.business.entity.Tags)1 CommentConditionVO (com.zyd.blog.business.vo.CommentConditionVO)1 FileUploader (com.zyd.blog.file.FileUploader)1 VirtualFile (com.zyd.blog.file.entity.VirtualFile)1 ZhydArticleException (com.zyd.blog.framework.exception.ZhydArticleException)1 ZhydCommentException (com.zyd.blog.framework.exception.ZhydCommentException)1 ZhydLinkException (com.zyd.blog.framework.exception.ZhydLinkException)1 BizComment (com.zyd.blog.persistence.beans.BizComment)1 BizTags (com.zyd.blog.persistence.beans.BizTags)1 GlobalFileUploader (com.zyd.blog.plugin.file.GlobalFileUploader)1