In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the introduction of Ehcache and the integration of Spring to achieve the role of caching", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the introduction of Ehcache and the integration of Spring to achieve caching"!
Ehcache introduction
EhCache is a pure Java in-process cache framework, which is fast and lean, and is the default CacheProvider in Hibernate. It uses JVM's heap memory, which can be cached to disk, and the enterprise version can use physical memory outside the JVM heap.
Spring integrates Ehcache
First add the latest ehcache's maven dependency
Net.sf.ehcache
Ehcache
2.10.4
Add the following configuration to the Spring configuration file
Add ehcache's configuration file ehcache.xml to classpath
Profile description
Spring Cache's Cacheable annotations do not support expiration time settings, so caching can only be designed on top of ehcache.xml. The above configuration file is configured with two caches, one that does not expire forever, and one that is cached for 1 hour, with more time limits added later.
Parameter description
UpdateCheck: it is recommended to disable the automatic update of ehcache, otherwise you have to go to the official website to update it every time you start.
Monitoring: automatic detection
DynamicConfig: supports dynamic configuration changes
MaxBytesLocalHeap: maximum heap memory usage. Unit can be K _ Magi _ M _ G
MaxBytesLocalDisk: maximum disk usage. Unit can be K _ Magi _ M _ G
MaxBytesLocalHeap and maxBytesLocalDisk are defined on the overall configuration, but they can also be specified on a specific cache, but the maximum capacity is based on the size above the total configuration.
DiskStore: specify the data storage location, and you can specify the folder location on the disk
DefaultCache: default management policy
The following properties are required
Name: the name of the Cache must be unique. Ehcache will put the cache in the HashMap.
MaxElementsInMemory: the maximum number of element cached in memory.
MaxElementsOnDisk: the maximum number of element cached on disk. The default value is 0, which means there is no limit.
Eternal: sets whether the cached elements never expires. If it is true, the cached data is always valid, and if it is false, then it is judged by timeToIdleSeconds,timeToLiveSeconds.
OverflowToDisk: if the data in memory exceeds the memory limit, whether to cache it to disk.
The following properties are optional:
TimeToIdleSeconds: the idle time of an object, which refers to how long the object has not been accessed before it becomes invalid. Valid only if eternal is false. The default value is 0, which means that it can always be accessed.
TimeToLiveSeconds: object survival time, which refers to the time it takes for an object to expire from creation to failure. Valid only if eternal is false. The default value is 0, which means that it can always be accessed.
DiskPersistent: whether to persist on disk. This refers to whether the data is valid after restarting jvm. The default is false.
DiskExpiryThreadIntervalSeconds: object detects thread run time interval. How often does the thread that identifies the state of the object run.
DiskSpoolBufferSizeMB: the disk size used by DiskStore. The default is 30MB. Each cache uses its own DiskStore.
MemoryStoreEvictionPolicy: the strategy for caching disk if the data in memory exceeds the memory limit. The default value is LRU. You can choose FIFO and LFU.
Three kinds of cache emptying strategies
FIFO-first in first out (first in, first out).
LFU-Less Frequently Used (least used), which means the least used all the time. The cached element has a hit attribute, and the one with the lowest hit value will be cleared out of the cache.
LRU-Least Recently Used (least recently used), (ehcache default). The cached element has a timestamp, and when the cache capacity is full and the space is needed to cache the new element, the element with the farthest timestamp from the current time in the existing cached element will be cleared out of the cache.
Cache usage
You can use the annotations provided by Spring and pass in the parameters needed for annotations such as vaue,key.
@ Cacheable: check the cache first. If there is no method, it will be cached again if there is no method.
CachePut: the method is executed each time and then cached.
@ CacheEvict: invalidate the cache.
@ Caching: the above three annotations can be combined.
At this point, I believe you have a deeper understanding of "the introduction of Ehcache and the integration of Spring to achieve caching". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.