Guava Cacha mechanism and source code analysis

Source: Internet
Author: User
Tags configuration settings garbage collection numeric value object object

Guava is Google's Open source Java Common Toolset Library, including collections, caches, concurrency, strings, I/O operations and so on in the Java development process often need to implement the tool class.

In the normal development process, many situations need to use caching to avoid frequent SQL queries or other time-consuming operations, which will take the results of caching these operations to the next request. If the result of our operation is always unchanged, we can use Concurrenthashmap to store the data, but if these results change over time or if we want the data to be stored in a controlled space, we need to implement this structure ourselves.

Obviously, for this very common requirement, guava provides its own tool class implementation. Guava cache provides almost all of the features we need to use caching, mainly:

* Automatically load the entry node into the cache structure;

* Use the LRU algorithm to remove some data when the cached data has exceeded the pre-set maximum value;

* The expiration mechanism is calculated based on the time the entry node was last accessed or written;

The cached key is encapsulated within the ' weakreference ' reference;

* The cached value is encapsulated in ' weakreference ' or ' softreference ' references;

* Remove entry node to trigger listener notification event ;

* Statistics of hits/outliers/misses during cache usage.

In addition, the core data structure of the guava Cache is roughly the same as the concurrenthashmap, and there are some differences in specifics. Functionally, Concurrentmap will save all added elements until explicitly removed. In contrast, the guava Cache is usually set to automatically reclaim elements in order to limit memory consumption. In some scenarios, although it does not reclaim elements, it is also useful because it automatically loads the cache. Guava Cache Introduction

Before introducing the guava Cache, it is necessary to introduce the following official recommended usage scenarios:

* Willing to consume some memory space to increase speed;

* can expect some keys to be queried more than once;

* The total amount of data stored in the cache will not exceed the memory capacity (' Guava cache ' is a local cache when a single app is running).

Guava cache is definitely the first recommended tool class in the local cache class library, regardless of performance or availability. It provides the builder mode of the Cachebuilder generator to create caches in a way that is very handy, and the configuration settings of each cache parameter, similar to the writing of functional programming, are also particularly good.

Guava Cache's Official document address: http://code.google.com/p/guava-libraries/wiki/CachesExplained. This document provides a detailed description of the Cache.

Tips: In the official documentation, mention three ways to load <key,value> into the cache. respectively:

Loadingcache when building the cache, use the build method to internally invoke the Cacheloader method to load the data;

When using the Get method, if the cache does not exist the key or the key expires, then call get (K, callable<v>) to load the data, using a rude direct way, directly want to cache the put data.

It should be noted that if the value cannot be computed quickly by key, it is still not necessary to directly invoke Cacheloader to load the data into the cache at initialization time. 2.1 Guava Cache Use Example

Import Java.util.Date;
Import java.util.concurrent.Callable;

Import Java.util.concurrent.TimeUnit;
Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;
Import Com.google.common.cache.Cache;
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; /** * @author tao.ke date:14-12-20 Time: PM 1:55 * @version \ $Id $ */public class Cachesample {private static fin

    Al Logger Logger = Loggerfactory.getlogger (Cachesample.class);
            Callable in the form of Cache private static final cache<string, string> callable_cache = Cachebuilder.newbuilder () . Expireafterwrite (1, timeunit.seconds). MaximumSize (+). Recordstats (). Removallistener (New Removalliste Ner<object, object> () {@Override public void Onremoval (REMOVALNOTIFICATION&LT;OBJEC T, object> Notification) {Logger.info ("Remove a map entry which key is {},value are {},cause is {}.", Notificati
                On.getkey (), Notification.getvalue (), Notification.getcause (). name ());

    }}). Build (); Cacheloader in the form of CACHE private static final loadingcache<string, string> loader_cache = Cachebuilder.newbuilder ( ). expireafteraccess (1, timeunit.seconds). MaximumSize (+). Recordstats (). Build (new cacheloader<string, STR
                    Ing> () {@Override Public String Load (string key) throws Exception {
                Return key + new Date ();

    }
            });
        public static void Main (string[] args) throws Exception {int times = 4;

            while (times--> 0) {thread.sleep (900);
                String valuecallable = callable_cache.get ("Key", new Callable<string> () {@Override Public String Call() throws Exception {return "key" + new Date ();

            }
            });
            Logger.info ("Callable Cache----->>>>> key is {},value is {}", "Key", valuecallable); Logger.info ("Callable Cache----->>>>> stat Miss:{},stat hit:{}", Callable_cache.stats (). Missrate (),

            Callable_cache.stats (). Hitrate ());

            String Valueloader = loader_cache.get ("key");
            Logger.info ("Loader Cache----->>>>> key is {},value is {}", "Key", Valueloader); Logger.info ("Loader Cache----->>>>> stat Miss:{},stat hit:{}", Loader_cache.stats (). Missrate (),

        Loader_cache.stats (). Hitrate ()); }

    }
}

The above code, simply describes the use of the guava cache, gives two ways to load the build cache. Among the methods provided by Guava Cache, Recordstats and Removallistener are two very interesting interfaces that can help us to complete statistical functions and entry to remove the listener triggering function.

In addition, although rich features are provided in the external method interface of the guava Cache, it is not recommended to set these properties if we do not need them in the actual code because they will take up extra memory and will be more computationally intensive. Guava Cache Analysis of pre-built knowledge

Guava cache is the idea of Java Concurrenthashmap to implement a local cache, but its internal code implementation, there are a lot of very exciting design implementation, and if the Concurrenthashmap Internal implementation is not very clear, by reading the implementation of the Cache, the implementation of CONCURRENTHASHMAP will basically have a comprehensive understanding. 3.1 Builder Mode

The builder mode of the design pattern is used in many places in the guava. Builder mode is the separation of the construction of a complex object from its corresponding configuration property, that is, you can use the basic same construction procedure to create different concrete objects.

Typical structure diagrams for the builder pattern are:

Builder: Abstract interface for creating individual parts of a product object;

ConcreteBuilder: The concrete builder, which is responsible for the real production;

Director: directing, building the executor, it is responsible for issuing orders;

Product: End-Consumer Products

The interactions between the various types are shown below:

The key to the builder pattern is that the Director object does not return objects directly, but rather creates the objects by (BUILDPARTA,BUILDPARTB,BUILDPARTC) step-by-step. Of course, here the Director can provide a default interface for returning objects (that is, the creation of a generic complex object that returns a parameter that is not specified or uniquely specified in Buildpart).

Tips: In the second edition of effective Java, Josh Bloch mentioned in the second chapter that using the builder pattern to handle constructors that require a lot of parameters. Not only does he demonstrate the use of builder, but it also describes the benefits of this approach relative to the use of constructors with many parameters.

The advantages and disadvantages of using the builder schema to construct an object (the amount of code increase) are obvious.

Import Org.apache.commons.lang3.builder.ToStringBuilder;

Import Org.apache.commons.lang3.builder.ToStringStyle;
     /** * @author tao.ke date:14-12-22 Time: PM 8:57 * @version \ $Id $ */public class Builderpattern {/** * name

    */private String name;

    /** * Age */private int;

    /** * Gender * */private Gender Gender;
    public static Builderpattern Newbuilder () {return new Builderpattern ();
        } public Builderpattern SetName (String name) {this.name = name;
    return this;
        } public Builderpattern Setage (int.) {this.age = age;
    return this;
        } public Builderpattern Setgender (Gender Gender) {this.gender = Gender;
    return this; } @Override Public String toString () {return tostringbuilder.reflectiontostring (this, tostringstyle.shor
    T_prefix_style); } enum Gender {MALE, FEMALE} public static void Main (string[] ArGS) {Builderpattern bp = Builderpattern.newbuilder (). Setage () setName ("Zhangsan"). Setgender (Gender.female);
    System.out.println (Bp.tostring ()); }

}
3.2 Java Object reference

Object reference before you need to look at the object's access location.

When the virtual machine executes, when it encounters a new directive, it first checks to see if the directive has a symbolic reference to the class in the constant pool, and checks whether the corresponding class for the symbol reference has been loaded, parsed, and initialized. If not, then the appropriate class loading process is performed.

The virtual machine then allocates memory for the new object. Virtual machines are allocated according to the garbage collector rules we configure, including how the pointer collision is allocated and how the free list is allocated.

After the memory allocation is complete, the Init method is started. The Init method is initialized according to the code's specified procedure and assigns values to some class properties.

Then, we need to access this object, what to do. In the Java Runtime Memory area model, a thread has a virtual machine stack that has a local method table, which stores a reference address, as shown in the following figure (the Hotspot virtual machine takes this approach, and there is another form here):

Prior to JDK 1.2, the definition of reference in Java was: if the numeric value stored in the data of the reference type represents the starting address of another piece of memory, this memory is referred to as a reference. This definition indicates that there are only two types of objects: the object being referenced and the object that is not referenced. This approach is not good for garbage collection GC because many objects are not very important to be referenced and not referenced, and this phenomenon cannot be divided. Garbage collection, it is not possible to better and more accurately zoned as a GC object.

Therefore, after JDK 1.2, Java extends the concept of references with the following four types of references (sorted by intensity):

* Strong reference (strong Reference)

* Soft Reference (SoftReference)

* Weak reference (WeakReference)

* Virtual Reference (phantomreference)

Strong references: Strong references are ubiquitous in program code. For example: Object object = new Object (), the garbage collector never reclaims objects of such reference as long as it exists.

Soft references: Soft references are used to describe some objects that are useful but not necessary. For objects associated with soft references, the garbage collector will recycle the referenced objects a second time before the system will likely have a memory overflow exception. The memory overflow exception is thrown only if the garbage collection does not have enough memory.

Weak references: Weak references are a reference that is weaker than soft reference strength, so these referenced objects are also non-mandatory. However, objects that are weakly referenced can only survive until the next garbage collection occurs. When garbage collection is started, these weakly referenced objects are recycled regardless of whether the current memory is sufficient.

Virtual Reference: A virtual reference is the weakest reference. Whether an object is associated with a virtual reference does not affect its lifetime at all, nor can it obtain an object instance from a virtual reference. The only purpose of setting a virtual reference association for an object is to be able to receive a system notification when the object is reclaimed by the collector.

Notes: The most typical use of references is the custom development of HashMap, including inside the JDK.

Strong reference-> HashMap: By default, the reference used by HASHMAP is a strong reference, that is, when garbage is collected, the object referenced in the map is not dropped by GC.

Weak reference-> WEAKHASHMAP:JDK also has a hashmap,weakhashmap based on the reference type implementation. When the node key is not being used, the entry is automatically reclaimed. Therefore, for a mapping mapping, there is no guarantee that the next GC will not reclaim this entry.

Soft reference-> Softhashmap: There is no hashmap in the JDK that is based on a soft-reference implementation, probably because a memory overflow is not generally expected, and when a memory overflow occurs, a little bit of soft reference to the remaining memory space of the GC, Certainly won't play a key role. However, although not widely available, the SOFTHASHMAP is implemented in the Classloaderrepository class provided by ASPECTJ, and as a method based on ClassLoader bytecode implementation, it is clear that in oom, it is necessary to consider freeing the memory space by GC. Also the Softhashmap is used internally as a cache. 3.3 jmm Visibility

In the Java Small Tips blog, simply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.