zoukankan      html  css  js  c++  java
  • Guava Cache:

    个人使用场景: 做三级缓存

    本地缓存实现,支持多种缓存过期策略

    //初始化 Guava内存  LRU算法
    private static LoadingCache<String, String> localCache = CacheBuilder.newBuilder()
                                                                     .initialCapacity(1000)
                                                                     .maximumSize(10000)
                                                                     .expireAfterAccess(12, TimeUnit.HOURS)
                                                                     .build(new CacheLoader<String, String>() {
                                                                         //默认的数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用这个方法进行加载
                                                                         @Override
                                                                         public String load(String s) throws Exception {
                                                                             return "null";
                                                                         }
                                                                     });
    
    public static void setKey(String key, String value) {
        localCache.put(key, value);
    }
    
    public static String getKey(String key) {
        String value = null;
        try {
            value = localCache.get(key);
            if ("null".equals(value)) {
                return null;
            }
        } catch (ExecutionException e) {
            log.error("localCache get error", e);
        }
        return null;
    }
  • 相关阅读:
    leetcode144 longest-palindromic-substring
    数据结构之二叉树
    数据结构之堆
    数据结构之图
    数据结构之排序
    数据结构之动态规划
    14 RPC
    数据结构之字符串
    数据结构之散列表
    16 中间人攻击
  • 原文地址:https://www.cnblogs.com/lshan/p/14475147.html
Copyright © 2011-2022 走看看