Skip to content

Guava大全

CoderDream edited this page Apr 29, 2022 · 10 revisions

源码研究

  1. 深入Guava Cache的refresh和expire刷新机制
  2. 源码分析:Guava Cache原理以及源码分析
  3. Guava Cache源码详解
  4. Caffeine源码解读-缓存过期淘汰相关算法
  5. Guava Cache (一)之入门使用
  6. Guava Cache (二)之淘汰删除策略
  7. Guava Cache内存缓存使用实践-定时异步刷新及简单抽象封装
  8. Guava Cache:缓存的回收、刷新和统计
  9. Guava Cache 数据变化实现回调的监听器RemovalListener
  10. Guava | 缓存 与 Redis
  11. Guava LoadingCache用法
  12. GuavaCache简介(一)_done_20220428

布隆过滤器

  1. Google Guava 中布隆过滤器的介绍和使用
  2. 布隆过滤器误判怎么办为什么会_最牛一篇布隆过滤器详解,布隆过滤器使用原理...
/**
 * 预计要插入多少数据
 */
private static int size = 1000000;

/**
 * 期望的误判率
 */
private static double fpp = 0.01;

/**
 * 布隆过滤器
 */
private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size, fpp);

@Test
public void test5() {
    // 插入10万样本数据
    for (int i = 0; i < size; i++) {
        bloomFilter.put(i);
    }
    // 用另外十万测试数据,测试误判率
    int count = 0;
    for (int i = size; i < size + 100000; i++) {
        if (bloomFilter.mightContain(i)) {
            count++;
            System.out.println(i + "误判了");
        }
    }
    System.out.println("总共的误判数:" + count);
}

GuavaCache简介(一)

通过分析发现:
(1)缓存项<1,"Hi">的存活时间是5秒,但经过5秒后并没有被清除,因为还是size=1
(2)发生写操作cache.put(2, "bbb")后,缓存项<1,"Hi">被清除,因为size=1,而不是size=2
(3)发生读操作cache.getIfPresent(1)后,缓存项<2,"bbb">没有被清除,因为还是size=1,看来读操作确实不一定会发生清除
(4)发生读操作cache.getIfPresent(2)后,缓存项<2,"bbb">被清除,因为读的key就是2
package com.example.demo.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;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class GuavaCacheServiceTest {
    // 设置cache中的数据在写入之后的存活时间为5秒
    static Cache<Integer, String> cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build();
    //static Cache<Integer, String> cache = CacheBuilder.newBuilder().initialCapacity(10).expireAfterWrite(50, TimeUnit.SECONDS).build();

    @Test
    public void test1() throws Exception {
        // 监控 Cache 大小,2秒刷新一次
        new Thread() {
            public void run() {
                while (true) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                    System.out.println(simpleDateFormat.format(new Date()) + " size: " + cache.size());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();;
                    }
                }
            };
        }.start();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        cache.put(1, "Hi");
        System.out.println("write key:1, value: " + cache.getIfPresent(1));

        // 休眠10秒
        Thread.sleep(10000);
        // when write, key:1 clear -》10秒过后,有新的数据插入,数据1才在这里清除
        cache.put(2, "bbb");
        System.out.println("write key:2, value: " + cache.getIfPresent(2));

        // 休眠10秒
        Thread.sleep(10000);
        // when read other key, key:2 do not clear 
        System.out.println(simpleDateFormat.format(new Date()) +  " after write key:1, value: " + cache.getIfPresent(1));
        Thread.sleep(2000);
        // when read same key, key:2 clear
        System.out.println(simpleDateFormat.format(new Date()) +  " final key:2, value: " + cache.getIfPresent(2));
    }

}
  • 输出
com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.example.demo.guava.GuavaCacheServiceTest,test1
11:39:26 size: 0
write key:1, value: Hi
11:39:28 size: 1
11:39:30 size: 1
11:39:32 size: 1
11:39:34 size: 1
write key:2, value: bbb
11:39:36 size: 1
11:39:38 size: 1
11:39:40 size: 1
11:39:42 size: 1
11:39:44 size: 1
11:39:46after write key:1, value: null
11:39:46 size: 1
11:39:48final key:2, value: null

Process finished with exit code 0
Clone this wiki locally