CacheManager is mainly used to cache the authentication information and authorization information of the session, realm in Shiro.
1. Class structure
2. Interface and class Introduction
Provides the role of acquiring the cache by name.
A local concurrency map is provided for caching. Provides an abstract class for subclass inheritance, and subclasses only need to create the cache.
Implement the above abstract class. Create a map as a cache.
3.Cache Related Introduction
The main provision of cache-related methods of adding and deleting changes.
Do a cache with map. Injected through the constructor.
The following also provides my own Rediscache implementation. Key is of type string. Need to provide spring redistemplate yourself.
ImportLombok. Getter;ImportLombok. Setter;Importorg.apache.commons.collections.CollectionUtils;ImportOrg.apache.shiro.cache.Cache;Importorg.apache.shiro.cache.CacheException;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.data.redis.core.ValueOperations;ImportJava.util.*;ImportJava.util.concurrent.TimeUnit;/*** Desc: * *@author: * CREAT_DATE:2018/3/22 0022 * creat_time:9:53 **/@Getter @setter Public classShirorediscache<v>ImplementsCache<string, v> { PrivateLogger log =Loggerfactory.getlogger (GetClass ()); PrivateRedistemplate<string, v>redistemplate; /*** Global prefix for cache*/ PrivateString Globalprefix = "Shiro_cache:"; /*** True Cache prefix = Global prefix + cache name*/ PrivateString prefix; /*** Expiry Time*/ Private intExpiretime; PublicShirorediscache (redistemplate<string, v> redistemplate, String prefix,intexpiretime) { This. redistemplate =redistemplate; This. prefix =prefix; This. Expiretime =Expiretime; } @Override PublicV get (String key)throwscacheexception {if(log.isdebugenabled ()) {Log.debug ("Key: {}", key); } if(Key = =NULL) { return NULL; } returnRedistemplate.opsforvalue (). get (key); } @Override PublicV Put (String key, V value)throwscacheexception {if(log.isdebugenabled ()) {Log.debug ("Key: {}, Value: {}", key, value); } if(Key = =NULL|| Value = =NULL) { return NULL; } redistemplate.opsforvalue (). Set (key, value); Redistemplate.expire (Key, Expiretime, timeunit.minutes); returnvalue; } @Override PublicV Remove (String key)throwscacheexception {if(log.isdebugenabled ()) {Log.debug ("Key: {}", key); } if(Key = =NULL) { return NULL; } valueoperations<string, v> vo =Redistemplate.opsforvalue (); V value=Vo.get (key); Redistemplate.delete (key); returnvalue; } @Override Public voidClear ()throwscacheexception {redistemplate.delete (keys ()); } @Override Public intsize () {intLen =keys (). Size (); returnLen; } @SuppressWarnings ("Unchecked") @Override PublicSet<string>keys () {String key= prefix + "*"; Set<String> set =Redistemplate.keys (key); if(Collectionutils.isempty (set)) {returnCollections.emptyset (); } returnset; } @Override PublicCollection<v>values () {Set<String> keys =keys (); List<V> values =NewArraylist<>(Keys.size ()); for(String key:keys) {Values.add (Redistemplate.opsforvalue (). Get (key)); } returnvalues; }}
View Code
Introduction to CacheManager-related class structure in Shiro, providing Redis cache implementation