zoukankan      html  css  js  c++  java
  • guava cache

    缓存是提高性能的一把利器。
    常用到的缓存技术有分布式缓存,像Redis、MC;也有本地缓存,像ehcache、guava cache等。这里说的是本地缓存guava cache。

    guava cache刚开始接触,这就记录下来。。

        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(8, TimeUnit.SECONDS)
              //设置写缓存后1秒钟刷新
             .refreshAfterWrite(1, TimeUnit. SECONDS)                
    //设置缓存容器的初始容量为10                 .initialCapacity(10)                 //设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项                 .maximumSize(100)                 //设置要统计缓存的命中率                 .recordStats()                 //设置缓存的移除通知                 .removalListener(new RemovalListener<Object, Object>() {                     @Override                     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(1);             System.out.println(student);             //休眠1秒             TimeUnit.SECONDS.sleep(1);         }         System.out.println("cache stats:");         //最后打印缓存的命中率等 情况         System.out.println(studentCache.stats().toString());     }

    还有另一种方法

    package com;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.TimeUnit;
    
    import com.google.common.cache.*;
    
    /** 
     * @author  作者 PZhang  E-mail:pzhang@rxhui.com 
     * @date 创建时间:2017-2-15 上午9:58:00 
     * @version 1.0 
     * @parameter   
     * @return  
     */
    public class CacheModel {
        public Student getStudent(Integer key){
            System.out.println("load student " + key);
            Student student = new Student();
            student.setId(key);
            student.setName("name " + key);
            return student;
        }
        //load Method
        public void loadCacheA() throws Exception{
             LoadingCache<Integer,Student> studentCache= CacheBuilder.newBuilder().concurrencyLevel(8).
                     expireAfterWrite(8, TimeUnit.SECONDS).refreshAfterWrite(1, TimeUnit. SECONDS).initialCapacity(10).maximumSize(100)
                     .recordStats().removalListener(new RemovalListener<Object, Object>() {
                         public void onRemoval(RemovalNotification<Object, Object> notification) {
                                     System.out.println(notification.getKey() + " was removed, cause is " + notification);}
                         }).build(
                        new CacheLoader<Integer, Student>() {
                            @Override
                            public Student load(Integer key) throws Exception {
                                return getStudent(key);
                               }
                            }
              );
    
             for (int i=0;i<20;i++) {
               Student student = studentCache.get(1);
               System.out.println(student);
               TimeUnit.SECONDS.sleep(1);
             }
    
             System.out.println("cache stats:");
             System.out.println(studentCache.stats().toString());
             
        }
        //call back Method
        public void loadCacheB(final Integer key) throws Exception{
             Cache<Integer, Student> cache = CacheBuilder.newBuilder().maximumSize(1000).recordStats().expireAfterWrite(8, TimeUnit.SECONDS).build();  
                 
             for (int i=0;i<20;i++) {
                    System.out.println(cache.get(key, new Callable<Student>() {  
                         public Student call() {  
                            return getStudent(key);
                        }  
                    }));
                    TimeUnit.SECONDS.sleep(1);
                  }
    
                  System.out.println("cache stats:");
                  System.out.println(cache.stats().toString());
        }
        
        public static void main(String[] args) throws Exception {
            CacheModel cache = new CacheModel();
            cache.loadCacheB(2);
        }
        
        
        
    }
     

      guava Cache数据移除:

      guava做cache时候数据的移除方式,在guava中数据的移除分为被动移除和主动移除两种。
      被动移除数据的方式,guava默认提供了三种方式:
      1.基于大小的移除:看字面意思就知道就是按照缓存的大小来移除,如果即将到达指定的大小,那就会把不常用的键值对从cache中移除。
      定义的方式一般为 CacheBuilder.maximumSize(long),还有一种一种可以算权重的方法,个人认为实际使用中不太用到。就这个常用的来看有几个注意点,
        其一,这个size指的是cache中的条目数,不是内存大小或是其他;
        其二,并不是完全到了指定的size系统才开始移除不常用的数据的,而是接近这个size的时候系统就会开始做移除的动作;
        其三,如果一个键值对已经从缓存中被移除了,你再次请求访问的时候,如果cachebuild是使用cacheloader方式的,那依然还是会从cacheloader中再取一次值,如果这样还没有,就会抛出异常
      2.基于时间的移除:guava提供了两个基于时间移除的方法
        expireAfterAccess(long, TimeUnit)  这个方法是根据某个键值对最后一次访问之后多少时间后移除
        expireAfterWrite(long, TimeUnit)  这个方法是根据某个键值对被创建或值被替换后多少时间移除
      3.基于引用的移除:
      这种移除方式主要是基于java的垃圾回收机制,根据键或者值的引用关系决定移除
      主动移除数据方式,主动移除有三种方法:
      1.单独移除用 Cache.invalidate(key)
      2.批量移除用 Cache.invalidateAll(keys)
      3.移除所有用 Cache.invalidateAll()
      如果需要在移除数据的时候有所动作还可以定义Removal Listener,但是有点需要注意的是默认Removal Listener中的行为是和移除动作同步执行的,如果需要改成异步形式,可以考虑使用RemovalListeners.asynchronous(RemovalListener, Executor)

       

  • 相关阅读:
    只要有梦想
    过去这一年
    Importing BizTalk Applications to Production Environment
    SQL Server 2005 – Database Master Key
    Check Page Rank of any web site pages instantly
    An Introduction to SQL Server Service Broker [WORD DOCUMENT]
    Svcutil.exe – Generate the proxy class for the WCF client application
    SQL Server 2005: how to add a linked server
    BizTalk Error: The published message could not be routed
    Enable routing for failed messages in BizTalk 2006
  • 原文地址:https://www.cnblogs.com/bestzhang/p/6400603.html
Copyright © 2011-2022 走看看