zoukankan      html  css  js  c++  java
  • google guava cache笔记

    前言

    最近看代码看到了缓存,缓存有各种各样的实现,基于自定义map,基于工具的。
    Guava cache是google开源的,api学习很简单。
    我直接贴下常用的api代码

    代码

    package com.learn.java.javabase.jar.guava;
    
    import com.google.common.cache.Cache;
    import com.google.common.cache.CacheBuilder;
    import com.google.common.cache.RemovalListener;
    import com.google.common.cache.RemovalNotification;
    
    public class CacheTest01 {
        public static void main(String[] args) {
            test01();
        }
        private static void test01(){
            Cache <String,String> cache = CacheBuilder.newBuilder().maximumSize(2)
                    .removalListener(new RemovalListener<String, String>() {
                        @Override
                        public void onRemoval(RemovalNotification<String, String> removalNotification) {
                            //移除监听器,监听移除动作
                            System.out.println("remove "+ removalNotification.getKey()+" : "+removalNotification.getValue());
                        }
                    })
                    .build();
    
            //超过maxnum,会删除前面插入的记录,获取没有的key返回的null。
            cache.put("key1","value1");
            cache.put("key2","value2");
            cache.put("key3","value3");
            System.out.println(cache.getIfPresent("key1"));
            System.out.println(cache.getIfPresent("key2"));
            System.out.println(cache.getIfPresent("key3"));
    
            //显式清除
            cache.invalidate("key2");
            System.out.println(cache.getIfPresent("key2"));
        }
    }
    
    
  • 相关阅读:
    serial number
    python getopt
    python readline,seek
    linux scp
    jenkinsapi
    windows kill process
    python time
    python configparse
    解决某些.net不方便解决的问题,解决方法就是 DHTML
    (转)windows XP 系统服务“关闭”详细列表,释放N多内存,128也够用了!
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/12520073.html
Copyright © 2011-2022 走看看