前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shiro的Cache机制

shiro的Cache机制

作者头像
用户5640963
发布2019-07-28 13:48:16
9620
发布2019-07-28 13:48:16
举报
文章被收录于专栏:卯金刀GG卯金刀GG卯金刀GG

Shiro开发团队明白在许多应用程序中性能是至关重要的。Caching 是Shiro 中的一个重要功能,以确保安全操作保持尽可能的快。

但是,Shiro并不实现缓存的功能,Shiro 的缓存支持基本上是一个抽象的(包装)API,它将“坐”在一个基本的缓存机制产品(例 如,Ehcache,OSCache,Terracotta,Coherence,GigaSpaces,JBossCache 等)之上。这允许Shiro终端用户配置他们喜欢的任何缓存机制。

Caching API

Shiro 有三个重要的缓存接口:

1:CacheManager - 负责所有缓存的主要管理组件,它返回Cache 实例

2:Cache - 维护key/value 对

3:CacheManagerAware - 通过想要接收和使用CacheManager 实例的组件来实现

CacheManager 返回Cache 实例,各种不同的Shiro 组件使用这些Cache 实例来缓存必要的数据。任何实现了CacheManagerAware 的Shiro 组件将会自动地接收一个配置好的CacheManager,该CacheManager 能够用来获取Cache 实例。

Shiro 的SecurityManager 实现及所有AuthorizingRealm实现都实现了CacheManagerAware

Shiro 提供了一个个立即可用的EhCacheManager 实现

Caching 配置

通过在SecurityManager上设置了CacheManger,它反过来也会将它设置到实现了CacheManagerAware 的各种不同的Realm 上,示例如下:

spring-shiro.xml配置

<!-- 用户授权信息Cache, 采用EhCache --> <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:spring/ehcache-shiro.xml" /> </bean>

ehcache-shiro.xml

<ehcache updateCheck="false" name="shiroCache">

<diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="true" diskExpiryThreadIntervalSeconds="120" />

<cache name="shiro-activeSessionCache" maxElementsInMemory="10000" eternal="true" timeToLiveSeconds="0" timeToIdleSeconds="0" diskPersistent="true" overflowToDisk="false" diskExpiryThreadIntervalSeconds="600"> </cache> </ehcache>

name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。

包装使用其他的Cache框架

可以通过写一个类来实现Shiro的CacheManager,在这个类里面包装使用任何你想要使用的Cache框架,这里以使用Srping的缓存框架为例,参考如下:

public class MyCacheManager implements CacheManager {

public <K, V> Cache<K, V> getCache(String name) throws CacheException {

org.springframework.cache.Cache springCache = cacheManager.getCache(name);

return new SpringCacheWrapper(springCache);

}

class SpringCacheWrapper implements Cache {

private org.springframework.cache.Cache springCache;

SpringCacheWrapper(org.springframework.cache.Cache springCache) {

this.springCache = springCache;

}

public Object get(Object key) throws CacheException {

Object value = springCache.get(key);

if (value instanceof SimpleValueWrapper) {

return ((SimpleValueWrapper) value).get();

}

return value;

}

}

}

缓存数据同步更新的解决方案

使用Shiro的时候,缓存数据最大的问题就在于数据同步更新。

因为Shiro只负责验证部分,如果应用程序修改了人员的权限,那么就需要同步更新到Shiro里面去,也就是要同步Shiro的缓存数据。

一个解决方案就是完全废弃Shiro的缓存机制,自己在应用中控制数据的缓存

这里给出另一种简易可行的方案:

1:如果你使用的Spring,而且是自定义的Realm,那么可以在你的Realm里面添加一个方法来删除该用户的缓存数据,这样下次shiro在验证这个用户的时候,就会重新去获取数据,从而实现数据的同步

2:由于是自定义的Realm,可以把该对象作为Spring的bean,注入到你的业务对象中,在需要的时候就可以调用该方法来删除shiro的缓存数据了

示例,比如在前面自定义的MyRealm中,添加如下方法,示例如下:

public void removeUserCache(String userId){

SimplePrincipalCollection pc = new SimplePrincipalCollection();

pc.add(userId, super.getName());

super.clearCachedAuthorizationInfo(pc);

}

然后在HelloAnno中进行测试,示例如下:

1:要注入MyRealm,但注意需要使用getter/setter来注入

2:在main方法中,示例如下:

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

HelloAnno t = (HelloAnno)ctx.getBean("helloAnno");

t.login();

t.t();

t.t();

t.getMr().removeUserCache("javass");

t.t();

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档