GuavaCache批量优惠券ID接口

//需求:批量优惠券id获取优惠券信息,如果缓存中存在就直接返回。不存在就去DB中去查找,然后把查询结果放到缓存

 

package com.xdclass.couponapp.service;


import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;

import com.xdclass.couponapp.domain.TCoupon;
import com.xdclass.couponapp.domain.TCouponExample;
import com.xdclass.couponapp.mapper.TCouponMapper;
import com.xdclass.userapi.service.IUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CouponService {

     @Resource
     private TCouponMapper tCouponMapper;


    /* @Reference
     private IUserService iUserService;*/

    LoadingCache> couponCache = CacheBuilder.newBuilder()
            .expireAfterWrite(10,TimeUnit.MINUTES).refreshAfterWrite(5,TimeUnit.MINUTES)
            .build(new CacheLoader>() {
                @Override
                public List load(Integer o) throws Exception {
                    return loadCoupon(o);
                }
            });

    //需求:批量优惠券id获取优惠券信息,如果缓存中存在就直接返回。不存在就去DB中去查找,然后把查询结果放到缓存

     LoadingCache couponIdsCache = CacheBuilder.newBuilder()
            .expireAfterWrite(10,TimeUnit.MINUTES).refreshAfterWrite(5,TimeUnit.MINUTES)
            .build(new CacheLoader() {
                @Override
                public TCoupon load(Integer o) throws Exception {
                    return loadIdCoupon(o);
                }
            });

    private TCoupon loadIdCoupon(Integer id) {
        return tCouponMapper.selectByPrimaryKey(id);
    }

    public List getCouponListByIds(String ids){
          String[] idStr= ids.split(",");
          List loadForm = Lists.newArrayList();
          List tcoupons = Lists.newArrayList();
          List idList = Lists.newArrayList(idStr);
          for(String id : idList){
            TCoupon tCoupon = couponIdsCache.getIfPresent(id);
            if(tCoupon==null){
                //缓存没有,先加到集合里。然后去DB查询
                loadForm.add(Integer.parseInt(id));
            }else{
                tcoupons.add(tCoupon);
            }
        }
        //缓存中没有,去DB中查询
        List tCoupons1 = couponByIds(loadForm);
        Map tCouponMap = tCoupons1.stream().collect(Collectors.toMap(TCoupon::getId, Function.identity()));
        //将查询出来的结果放到缓存里
        couponIdsCache.putAll(tCouponMap);
        tcoupons.addAll(tCoupons1);
        return tcoupons;
    }

    private List couponByIds(List ids) {
        TCouponExample tCouponExample = new TCouponExample();
        tCouponExample.createCriteria().andIdIn(ids);
        return tCouponMapper.selectByExample(tCouponExample);
    }

    private List loadCoupon(Integer o) {
        TCouponExample tCouponExample = new TCouponExample();
        tCouponExample.createCriteria().andStartTimeLessThan(new Date())
                .andEndTimeGreaterThan(new Date());
        return  tCouponMapper.selectByExample(tCouponExample);
    };


    /**
     * 获取优惠券列表接口
     * @return
     */
    public List getCouponList(){
        List coupons = new ArrayList<>();
        try {
            coupons = couponCache.get(1);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return  coupons;
    }

   /* public String getUserById(int id){
        return iUserService.getUserById(id).toString();
    }*/
}

你可能感兴趣的:(GuavaCache批量优惠券ID接口)