Ehcache is an open-source, standards-based cache that improves performance and reduces database load, and is the most widely used Java-based cache today.
- Cache: The wiki defines the cache as "storage of what is going to be used and can be retrieved quickly". A cache is a set of temporary data, either a copy of the data elsewhere, or a result of the calculation. data already in the cache can be repeatedly accessed at minimal cost in terms of time and resources.
- Cache entry: A cache entry consists of a key and its mapped data values in the cache.
- Cache hit: When a data element is requested from the cache, there is an element for the given key, which is known as a cache hit.
- Cache misses: When a data element is requested from the cache, there is no element for the given key, which is known as a cache miss.
- Recording system: The authoritative source of data, the lowest level in the storage layer. It is usually a traditional database, which may be a specialized file system or other reliable long-term storage.
- Eviction: Removes entries from the cache to make room for newer entries (typically when the cache is running out of data storage capacity).
- Expiration: After a period of time, entries are removed from the cache, usually to avoid stale data in the cache.
- Hot data: Data recently used by applications is likely to be accessed again soon. Such data are considered to be popular. The cache may try to save the hottest data in the fastest area while trying to choose the least popular data for eviction.
(1) Freshness of data
Data freshness is the most recent situation in which a copy of the data (for example, in the cache) is compared to the source version of the data (for example, in a recording system). An expired copy is considered to be out of sync (or most likely out of sync).
(2) Cache entry expires
Ehcache can help you reduce the likelihood of using outdated data. Configure how and when the cache expires in your app, and once it expires, the entry is automatically removed from the cache. 3 types of expiration methods currently supported:
- No expiry: Never expire
- Time-to-live: After the entry has been created, it will expire after the set time is exceeded
- Time-to-idle: If the last access time of an entry exceeds the set length of time, it will be processed out of date
The concept of tiered caching
(1) Storage layer Introduction
Storage Tiers
Ehcache 3, as in previous versions, offers a tiering model to allow storing increasing amounts of data on slower tiers (wh Ich is generally more abundant).
The idea was that resources related to faster storage was more rare, but was located where the ' hottest ' data is preferred To IS. Thus Less-hot (less frequently used) data was moved to the more abundant but slower tiers. Hotter data is moved onto the faster tiers.
Ehcache 3, as in previous versions, provides a layered model that allows more data to be stored on slower tiers.
Faster storage tiers typically have fewer resources, but are located in the "hottest" data location. As a result, less commonly used data is transferred to richer but slower tiers, while more commonly used data is transferred to a faster layer.
You can configure Ehcache to use various data storage areas. When a cache was configured to use more than one storage area, those areas was arranged and managed as tiers
. They is organized in a hierarchy, with the lowest tier (farther) being called the authority
tier and the others being part of caching
the tier (nearer, also called near cache
). The caching tier can itself is composed of more than one storage area. The hottest data is kept in the caching tier, which are typically less abundant but faster than the authority tier . All the data are kept in the authority tier, which are slower but more abundant.
Data stores supported by Ehcache include:
On-heap store-utilizes Java ' s on-heap RAM memory to Store cache entries. This tier utilizes the same heap memory as your Java application, all of which must is scanned by the JVM garbage collecto R. The more heap space your JVM utilizes, the more your application performance would be impacted by garbage collection Pau Ses. This store was extremely fast, but was typically your most limited storage resource.
Off-heap store-limited in size is only by available RAM. Not subject to Java garbage collection (GC). is quite fast, yet slower than the On-heap Store because data must being moved to and from the JVM Heap as it is stored and r E-accessed.
Disk store-utilizes a disk (file system) to Store cache entries. This type of storage resource was typically very abundant but much slower than the ram-based stores. As-all application using disk storage, it's recommended to use a fast and dedicated disk to optimize the throughput.
Clustered store-this Data Store is a cache on a remote server. The remote server may optionally has a failover server providing improved high availability. Since Clustered storage comes with performance penalties due to such factors as network latency as well as for Establishin G Client/server Consistency, this tier, by nature, is slower than local off-heap storage.
The lowest level in the storage layer is named the Authority layer (tier authority
, the most resource-rich layer), typically the disk tier or the cluster storage tier (Clustered tier), and the other faster-access tiers have a heap storage layer (heap tier) and the off-heap storage tier (off heap tier)
The data stores supported by Ehcache include:
- In-heap storage-use Java's heap memory to store cache entries. Use the same heap memory managed by the JVM garbage collector as the Java application. The more heap space The JVM uses, the greater the garbage collection pauses that are affected by application performance. This storage is the fastest but with minimal space.
- Out-of-heap storage-only available RAM limits the size. Not affected by the Java garbage collection (GC). It is very fast, but slower than in-heap storage because the data is stored and re-accessed through the heap storage layer.
- Disk storage-Use disk (file system) to store cache entries. This type of storage resource is usually very rich, but much slower than RAM-based storage. For all applications that use disk storage, it is recommended to use a fast and dedicated disk to optimize throughput.
- Cluster storage-This data store is a cache on the remote server. A remote server can selectively provide a failover server to make the availability higher. Cluster storage is subject to performance penalties due to network latency and client/server consistency, so the performance of this layer is slower than the local out-of-heap storage.
From the official: http://www.ehcache.org/documentation/3.4/caching-concepts.html#storage-tiers
Sequence flowchart for caching operations in multiple storage tiers
Sequence Flow for cache Operations with multiple Tiersin order to understand what happens for different cache Operations W Hen using multiple tiers, here is examples of
Putand
GetOperations. The sequence diagrams is oversimplified but still show the main points.
Figure 2. Multiple tiers using Put
Figure 3. Multiple tiers using Get
You should then notice the following:
When putting a value into the cache, it goes straight to the authoritative tier, and which is the lowest tier.
A following would get
push the value upwards in the caching tiers.
Of course, as soon as a value is put in the authoritative tier, all higher-level caching tiers are invalidated.
A Full Cache miss (the value is ' t on any tier) 'll always go all the the the the-authoritative tier.
To understand what happens to different cache operations when multiple tiers are used, the figure is an example of put and get operations. The sequence diagram is too simplified, but it still shows the point.
You should notice the following points:
- When a value is put into the cache, it goes directly to the authority layer, which is the lowest level.
- Get pushes values upward in the cache layer.
- Once the values are placed in the authority layer, all the more recent cache tiers will be invalidated.
- The cache is lost (the value is not on any tier) and always gets down until the authority layer.
From the official: Http://www.ehcache.org/documentation/3.4/tiering.html#multi-tier-sequence-flow
About Hibernate level two cache third-party plug-in Ehcache cache