Based on guava achieve local caching

An interface timeout response today, then I optimize, use the local cache.

maven dependence

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>27.1-jre</version>
    </dependency>

 

LocalCache local cache tools

Package com.itbac.common.cache; 

Import java.util.Optional;
 Import java.util.concurrent.TimeUnit;
 Import java.util.function.Supplier; 

Import com.google.common.cache.Cache;
 Import com.google. common.cache.CacheBuilder; 



/ ** 
 * local caching facility 
 * 
 * a call for some very low and frequent changes in frequency, may be combined with locally available short time (1 minute) secondary cache 
 * 
 * @author Bac 
 * 
 * @Date 2020 January 6 pm 12:12:12 
 * / 
public  class LocalCache <K, V> { 

    / ** default local cache maximum data length * / 
    Private  static  Final  Long= Default_Maximum_Size 5_000;
     / ** local cache data length. 1     * / 
    Private  static  Final  Long Single_Size =. 1 ; 

    / ** failure time in seconds after the write to local cache * / 
    Private  static  Final  Long Max_Expire_After_Write * = 60. 1 ; 


    Private the Cache <K, Optional the <V >> cache; 

    Private the LocalCache () {} 

    / ** 
     * Create a local cache object (the object created, the only need to be initialized with static member variable references) 
     * (default is one minute cache) 
     * @ return 
     * / 
    public  static <K, V> the LocalCache <K, V> Create () { 
        the LocalCache<K, V> = local new new the LocalCache <K, V> (); 

        local.cache =   CacheBuilder.newBuilder () 
                .maximumSize (Default_Maximum_Size) 
                .expireAfterWrite (Max_Expire_After_Write, TimeUnit.SECONDS) 
                .build (); 
        return local; 
    } 

    / ** 
     * Create a local cache object 
     * @param after expireAfterWrite write cache invalidation seconds 
     * @return 
     * / 
    public  static <K, V> the LocalCache <K, V> Create ( Long expireAfterWrite) { 
        the LocalCache <K, V> local = new new the LocalCache <K, V>(); 

        Local.cache =   CacheBuilder.newBuilder () 
                .maximumSize (Default_Maximum_Size) 
                .expireAfterWrite (expireAfterWrite <= 0?   Max_Expire_After_Write: expireAfterWrite, TimeUnit.SECONDS) 
                .build (); 
        return local; 
    } 

    / ** 
     * Create a cache only local cache data element object 
     * @return 
     * / 
    public  static <K, V> the LocalCache <K, V> createSingleSize () { 
        the LocalCache <K, V> = local new new the LocalCache <K, V> (); 

        local.cache =   CacheBuilder.newBuilder ()
                .maximumSize (Single_Size) 
                .expireAfterWrite (Max_Expire_After_Write, TimeUnit.SECONDS) 
                .build (); 
        return local; 
    } 



    Private the Cache <K, Optional The <V >> the getCache () {
         return  the this .cache; 
    } 

    / ** 
     * Get cache k value corresponding to the value returned is not null 
     * @param K cache key 
     * @return 
     * / 
    public Optional the <V> GET (K K) {
         return the getCache () getIfPresent (K);. 
    } 


    / ** 
     * Get the cache key corresponding to value, if the key is not in the local cache, a call to initialize 
     * @paramk cache key 
     * @param call if the key is not in the local cache, a call to initialize. 
     * Available Lambada expression, () -> {specific logic, V} return; 
     * @return 
     * / 
    public V GET (K K, Supplier <V> Call) {
         IF (K == null ) {
             return  null ; 
        } 
        / ** 
         * the Callable Interface Guava's, the use of outdated mechanisms 
         * If the built-in Callable returns null, get (xx, CallAble) will throw an exception: the CacheLoader returned null for Key 
         * so the use of Optional + extra Supplier 
         * / 
        Optional The <V> value = GET (K);
         // not placed local data cache 
        if (value == null) {
            V v =  call.get();
            getCache().put(k, Optional.ofNullable(v));
            return v;
        }
        return value.orElse(null);
    }
}

Use the local cache

Package com.itbac.common.cache; 

Import of java.util.ArrayList;
 Import java.util.List; 


public  class SkuQueryService { 

    // local cache 20 seconds after writing failure 
    Private  static  Final the LocalCache <String, List <String >> = LocalCache.create the cache (20 is ); 

    public  static  void main (String [] args) { 

        // use the local cache 
        List <String> = cache.get List ( "Key", () -> {
             // specific data acquired logic 
            List <String> strings = new new the ArrayList <> (); 
            strings.add ( "pretend to have data" );
            return strings;
        });

    }
}

After setting the cache to wait until the time, the cache will be invalidated. We need to tolerate inconsistent data over time.

Guess you like

Origin www.cnblogs.com/itbac/p/12159157.html
Recommended