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"));
        }
    }
    
    
  • 相关阅读:
    目标跟踪的评价指标
    [c++] stringstream的用法
    [OpenCV] sift demo
    [TCP/IP] 滑动窗口
    [python] 一行命令搭建http服务内网传文件
    YII 获取系统级请求参数的常用方法
    windows系统npm如何升级自身
    修补--Redis未授权访问漏洞
    MySQL中的两个时间函数,用来做两个时间之间的对比
    Centos 安装mongodb
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/12520073.html
Copyright © 2011-2022 走看看