In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of the sample analysis of the Mybatis caching module. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1 Cache component
The code related to the cache module in MyBatis is located under the org.apache.ibatis.cache package, and the Cache interface is the core interface in the cache module, which defines the basic behavior of all caches.
Public interface Cache {/ * get the current cache Id * / String getId (); / * the key and value,key stored in the cache are usually CacheKey objects * / void putObject (Object key, Object value); / * get the cache value according to key * / Object getObject (Object key); / * delete the specified cache entry * / Object removeObject (Object key) / * clear the cache * / void clear (); / * get the size of the cache * / int getSize (); / * *! * get the read-write lock. As you can see, this interface method provides a default implementation! * this is a new feature of Java8! It's just that it is seldom used in development! *! * / default ReadWriteLock getReadWriteLock () {return null;}}
As shown in the following figure, there are many implementation classes for the Cache interface, but most of them are decorators, and only PerpetualCache provides the basic implementation of the Cache interface.
1.1 PerpetualCache
PerpetualCache (Perpetual: eternal, persistent) plays a decorated role in the cache module, and its implementation is relatively simple. The underlying HashMap records cache items, which are also the corresponding methods defined in the Cache interface implemented through the methods of the HashMap object.
The unique identity of the public class PerpetualCache implements Cache {/ / Cache object private final String id; / / all of its caching functions are based on the method private Map cache = new HashMap () provided by JDK's HashMap; public PerpetualCache (String id) {this.id = id;} @ Override public String getId () {return id;} @ Override public int getSize () {return cache.size () @ Override public void putObject (Object key, Object value) {cache.put (key, value);} @ Override public Object getObject (Object key) {return cache.get (key);} @ Override public Object removeObject (Object key) {return cache.remove (key);} @ Override public void clear () {cache.clear () } / * it overrides the equals () and hashCode () methods in Object, both of which only care about the id field * / @ Override public boolean equals (Object o) {if (getId () = = null) {throw new CacheException ("Cache instances require an ID.");} if (this = = o) {return true;} if (! (o instanceof Cache)) {return false } Cache otherCache = (Cache) o; return getId () .equals (otherCache.getId ());} @ Override public int hashCode () {if (getId () = = null) {throw new CacheException ("Cache instances require an ID.");} return getId () .hashCode ();}}
Let's take a look at the decorators provided under the cache.decorators package, all of which directly implement the Cache interface and act as decorators. These decorators will provide some additional functionality on top of the PerpetualCache, through multiple combinations to meet a specific requirement.
1.2 BlockingCache
BlockingCache is a blocking version of the cache decorator that ensures that only one thread goes to the database to find the data corresponding to the specified key.
Public class BlockingCache implements Cache {/ / blocking timeout private long timeout; / / held decorator private final Cache delegate; / / each key has its corresponding ReentrantLock lock object private final ConcurrentHashMap locks; / / initializing the held decorator and lock collection public BlockingCache (Cache delegate) {this.delegate = delegate; this.locks = new ConcurrentHashMap ();}}
Assuming that thread A does not find the cache item corresponding to keyA in BlockingCache, thread A will acquire the lock corresponding to keyA, so that when thread A looks for keyA later, other threads will be blocked.
/ / acquire the lock object according to key, and then lock private void acquireLock (Object key) {/ / acquire the lock object corresponding to key Lock lock = getLockForKey (key); / / acquire the lock with timeout if (timeout > 0) {try {boolean acquired = lock.tryLock (timeout, TimeUnit.MILLISECONDS) If (! acquired) {/ / timeout, the exception throw new CacheException ("Couldn't get a lock in" + timeout + "for the key" + key + "at the cache" + delegate.getId ()) is thrown;}} catch (InterruptedException e) {/ / if the lock acquisition fails, the throw new CacheException is blocked for a period of time ("Got interrupted while trying to acquire lock for key" + key, e) }} else {/ / lock lock.lock ();}} private ReentrantLock getLockForKey (Object key) {/ / Java8 new feature, new method / / V computeIfAbsent (K key, Function) added to Map series classes
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.