第十一章 缓存机制(六) 概述

来源:互联网 发布:如何做好旅游网络推广 编辑:程序博客网 时间:2024/06/07 00:08

Shiro 提供了类似于Spring的Cache抽象,即Shiro 本身不实现Cache,但是对Cache 进行了又抽象,方便更换不同的底层Cache实现。对于Cache的一些概念可以参考我的《SpringCache抽象详解》:http://jinnianshilongnian.iteye.com/blog/2001040。

Shiro提供的Cache接口:

public interface Cache<K, V> {//根据Key获取缓存中的值public V get(K key) throws CacheException;//往缓存中放入key-value,返回缓存中之前的值public V put(K key, V value) throws CacheException;//移除缓存中key对应的值,返回该值public V remove(K key) throws CacheException;//清空整个缓存public void clear() throws CacheException;//返回缓存大小public int size();//获取缓存中所有的keypublic Set<K> keys();//获取缓存中所有的valuepublic Collection<V> values();}


Shiro提供的CacheManager接口:

public interface CacheManager {//根据缓存名字获取一个Cachepublic <K, V> Cache<K, V> getCache(String name) throws CacheException;}


Shiro还提供了CacheManagerAware用于注入CacheManager:

public interface CacheManagerAware {//注入CacheManagervoid setCacheManager(CacheManager cacheManager);}

Shiro 内部相应的组件(DefaultSecurityManager)会自动检测相应的对象(如Realm)是否实现了CacheManagerAware并自动注入相应的CacheManager。


本章用例使用了与第六章的代码。