zoukankan      html  css  js  c++  java
  • Guava学习

    转载:Guava

    https://www.yiibai.com/guava/guava_caching_utilities.html

    转载:

    https://www.concretepage.com/google-api/google-guava-cache-example-using-loadingcache-cachebuilder-and-cacheloader

    Google Guava Cache Example using LoadingCache, CacheBuilder and CacheLoader

    By Arvind Rai, November 13, 2015

    This page will walk through Google Guava cache example using LoadingCache, CacheBuilder and CacheLoader. Guava cache stores key and object like ConcurrentHashMap. Guava cache is thread safe. The feature provided by Guava cache is basically same as ConcurrentHashMap but Guava cache is more preferable than ConcurrentHashMap in terms of cache optimization. Find some features of Guava cache. 

    1. We can define maximum capacity of cache to store key/object. 
    2. We can set expiry time of cache. 
    3. We can configure listener that listens any removal of cache element. 
    4. We can configure concurrency level. 

    Find the code snippet for using Guava cache.

    LoadingCache<Integer, Employee> empCache = CacheBuilder.newBuilder()
           .maximumSize(100)
           .expireAfterWrite(10, TimeUnit.MINUTES)
           .build(
               new CacheLoader<Integer, Employee>() {
    		@Override
    		public Employee load(Integer id) throws Exception {
    			return getEmployeeById(id);
    		}
               }
           );

    LoadingCache<K,V>

    K: Base key type. 
    V: Base value type. 

    com.google.common.cache.LoadingCache loads data in cache automatically. Data remains in cache until it is evicted or cache is invalidated. LoadingCache is thread safe. This class provides different methods to access data for the given key. Some methods of this class are get(K key), asMap(), getUnchecked(K key), refresh(K key).

     

    CacheBuilder<K,V>

    com.google.common.cache.CacheBuilder is the builder for LoadingCache. Using CacheBuilder, we can configure cache settings. Find some methods of this class. 

    concurrencyLevel(int concurrencyLevel) : It sets concurrency level among update operations. 
    expireAfterAccess(long duration, TimeUnit unit) : We set the time to expire and once time is up, cache is expired after most resent access. 
    expireAfterWrite(long duration, TimeUnit unit) : Cache is expired in a given duration after most resent write. 
    maximumSize(long size) : Maximum size of entries that cache can store. 
    removalListener(RemovalListener<? super K1,? super V1> listener) : Sets the listener that listens when any entry is removed.

    CacheLoader<K,V>

    com.google.common.cache.CacheLoader loads values for the given key and is cached with LoadingCache. We need to override load(K key) that returns the object to be cached. Within load(K key) method, we call our method which output needs to be cached. Normally we need to cache the output of the methods that performs some expensive calculation or database access which is being retrieved frequently.

    Complete Example


    build.gradle

    apply plugin: 'java'
    apply plugin: 'eclipse'
    archivesBaseName = 'GoogleGuava'
    version = '1' 
    repositories {
        mavenCentral()
    }
    dependencies {
        compile 'com.google.guava:guava:19.0-rc2'
    }


    EmployeeGuavaCacheUtil.java

    package com.concretepage;
    import java.util.concurrent.TimeUnit;
    import com.google.common.cache.CacheBuilder;
    import com.google.common.cache.CacheLoader;
    import com.google.common.cache.LoadingCache;
    public class EmployeeGuavaCacheUtil {
        private static LoadingCache<Integer, Employee> empCache;
        static {
    	empCache = CacheBuilder.newBuilder()
    	       .maximumSize(100)
    	       .expireAfterWrite(10, TimeUnit.MINUTES)
    	       .build(
    	           new CacheLoader<Integer, Employee>() {
            		@Override
    			public Employee load(Integer id) throws Exception {
    				return getEmployeeById(id);
    			}
    	           }
    	       );
        }
        public static LoadingCache<Integer, Employee> getLoadingCache() {
    	return empCache;
        }
        public static Employee getEmployeeById(int id) {
    	System.out.println("--Executing getEmployeeById--");
    	//Perform any expensive task like fetching data from Database.
    	//For the demo we are performing a simple task
    	Employee emp1 = new Employee(1, "Ramesh");
    	Employee emp2 = new Employee(2, "Mohan");
    	if(id == 1 ) {
    	   return emp1;
    	} else {
    	   return emp2;
    	}
       }
    }
    class Employee {
    	private int id;
    	private String name;
    	public Employee(int id, String name) {
    		this.id = id;
    		this.name = 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;
    	}
    }


    CacheDemo.java

    package com.concretepage;
    import java.util.concurrent.ExecutionException;
    import com.google.common.cache.LoadingCache;
    public class CacheDemo {
      public static void main(String[] args) {
    	CacheDemo cacheDemo = new CacheDemo();
    	try {
    		//Access employee first time with id 1, getEmployeeById() will be called.
    		System.out.println(cacheDemo.getEmpUsingGuava(1).getName());
    		System.out.println("------------------------");
            	//Access employee second time with id 1, getEmployeeById() will be not called.
    		//Data is cached.
    		System.out.println(cacheDemo.getEmpUsingGuava(1).getName());
    	} catch (ExecutionException e) {
    		e.printStackTrace();
    	}
      }
      private Employee getEmpUsingGuava(int id) throws ExecutionException {
    	LoadingCache<Integer, Employee> empCache = EmployeeGuavaCacheUtil.getLoadingCache();
    	//System.out.println(empCache.stats());
    	System.out.println("Cache Size:" + empCache.size());
    	return empCache.get(id);
      }
    }

    Find the output.

    Cache Size:0
    --Executing getEmployeeById--
    Ramesh
    ------------------------
    Cache Size:1
    Ramesh

    Download Source Code

    相关学习:

    http://www.360doc.com/content/14/1219/00/9437165_434023468.shtml

    https://segmentfault.com/a/1190000007300118

  • 相关阅读:
    [BJOI2006]狼抓兔子
    [HNOI2016]最小公倍数
    hihocoder 1419 重复旋律4
    [NOI2015]品酒大会
    [SDOI2016]生成魔咒
    [ZJOI2009]狼和羊的故事
    BZOJ4361 isn
    [SDOI2009]虔诚的墓主人
    BZOJ 3329 Xorequ
    [ZJOI2013]丽洁体
  • 原文地址:https://www.cnblogs.com/mybatis/p/9360899.html
Copyright © 2011-2022 走看看