Spring boot integrates Redis for Shiro CacheManager

Source: Internet
Author: User
Tags int size redis

Then the previous blog: Spring boot integrated Jpa,shiro for Rights management

Shiro By default integrates the Ehcache to implement caching, if we want to use Redis to replace the Ehcache to implement the cache. We can find some clues from the source code of Shiro. We can simulate how ehcachemanager is implemented, and the Ehcachemanager class is defined as follows:

public class Ehcachemanager implements CacheManager, Initializable, destroyable {

}

We can see from the above code that the final thing is to implement the CacheManager interface, the interface is simple, there is only one method:

Public interface CacheManager {

    /**
     * Acquires the cache with the specified <code>name</code>.  If A cache does not yet exist with the that name, a new one * would be the created with the that
     name and returned.
     * * @param name The name of the the
     cache to acquire.
     * @return The cache with the given name
     * @throws Cacheexception If there are an error acquiring the cache Instance.
  */public
    <k, v> cache<k, v> getcache (String name) throws Cacheexception;
}

From the comments above, we can find that this interface needs a cache, through name to get the cache, first of all, we implement CacheManager this interface

@Service public
class Rediscachemanager implements CacheManager {
	
	@Autowired
	private redistemplate Redistemplate; Redistemplate, if you do not understand how to use, please refer to http://blog.csdn.net/liuchuanhong1/article/details/54601037
	
	@Override
	Public <k, v> cache<k, v> getcache (String name) throws Cacheexception {
		System.out.println ("Name:" +name );
		return new rediscache<k, v> (redistemplate);//To simplify the writing of the code, a new cache} is directly here



Now, let's see how this cache is written.

Package Com.chhliu.springboot.shiro.cache;
Import java.util.Collection;
Import Java.util.Set;

Import Java.util.concurrent.TimeUnit;
Import Org.apache.shiro.cache.Cache;
Import org.apache.shiro.cache.CacheException;

Import Org.springframework.data.redis.core.RedisTemplate; public class Rediscache<k, v> implements Cache<k, v> {private Long expiretime = 120;//cache time-out, unit s PRI
	Vate redistemplate<k, v> redistemplate;//injects the object into public Rediscache () {super () by constructing the method;
		} public Rediscache (long expiretime, redistemplate<k, v> redistemplate) {super ();
		This.expiretime = Expiretime;
	This.redistemplate = redistemplate; }/** * Through the key to obtain the corresponding cache object * Through the source we can find that the type of key Shiro need is the type of Object,v authorizationinfo Object */@Override public V get (K
	Key) throws Cacheexception {return Redistemplate.opsforvalue (). get (key); /** * Add permission information to the cache */@Override public V put (K key, V value) throws cacheexception {Redistemplate.opsforvalue () . Set (key, value, thiS.expiretime, Timeunit.seconds);
	return value; /** * Remove the permission information from the cache */@Override public V Remove (K key) throws Cacheexception {V v = redistemplate.opsforvalue (
		). Get (key);
		Redistemplate.opsforvalue (). GetOperations (). Delete (key);
	return v;
	} @Override public void Clear () throws Cacheexception {} @Override public int size () {return 0;
	} @Override public set<k> keys () {return null;
	} @Override public collection<v> values () {return null;
 }

}

After these two steps are completed, you need to replace the original Ehcachemanager configuration with Rediscachemanager.

@Bean public
	Defaultwebsessionmanager Configwebsessionmanager () {
		Defaultwebsessionmanager manager = new Defaultwebsessionmanager ();
		Manager.setcachemanager (CacheManager);//Switch to Redis cache manager
		Manager.setsessiondao (Sessiondao);
		Manager.setdeleteinvalidsessions (true);
		Manager.setglobalsessiontimeout (Sessiondao.getexpiretime ());
		Manager.setsessionvalidationschedulerenabled (true);
		
		return manager;
	}

With the steps above, we have implemented information such as the ability to cache Shiro with Redis.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.