Analysis of Local Cache Technology-Guava Cache | JD Logistics Technical Team

1 Introduction

As a java developer, I believe that everyone is not too unfamiliar with the guava toolkit. As for the local caching technology guava cache, everyone also knows something about it in daily work and development. Next, this article will analyze the guava cache provided by Google from various angles.

2 guava cache application scenarios

The data read and write of the local cache is all in one process. Compared with distributed caches such as redis, it does not require the process of network transmission, and the access speed is very fast. At the same time, it is also restricted by the JVM memory and cannot be used in scenarios with a large amount of data.

Based on the above characteristics, the main application scenarios of guava cache are as follows:

  • There are greater requirements for access speed
  • Stored data changes infrequently
  • The amount of data is not large, and the memory usage is small
  • need to access the entire collection
  • Can tolerate data not real-time

Here guava cache is used to store parameter configuration, which also meets the above application scenario conditions.

3 How to use guava cache

Guava cache is located under the com.google.common.cache package. There are two core classes, one is CacheBuilder, which is used to build the cache, and the other is Cache, which is the cache container, which is used to store cached data.

To use guava cache, first introduce maven dependencies:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

Next write a simple example:

Cache<String, String> localCache = CacheBuilder.newBuilder()
                .initialCapacity(5)
                .maximumSize(10)
                .concurrencyLevel(3)
                .expireAfterWrite(10, TimeUnit.SECONDS)
                .build();

The above example instantiates a local cache, and then introduces the meaning of the initialization parameters:
initialCapacity: The minimum capacity of the internal hash table, that is, the initial capacity of the cache.

maximumSize: The maximum cache size of the cache.

concurrencyLevel: Concurrency level, which can also be defined as the number of threads operating the cache at the same time, determined by

It can be seen that the number of threads defaults to 4.

expireAfterWrite: cache refresh time after writing.

The method to get data from the cache is the get(K key, Callable<? extends V> loader) method. The meaning of this method is to get data according to the key key. If the key does not exist, the cache is constructed by executing the specified Callable method. The sample code is as follows:

Map<String, Dicdetail> dicDetailMap = ObLocateCache.locateConfigCache.get(key.toString(), new Callable<Map<String, Dicdetail>>() {
                @Override
                public Map<String, Dicdetail> call() throws Exception {
                    return getConfigParameterFromMaster(baseDomain, KeyConstants.WMS5_LOCATE_MANUAL);
                }
            });

Deleting data from the cache is divided into passive deletion and active deletion:

1. Passive deletion:

  • Delete based on data size: LRU+FIFO
  • Delete based on expiration time: not accessed within the specified time
  • Reference-based deletion: use the weakKeys and weakValues ​​methods to specify that the Cache only saves weak references to the cache record key and value. In this way, when there are no other strong references to the key and value, the key and value objects will be reclaimed by the garbage collector

2. Actively delete:

//删除指定的key对应数据
cache.invalidate("s");
//将一批对应的数据删除
cache.invalidateAll(Arrays.asList("st","r","ing"));
//全部删除
cache.invalidateAll();

4 Analyze the storage principle of guava cache according to the source code

The data structure of guava cache is similar to that of ConcurrentHashMap. The basic difference between the two is that ConcurrentMap will keep all the added elements until the added elements are removed. In contrast, in order to limit memory usage, guava cache is usually set to automatically recycle elements.

The core class of guava cache is LocalCache, and LocalCache implements the ConcurrentMap interface. There is a Segment array, as shown below

The source code of the data acquisition method is shown in the figure below. It can be seen that the storage principle of guava cache is a data structure composed of a Segment array, a ReferenceEntry linked list, and an AtomicReferenceArray array.

The data structure diagram is as follows:

The length of the Segement array determines the number of concurrent caches. Each Segment inherits ReentrantLock and uses a separate lock. The write operation to the Segment needs to obtain the lock first. The source code of the write operation part is as follows:

5 summary

This article briefly describes the application scenarios and simple usage of guava cache, and introduces the storage principle of guava cache and simple reading and writing methods through the source code. I believe that by reading this article, you can have a general understanding of common guava caches.

Author: JD Logistics Wang Chenwei

Source: JD Cloud developer community Ziqishuo Tech

RustDesk 1.2: Using Flutter to rewrite the desktop version, supporting Wayland accused of deepin V23 successfully adapting to WSL 8 programming languages ​​​​with the most demand in 2023: PHP is strong, C/C++ demand slows down React is experiencing the moment of Angular.js? CentOS project claims to be "open to everyone" MySQL 8.1 and MySQL 8.0.34 are officially released Rust 1.71.0 stable version is released
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10089606