package com.hh;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class LocalUtil {
    private static Cache<String,Integer> demoCache= CacheBuilder.newBuilder().
            expireAfterWrite(10, TimeUnit.SECONDS).recordStats().build();

    public static Integer getKey(String key, Callable<Integer> callable) throws ExecutionException {
        Integer integer = demoCache.get(key, callable);
        return integer;
    }
}
package com.hh;

import com.google.common.cache.CacheBuilder;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

public class Test05 {
    public static void main(String[] args) throws ExecutionException {
        Callable<Integer> callable= new Callable<Integer>() {
            public Integer call() throws Exception {
                System.out.println("hello");
                return 10;
            }
        };

        System.out.println(LocalUtil.getKey("1",callable));
        System.out.println(LocalUtil.getKey("1",callable));
    }
}
package com.sankuai.grocerywms.logistics.gray.grocery.service.utils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sankuai.grocerywms.logistics.gray.grocerygraycenterclient.response.GrayBatchDTO;
import com.sankuai.grocerywms.logistics.gray.grocerygraycenterclient.response.GrayPlanApiDTO;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class LocalCacheUtils {


    public static void test() throws Exception {
        LoadingCache<String, Integer> cache = CacheBuilder.newBuilder()
                .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                .recordStats()//开启,记录状态数据功能
                .build(new CacheLoader<String, Integer>() {
                    //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                    @Override
                    public Integer load(String key) throws Exception {
                        // TODO Auto-generated method stub
                        return -1;
                    }
                });
        cache.put("key1", 1);
        System.out.println(cache.get("key1"));
        Thread.sleep(5000);
        System.out.println(cache.get("key1"));
        Thread.sleep(5000);
        System.out.println(cache.get("key1"));
    }

    /**
     * 从缓存中获取灰度信息列表
     *
     * @param listGrayPlanIds
     * @param listResponse
     * @return
     * @throws Exception
     */
    public static List<GrayPlanApiDTO> getGranPlanList(List<Long> listGrayPlanIds, List<GrayPlanApiDTO> listResponse) throws Exception {
        List<GrayPlanApiDTO> grayPlanApiDTOList = Collections.emptyList();
        if (listGrayPlanIds.size() > 0) {
            String key = StringNewUtils.listToString(listGrayPlanIds, ',');
            // 从缓存里取,如果取不到从库里取,然后放入缓存中
            LoadingCache<String, List<GrayPlanApiDTO>> cache = CacheBuilder.newBuilder()
                    .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                    .recordStats()//开启,记录状态数据功能
                    .build(new CacheLoader<String, List<GrayPlanApiDTO>>() {
                        //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                        @Override
                        public List<GrayPlanApiDTO> load(String key) throws Exception {
                            // 从数据库里取,根据list读取plan,batch,config前端,后端,终端
                            return listResponse;
                        }
                    });
            grayPlanApiDTOList = cache.get(key);
        } else {
            // todo 缓存所有有效的灰度信息
        }
        return grayPlanApiDTOList;
    }

    /**
     * 缓存操作对象-根据需求获取灰度信息
     */
    private static LoadingCache<String, List<GrayPlanApiDTO>> GLOBAL_CACHE = null;

    /**
     * 配置
     */
    private static LoadingCache<String, List<GrayBatchDTO>> GRAYBATCH_CACHE = null;

    /**
     * 配置-中的featureId
     */
    private static LoadingCache<String, List<String>> GRAYFEATUREID_CACHE = null;

    /**
     * 配置-中的featureId
     */
    private static LoadingCache<String, Long> GRAYDIMID_CACHE = null;


    static {
        List<GrayPlanApiDTO> grayPlanApiDTOList = Collections.emptyList();
        String key = "default";
        // 从缓存里取,如果取不到从库里取,然后放入缓存中
        GLOBAL_CACHE = CacheBuilder.newBuilder()
                .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                .recordStats()//开启,记录状态数据功能
                .build(new CacheLoader<String, List<GrayPlanApiDTO>>() {
                    //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                    @Override
                    public List<GrayPlanApiDTO> load(String key) throws Exception {
                        // 从数据库里取,根据list读取plan,batch,config前端,后端,终端
                        return Collections.emptyList();
                    }
                });

        // 灰度配置
        // 从缓存里取,如果取不到从库里取,然后放入缓存中
        GRAYBATCH_CACHE = CacheBuilder.newBuilder()
                .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                .recordStats()//开启,记录状态数据功能
                .build(new CacheLoader<String, List<GrayBatchDTO>>() {
                    //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                    @Override
                    public List<GrayBatchDTO> load(String key) throws Exception {
                        // 从数据库里取,根据list读取plan,batch,config前端,后端,终端
                        return Collections.emptyList();
                    }
                });

        GRAYFEATUREID_CACHE = CacheBuilder.newBuilder()
                .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                .recordStats()//开启,记录状态数据功能
                .build(new CacheLoader<String, List<String>>() {
                    //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                    @Override
                    public List<String> load(String key) throws Exception {
                        // 从数据库里取,根据list读取plan,batch,config前端,后端,终端
                        return Collections.emptyList();
                    }
                });

        GRAYDIMID_CACHE = CacheBuilder.newBuilder()
                .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                .recordStats()//开启,记录状态数据功能
                .build(new CacheLoader<String, Long>() {
                    //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                    @Override
                    public Long load(String key) throws Exception {
                        // 从数据库里取,根据list读取plan,batch,config前端,后端,终端
                        return -1L;
                    }
                });

    }

    /**
     * 获取缓存
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static List<GrayPlanApiDTO> getKeyValue(String key) throws Exception {
        return GLOBAL_CACHE.get(key);
    }

    /**
     * 更新缓存
     *
     * @param key
     * @param listResponse
     * @return
     * @throws Exception
     */
    public static void pushKeyValue(String key, List<GrayPlanApiDTO> listResponse) throws Exception {
        GLOBAL_CACHE.put(key,listResponse);
    }

    /**
     * 获取缓存
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static List<GrayBatchDTO> getKeyBatchValue(String key) throws Exception {
        return GRAYBATCH_CACHE.get(key);
    }

    /**
     * 更新缓存
     *
     * @param key
     * @param listResponse
     * @return
     * @throws Exception
     */
    public static void pushKeyBatchValue(String key, List<GrayBatchDTO> listResponse) throws Exception {
        GRAYBATCH_CACHE.put(key,listResponse);
    }

    /**
     * 获取缓存
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static List<String> getKeyFeatureValue(String key) throws Exception {
        return GRAYFEATUREID_CACHE.get(key);
    }

    /**
     * 更新缓存
     *
     * @param key
     * @param listResponse
     * @return
     * @throws Exception
     */
    public static void pushKeyFeatureValue(String key, List<String> listResponse) throws Exception {
        GRAYFEATUREID_CACHE.put(key,listResponse);
    }

    /**
     * 获取缓存
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static Long getKeyDimValue(String key) throws Exception {
        return GRAYDIMID_CACHE.get(key);
    }

    /**
     * 更新缓存
     *
     * @param key
     * @param listResponse
     * @return
     * @throws Exception
     */
    public static void pushKeyDimValue(String key, Long listResponse) throws Exception {
        GRAYDIMID_CACHE.put(key,listResponse);
    }
}
package com.sankuai.grocerywms.logistics.gray.grocery.service.utils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sankuai.grocerywms.logistics.gray.grocerygraycenterclient.response.GrayBatchDTO;
import com.sankuai.grocerywms.logistics.gray.grocerygraycenterclient.response.GrayPlanApiDTO;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class LocalCacheNewUtils {






    /**
     * 缓存操作对象-根据需求获取灰度信息
     */
    private static LoadingCache<String, Object> GLOBAL_CACHE = null;




    static {
        String key = "default";
        // 从缓存里取,如果取不到从库里取,然后放入缓存中
        GLOBAL_CACHE = CacheBuilder.newBuilder()
                .expireAfterWrite(60, TimeUnit.SECONDS)//缓存10秒,10秒之后进行回收
                .recordStats()//开启,记录状态数据功能
                .build(new CacheLoader<String, Object>() {
                    //数据加载,默认返回-1,也可以是查询操作,如从DB查询
                    @Override
                    public Object load(String key) throws Exception {
                        // 从数据库里取,根据list读取plan,batch,config前端,后端,终端
                        return "";
                    }
                });



    }

    /**
     * 获取缓存
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static Object getKeyValue(String key) throws Exception {
        return GLOBAL_CACHE.get(key);
    }

    /**
     * 更新缓存
     *
     * @param key
     * @param listResponse
     * @return
     * @throws Exception
     */
    public static void pushKeyValue(String key, Object listResponse) throws Exception {
        GLOBAL_CACHE.put(key,listResponse);
    }


}

 

posted on 2021-01-09 08:06  王洪洪  阅读(271)  评论(0编辑  收藏  举报