Redis统计某个时间段订单数量(用Guava缓存辅助解决)

简介

公司有这么个需求,需用统计店铺某个时间段(按自然天计算,不超过24小时)类的订单数量。因为这些统计数据不用持久化,考虑到性能问题,准备用Redis做统计。
- 设计思路:用Reids的一个有序集合实现。店铺Id作为有序集合key,订单ID作为有序集合member,插入到Redis时间戳作为有序集合的score。增加的时候用zadd(cacheKey, System.currentTimeMillis(), orderId),统计的时候用zcount(cacheKey, beginTimestamp, endTimestamp)统计出某个时间段的订单数量。
- 思考:不能只是想着插入Redis,还必须想着怎么清理老的数据,也就是清理截止到昨天晚上23:59:59的老数据。自然的想法就是每次生成到昨天的时间戳,然后每次插入的时候清理以前的老数据。
- 问题:
- 1.每次生成昨天的时间戳即使不是个耗时操作,也是没有必要的,因为一天只需要生成一次就够了,其它生成都是浪费的。那怎么保证新开始一天重新生成昨天的时间戳呢?
- 2.每次插入的时候清理也是没必要的,只要每天清理一次就行了,因为我们是按自然天保存订单数量的。怎么一天清理一次呢?
- 解决:
总的方案是用guava的缓存,实现类似定时的功能。利用缓存key不存在自动加载;以及Guava的缓存移除触发器,清理Redis中的老数据。
- 问题1:根据我们自身业务情况,没有必要及时处理清理老的数据,只要保证Redis内存不爆掉就行了。晚几个小时甚至晚一天清理一般也不会出问题。所以第一次生成昨天的时间戳可以在本地缓存起来,后续需要的话直接从本地缓存获取就行了,这样没有必要每次都生成这个时间戳,等第二天重新再生成。
- 问题2:更新订单数量的时候,将店铺ID和最近清理的时间戳插入到guava缓存中,插入之前先清理老数据;当guava的缓存中的key被因为过期被清理的时候,触发监听器,再次清理老数据。

guava使用demo

public static void main(String[] args) throws ExecutionException, InterruptedException{
        //缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存
        LoadingCache<Integer,Student> studentCache
                //CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例
                = CacheBuilder.newBuilder()
                //设置并发级别为8,并发级别是指可以同时写缓存的线程数
                .concurrencyLevel(8)
                //设置写缓存后8秒钟过期
                .expireAfterWrite(8, TimeUnit.SECONDS)
                //设置缓存容器的初始容量为10
                .initialCapacity(10)
                //设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项
                .maximumSize(100)
                //设置要统计缓存的命中率
                .recordStats()
                //设置缓存的移除通知
                .removalListener(new RemovalListener<Object, Object>() {
                    @Override
                    public void onRemoval(RemovalNotification<Object, Object> notification) {
                        System.out.println(notification.getKey() + " was removed, cause is " + notification.getCause());
                    }
                })
                //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存
                .build(
                        new CacheLoader<Integer, Student>() {
                            @Override
                            public Student load(Integer key) throws Exception {
                                System.out.println("load student " + key);
                                Student student = new Student();
                                student.setId(key);
                                student.setName("name " + key);
                                return student;
                            }
                        }
                );

源码实现

public class RedisUtil {
    private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
    private RedisUtil() {
    }
    private static final String YESTERDAY = "yesterday";
    private static final RedisExtraService redisExtraService = SpringContext.getBean(RedisExtraService.class);
    //缓存昨天最后时刻的时间戳,一天后会更新这个时间戳
    private static final LoadingCache<String, Long> timeCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(1, TimeUnit.DAYS)
            .build(new CacheLoader<String, Long>() {
                @Override
                public Long load(String key) throws Exception {
                    return DateUtil.getYesterdayEndTime();
                }
            });
    //缓存的key回收时会触发这个监听器
    private static final RemovalListener<String, Long> removalListener = new RemovalListener<String, Long>() {
        @Override
        public void onRemoval(RemovalNotification<String, Long> notification) {
            String key = notification.getKey();
            Long recentNeedRemoveTime = notification.getValue();
            //不等于时才清除老数据,因为等于情况说明创建key的时候已经清理了老数据
            if (!recentNeedRemoveTime.equals(getRecentNeedRemoveTime()))
                redisExtraService.zremrangeByScore(key, 0, recentNeedRemoveTime);
        }
    };
    //缓存待清理的redis中的key
    private static final Cache<String, Long> entityCache = CacheBuilder.newBuilder()
            .maximumSize(10000)
            .expireAfterWrite(1, TimeUnit.DAYS)
            .removalListener(removalListener)
            .build();
    public static void updateOrderCount(final String entityId, final String orderId) {
        checkArgument(entityId != null && orderId != null);
        try {
            entityCache.get(entityId, new Callable<Long>() {
                @Override
                public Long call() throws Exception {
                    //创建key的时候已经清理老数据,并将这时间戳记录下来,回收key的时候需要这个时间戳
                    Long recentNeedRemoveTime = getRecentNeedRemoveTime();
                    redisExtraService.zremrangeByScore(entityId, 0, recentNeedRemoveTime);
                    return recentNeedRemoveTime;
                }
            });
        } catch (ExecutionException e) {
            logger.error("从entityCache清理店铺昨天的订单数量失败, entityId: {}", entityId);
            //再次尝试清理
            redisExtraService.zremrangeByScore(entityId, 0, getRecentNeedRemoveTime());
        }
        String cacheKey = getRedisKey(entityId);
        Jedis jedis = redisExtraService.getResource();
//        long yesterdayEndTime = DateUtil.getYesterdayEndTime();
//        long yesterdayEndTime;
//        try {
//            yesterdayEndTime = timeCache.get(YESTERDAY);
//        } catch (ExecutionException e) {
//            logger.error("从timeCache获取yesterdayEndTime失败");
//            yesterdayEndTime = DateUtil.getYesterdayEndTime();
//        }
        try {
            Pipeline pipeline = jedis.pipelined();
//            pipeline.zremrangeByScore(cacheKey, 0, yesterdayEndTime);
            pipeline.zadd(cacheKey, System.currentTimeMillis(), orderId);
            pipeline.expire(cacheKey, OrderCacheConstant.EXPIRE_DAY);
            pipeline.sync();
        } finally {
            redisExtraService.returnResource(jedis);
        }
    }
    public static Long getOrderCount(String entityId, long beginTimestamp, long endTimestamp) {
        checkArgument(entityId != null);
        String cacheKey = getRedisKey(entityId);
        return redisExtraService.zcount(cacheKey, beginTimestamp, endTimestamp);
    }
    protected static String getRedisKey(String entityId) {
        return OrderCacheConstant.KEY_ORDER_COUNT + entityId;
    }
    /**
     * 获取最近需要删除的时间,一般是昨天最后的时间
     *
     * @return
     */
    private static Long getRecentNeedRemoveTime() {
        Long yesterdayEndTime;
        try {
            yesterdayEndTime = timeCache.get(YESTERDAY);
        } catch (ExecutionException e) {
            logger.error("从timeCache获取yesterdayEndTime失败");
            yesterdayEndTime = DateUtil.getYesterdayEndTime();
        }
        return yesterdayEndTime;
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值