Java implements the basic mechanism of the cache

Source: Internet
Author: User

I'm talking about this. The cache does not refer to caching between the CPU and RAM, but rather a common cache among Java applications. The most commonly used scenario is the cache used to improve efficiency when accessing the database. The general usage is to read the data from the database into memory, then the data access is read from memory, thus reducing the number of reads to the database to improve efficiency.

The easiest mistake to make when using the cache is that the cache involves business logic. The intent of using the cache is simply to increase the efficiency of the program rather than interfering with the program results. As defined by CAHCE, the cache should work transparently to the data access end. So when we use the cache, we can ask ourselves: "Can I take the cache and the program will run?" "Did the cache get rid of the results of the program running before and after?" ”。 If the answer is no, then you have to reconsider your cache scenario. I have encountered such a bug myself: The database has a table inside is some configuration information, that is, some read access to more than write access data. The data is then properly made into memory caches within the program. The problem is that a delete method deletes a piece of data, but does not update the memory cache. So the client code of the read operation is still able to read this data. The root of the problem is background data and cache inconsistencies.

Cache capacity is generally relatively limited relative to the amount of background data. Once the cache is full, it is imperative to choose the most useless data to remove it from the cache and make room for the new data. This involves the CAHCE algorithm cache algorithm or call substitution algorithm. In Java, the cache product is generally called evict policy. Let's take a look at the commonly used cache algorithm.

    • Least recently used algorithm Least recently Used (LRU):
This algorithm removes the data that is farthest from the last time it was used. The most intuitive structure should be the list, which is the algorithm that puts the element at the end of the list each time an element is accessed, so that the farthest-used element is naturally placed at the other end of the list. Remove the farthest-used element every time you evict. But the data structure used in reality is HashMap + List. Because the list is too slow, list can only provide an O (n) algorithm, and to make its add,remove and get algorithm O (1), the HashMap must be used. The simplest implementation is to take advantage of the linkedhashmap that comes with the JDK, and you can think of it as a normal hashmap, and each element's key is connected by a linked list to achieve a sequential structure. Linkedhashmap The default element order is put, but if you use constructors with parameters, Linkedhashmap adjusts the internal order according to the order of access. The Get () method of the Linkedhashmap can also place the accessed element at the bottom of the list in addition to returning the element, so that the element at the top is the remove element at a time.
    • First in, first out algorithm
This is more intuitive, is a queue. However, in order to ensure the efficiency of O (1), or to use Linkedhashmap. But this time using the default parameterless constructor, Linkedhashmap uses the put order internally. So every time you remove the top.
    • Most recently used algorithms most recently Used (MRU)
This algorithm and LRU are the opposite, so nothing new. The element at the bottom of each remove linkedhashmap can be implemented.
    • Minimum number of usage algorithms Least frequently Used (LFU)
The core of this algorithm is that each time an element is accessed, the element's number of times attribute is added to 1. So each remove operation is the element with the least number of times attribute. This is not possible with Linkedhashmap, because Linkedhashmap does not accept the function of comparator parameters. Some programs are implemented with LinkedList + HashMap. This allows the add and get operations to be O (1), except that the remove operation is sorted first and then remove, the fastest is O (n*log N), such as the use of fast sorting. Or simply do the algorithm to find the smallest element when removing it to remove the element with the least number of accesses.

There are also other cache algorithms, such as the algorithm of evict elements according to the expiration value of the element's expiration and the stochastic random. In a real cache product, data structures and algorithms are more complex than described above. Some products define data structures themselves to improve efficiency, after all, the cache is created to improve efficiency.    Advanced cache products may also include transactional mechanisms, JMX, and complex features that support the cluster environment. At present, the main cache products are Ehcache,oscache,swarmcache and JBoss cache, many people who use hibernate have some knowledge of this. With regard to JBoss Cache, it may be replaced in the future by another data grid platform project called Infinispan by JBoss.

Java implements the basic mechanism of the 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.