Java-Cache hotspot data, using algorithms at least recently, java-hotspot

Source: Internet
Author: User

Java-Cache hotspot data, using algorithms at least recently, java-hotspot

1. Least recently used algorithm LRU (Least recently used, Least recently used)

[Implementation]: The most common is to use a linked list to save cached data.

1. Insert new data into the head of the linked list;

2. When the cache hits (that is, the cache data is accessed), the data is moved to the head of the linked list;

3. When the linked list is full, the data at the end of the linked list is discarded;

 

[Cost]

When hit, you need to traverse the table, find the hit data block index, and then move the data to the header.

 

[Change]

Based on the above cost, we changed the maintained linked list to a two-way linked list (that is, each node has a prev and next). In addition, we need to maintain another map, put the reference of the cached object into map;

1. Insert new data into the head of the linked list and put it in map.

2. Whenever cache is required, search for the data in the map using the key. When the cache is hit, the data is moved to the head of the linked list (this movement is very good, you only need to assign the next attribute of the prev node to the next node of the node, and assign the prev attribute of the next node to the prev node of the node, and put the node in the head of the linked list ).

3. When the linked list is full, the data at the end of the linked list is discarded and the corresponding data in the map is deleted.

 

Result]

Based on the above-modified LRU algorithm, the performance has been greatly improved by removing the disadvantages of hitting the cache and requiring the traversal link table.

 

2. Use redis to cache data to ensure the Cache Usage and principles of hotspot data

One point: as long as the memory occupied by redis is limited, redis will load hot data to the memory according to its own data elimination policy.

[Implementation ]:

Use redis to set the expiration time to cache hotspot data

1. Set the expiration time for the data every time the cache hits

2. the cache that is frequently hit will never expire and will not be deleted, rather than the hotspot data will be deleted as soon as it expires, this ensures that hot data always exists in redis.

 

Principle]

1. The principle is actually the principle of delay blocking queue DelayQueue in Java

2. When an expiration time is set for the cached data in redis, it is equivalent to putting the cached data into the delayed blocking queue DelayQueue maintained in redis.

3. DelayQueue sorts the cached data according to the expiration time. The cache data is placed after the queue for a short time.

4. One or more threads are used to query DelayQueue cyclically. Once an element is obtained from DelayQueue, the cached data expires and can be retrieved and deleted.

5. When multiple threads query DelayQueue at the same time, only one thread can win the Header element, and other threads will be blocked. When the Header element is removed, it will wake up all the blocked threads. The thread competes with the Header element. The thread competing with the Header element will query the remaining delay time of the Header element, in addition, the Header element has been occupied by this thread, and then wait itself based on the delay time. Finally, it gets the Header element and wakes up other blocked threads.

Related Article

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.