自定义redis缓存和本地缓存注解

本文详细描述了项目中如何利用本地缓存(如JVM缓存、GuavaCache、Caffeine)、分布式缓存(如Redis)进行数据查询优化,重点关注MallCache注解在不同场景下的使用,以及缓存策略的选择和设置,包括过期时间和缓存层级管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据查询顺序

一级缓存:本地缓存 -》二级缓存:redis缓存 -》数据库

在这里插入图片描述

本地缓存和分布式缓存

  • 本地缓存基于jvm, 意思是程序放在哪,数据就存储在哪,不需要网络请求,特别快,但是需要占用jvm的内存,所以存储的东西不能太大,也不能太久,有内存淘汰机制。缓存组件包括: Guava CacheCaffeineEncache
  • 分布式缓存:请求需要网络传输,速度慢一点,组件包括:RedisMongoDB

哪些场景用到缓存?

  • 邀请入会的助力排行榜,key= 活动id+pageSize,缓存过期时间1分钟
/**
     * 查询邀请入会的助力排行榜
     * @param activityId
     * @param pageSize
     * @return
     */

    @Override
    @MallCache(value = "queryHelpRankingList", expireTime = 1, timeUnit = TimeUnitEnum.MINUTES)
    public List<ActivityHelpStatisticsEntity> queryRankingList(String activityId, Integer pageSize) {
   
        List<ActivityHelpStatisticsLogPO> statisticsLogs = mapper.queryRankingList(activityId, pageSize);
        if (CollectionUtils.isEmpty(statisticsLogs)) {
   
            return new ArrayList<>();
        }
        return statisticsLogs.stream().map(po -> ActivityHelpStatisticsConverter.convert(po))
                .collect(Collectors.toList());
    }
  • 验证用户是否在分组中,可以=groupId, String nascentId, String groupIds,缓存五秒
 @MallCache(value = "checkExistGroup", expireTime = 5, timeUnit = TimeUnitEnum.SECONDS)
    private boolean checkExistGroupCache(long groupId, String nascentId, String groupIds){
   
        return OpenPlatformHelperV4.checkExistGroup(groupId, nascentId, groupIds);
    }
  • 等级体系,缓存30分钟
@MallCache(value = "GradeSystem", expireTime = 30)
    @Override
    public GradeSystem queryGradeSystem(Long groupId, Long viewId, Long shopId) {
   
        GradeSystemGetRequest gradeSystemGetRequest = new GradeSystemGetRequest();
        gradeSystemGetRequest.setViewId(viewId);
        gradeSystemGetRequest.setShopId(shopId);
        gradeSystemGetRequest.setGroupId(groupId);
        GradeSystemGetResponse gradeResponse = OpenPlatformClient.exec(groupId, gradeSystemGetRequest);
        GradeSystem result = gradeResponse.getResult();
        return result;
    }
  • 等级名称 30分钟,这种需要调用三方接口,缓存可以减少调用接口次数
/**
     * 获取等级名称
     *
     * @param groupId
     * @param shopId
     * @param grade
     * @return
     */
    @MallCache(value = "CustomerGradeName", expireTime = 30)
    public String getCustomerGradeName(long groupId, long shopId, int grade) {
   
        List<Long> viewIds = cloudPlatformService.queryViewIdByAreaIdOrBrandId(groupId, getMallId(), Arrays.asList(shopId));
        return getCustomerGradeName(groupId, shopId, grade, viewIds.get(0));
    }

    public String getCustomerGradeName(long groupId, long shopId, int grade, long viewId) {
   
        if (grade == 0) {
   
            return "非会员";
        }
        GradeInfoGetResponse response = OpenPlatformHelperV4.getGradeInfo(groupId, shopId, grade, viewId);
        AssertUtil.assertTrue(response != null && response.getSuccess(), "获取等级信息失败");
        GradeInfo gradeInfo = response.getResult();
        AssertUtil.assertNotNull(gradeInfo, "获取等级信息失败");
        return gradeInfo.getGradeName();
    }
@Override
    @MallCache(value = "VIEWID", expireTime = 60)
    public Long queryViewIdByAreaIdOrBrandId(Long groupId, Long mallId, Long shopId) {
   
        WmCompanyDO company = WmCompanyDao.dao().findByCompanyId(groupId);
        List<Long> viewIds = CloudPlatformHelper.queryViewIdByShopIds(Arrays.asList(shopId), groupId, company.getViewOperationType());
        AssertUtil.assertTrue(!CollectionUtils.isEmpty(viewIds), "查询运营视角失败,请检查");
        return viewIds.get(0);
    }
  • 线下门店列表 缓存60分钟,这种东西一般不会经常变化,且进入首页就要用,
/**
     *
     * @Description  获取线下门店列表,并存入缓存
     * @Param
     * @param groupId
     * @param shopId
     * @return
     */
    @MallCache(value = "offlineShops", expireTime = 60)
    public TableResponse<Record> getOfflineShops(Long groupId,Long shopId){
   
        TableResponse<Record> tableResponse = new TableResponse<>();
        List<Record> offlineShops = WmOfflineShopDao.dao(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值