GoogleGuava-缓存cache简单使用

Maven

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>
public static class Student {
        private int id;
        public String name;
 
        public int getId() {
            return id;
        }
 
        public void setId(int id) {
            this.id = id;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
 
        @Override
        public String toString() {
            return id + "| " + name;
        }
    }

class

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
 
public class guavaCache {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存
        LoadingCache<Integer, Student> studentCache
        // CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例
                = CacheBuilder.newBuilder()
                        // 设置并发级别为8,并发级别是指可以同时写缓存的线程数
                        .concurrencyLevel(8)
                        // 设置写缓存后8秒钟过期
                        .expireAfterWrite(18, TimeUnit.SECONDS)
                        // 设置缓存容器的初始容量为10
                        .initialCapacity(2)
                        // 设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项
                        .maximumSize(2)
                        // 设置要统计缓存的命中率
                        .recordStats()
                        // 设置缓存的移除通知
                        .removalListener(new RemovalListener<Object, Object>() {
                            public void onRemoval(RemovalNotification<Object, Object> notification) {
                                System.out.println(
                                        notification.getKey() + " was removed, cause is " + notification.getCause());
                            }
                        })
                         
                        // build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存
                        .build(new CacheLoader<Integer, Student>() {
                            @Override
                            public Student load(Integer key) throws Exception {
                                System.out.println("load student " + key);
                                Student student = new Student();
                                student.setId(key);
                                student.setName("name " + key);
                                return student;
                            }
                        });
 
		for (int i = 0; i < 20; i++) {
			// 从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据
			Student student = studentCache.get(i);
			System.out.println(student);
			// 休眠1秒
			TimeUnit.SECONDS.sleep(1);
		}
                 
 
        System.out.println("cache stats:");
        // 最后打印缓存的命中率等 情况
        System.out.println(studentCache.stats().toString());

		//测试最大容量LRU算法, 感觉更像是把使用时间最近的保留
		Student student = studentCache.get(1);
		System.out.println(student);
		student = studentCache.get(1);
		System.out.println(student);
		student = studentCache.get(1);
		System.out.println(student);
		 
		student = studentCache.get(2);
		System.out.println(student);
		 
		student = studentCache.get(3);
		System.out.println(student);

    }
 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃醋的工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值