沫言|java:shiro高级篇——1( 二 )


packagecom.itheima.shiro.core;importorg.apache.shiro.cache.Cache;importorg.apache.shiro.cache.CacheException;/***@Description简单的缓存管理接口*/publicinterfaceSimpleCacheService{/***功能说明::新增缓存堆到管理器*/voidcreateCache(StringcacheName,Cachecache)throwsCacheException;/***方法名::getCache*功能说明::获取缓存堆*/CachegetCache(StringcacheName)throwsCacheException;/***方法名::removeCache*功能说明::移除缓存堆*/voidremoveCache(StringcacheName)throwsCacheException;/***方法名::updateCahce*功能说明::更新缓存堆*/voidupdateCahce(StringcacheName,Cachecache)throwsCacheException;}SimpleCacheServiceImpl
【沫言|java:shiro高级篇——1】调用RedissonClient去实现缓存 , 同时使用ShiroRedissionSerialize实现序列化
packagecom.itheima.shiro.core.impl;importcom.itheima.shiro.constant.CacheConstant;importcom.itheima.shiro.core.SimpleCacheService;importcom.itheima.shiro.utils.ShiroRedissionSerialize;importlombok.extern.log4j.Log4j2;importorg.apache.shiro.SecurityUtils;importorg.apache.shiro.cache.Cache;importorg.apache.shiro.cache.CacheException;importorg.redisson.api.RBucket;importorg.redisson.api.RedissonClient;importorg.springframework.stereotype.Component;importjavax.annotation.Resource;importjava.util.concurrent.TimeUnit;/****@Description简单的缓存管理接口的实现*/@Log4j2@ComponentpublicclassSimpleCacheServiceImplimplementsSimpleCacheService{@Resource(name="redissonClientForShiro")RedissonClientredissonClient;@OverridepublicvoidcreateCache(Stringname,Cachecache){RBucketbucket=redissonClient.getBucket(CacheConstant.GROUP_CAS+name);bucket.trySet(ShiroRedissionSerialize.serialize(cache),SecurityUtils.getSubject().getSession().getTimeout()/1000,TimeUnit.SECONDS);}@SuppressWarnings("unchecked")@OverridepublicCachegetCache(Stringname)throwsCacheException{RBucketbucket=redissonClient.getBucket(CacheConstant.GROUP_CAS+name);return(Cache)ShiroRedissionSerialize.deserialize(bucket.get());}@OverridepublicvoidremoveCache(Stringname)throwsCacheException{RBucketbucket=redissonClient.getBucket(CacheConstant.GROUP_CAS+name);bucket.delete();}@OverridepublicvoidupdateCahce(Stringname,Cachecache){RBucketbucket=redissonClient.getBucket(CacheConstant.GROUP_CAS+name);bucket.set(ShiroRedissionSerialize.serialize(cache),SecurityUtils.getSubject().getSession().getTimeout()/1000,TimeUnit.MILLISECONDS);}}