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"));
        }
    }
    
    
  • 相关阅读:
    通过Math.atan2计算角度 改变物体朝向
    table.sort 排序的问题
    shader 实现正旋波效果 水面波动效果
    第一篇碎碎心得
    ping 整理
    路由器
    C语言里如何读取位数据的某几位?
    ubunut命令
    基于SPIflash 的fatfs调试步骤
    makefile 学习总结--函数
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/12520073.html
Copyright © 2011-2022 走看看