Java Foundation--extension Package Guava (2) cache

Source: Internet
Author: User

1. Why Use Caching

Caching is an essential way to solve performance problems in our daily development. Simply put, the cache is a piece of memory space that is created to improve system performance.

The primary role of caching is to temporarily save the data processing results of the business system in memory and wait for the next access to be used. On many occasions of daily development, due to the performance of hard disk IO or the data processing and acquisition of our own business systems can be very time consuming, when we find our system This data request is very large, frequent IO and frequent logic processing will cause the bottleneck of hard disk and CPU resource. The role of the cache is to save these from the hard data in memory, when there are other threads or clients need to query the same data resources, directly from the cache of memory blocks to return data, so not only to improve the system's response time, but also to save the data processing process of resource consumption, on the whole, The system performance will be greatly improved.

Many systems and architectures are used in a wide range of applications, such as:

1.CPU Cache
2. Operating system cache
3. Local cache
4. Distributed Cache
5.HTTP Cache
6. Database caching
And so on, it can be said that in the computer and network domain, the cache is everywhere. So to speak, as long as there is no hardware performance, etc., where the network transmission will have a cache figure.

The guava cache is a full-memory local cache implementation that provides a thread-safe implementation mechanism. Overall, the guava cache is the perfect choice for local caches, easy to use and performance-friendly.

  The guava cache is created in two ways:

1. Cacheloader
2. Callable Callback

The cache created by these two methods is different from the one that is usually cached by map, except that both methods implement a logic-taking the value of key x from the cache, and if the value is already cached, the value in the cache is returned, and if it is not cached, it can be obtained by a method. But the difference is that the definition of cacheloader is broad and is defined for the entire cache and can be considered a uniform method of load value based on the key value. The callable approach is more flexible, allowing you to specify when you get it.

The difference between 2.Ehcache and guava Cache

Recently in the context of some cache transformation, there are some lessons to be summarized:

Cached version:

ehcache:2.8.3
guava:17.0

    1. Ehcache support persistent to local disk, guava can not;
    2. Ehcache has a ready-made cluster solution, guava not. But the personal feeling is more chicken, to the JVM level cache is too heavy;
    3. The Ehcache jar package is large, and the guava cache is just one of the tools in the Guava jar package, and the latter is much smaller than Ehcache;
    4. Both caches can overload data through the load interface when the cache expires or is not hit, with a slightly different invocation. The main difference is that the Ehcache cache load allows the user to return null, while the guava cache does not allow the return of NULL, because the guava cache determines if load is required based on whether value is null. Therefore, it is not allowed to return NULL, but it can be replaced with an empty object when used. Not allowing NULL to be returned is a good consideration;
    5. Ehcache has memory footprint and size statistics, guava cache does not, need to develop their own.

When to apply Ehcache, when to apply guava cache?

First, both are very mature JVM-level caches, so in most cases they can be met.

Conditions applicable to Ehcache

    1. Persistent persistence is required. Using the persistence feature requires that the cache is stable so that persistent data is not accurately affected by the results.
    2. There are clusters of solutions.

Conditions for Guava cache
Guava Cache said the simple point is a support LRU concurrenthashmap, it does not ehcache so many of the various features, but provides the increase, delete, change, check, refresh rules and rules of the aging rule settings, such as the most basic elements. One of the features in a jar package, the guava cache is extremely concise and can meet the needs of most people.

Summarize
The Ehcache has a full cache feature but is slightly heavier. The guava cache has the most basic caching features and is very light. Everyone chooses to use according to the specific situation.

Java Foundation--extension Package Guava (2) cache

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.