shiro 之 Cache
本节将学习以一下Shiro 的核心功能之一 Cache。关于Shiro 的 Cache 我再次先声明一下下:Shiro 的Cache 的服务范围一般在Shiro 的服务内。关于缓存我们之前有学习过Redis,在一些Application中我们也是用到了 Redis作为缓存来处理数据。因而我们可以得出一个结论:其他缓存可以作为 Shiro 的缓存代理,但是Shiro 自身的缓存(比如ehcache)只能服务于Shiro(比如认证授权时的Subject和一些Session等。)
概念
Shiro Cache
-
- The CacheManager creates and manages Cache instance lifecycles used by other Shiro components. Because Shiro can access many back-end data sources for authentication, authorization and session management, caching has always been a first-class architectural feature in the framework to improve performance while using these data sources. Any of the modern open-source and/or enterprise caching products can be plugged in to Shiro to provide a fast and efficient user-experience.
通常而言
- Shiro提供了类似于Spring的Cache抽象,即Shiro本身不实现Cache,但是对Cache进行了又抽象,方便更换不同的底层Cache实现。Shiro Cache 常用于Authentication、Authorization和SessionManagement。
-
Shiro 提供了三种 Cache Abstract Interface
Cache
package org.apache.shiro.cache; import java.util.Collection; import java.util.Set; public interface Cache<K, V> { public V get(K key) throws CacheException; public V put(K key, V value) throws CacheException; public V remove(K key) throws CacheException; public void clear() throws CacheException; public int size(); public Set<K> keys(); public Collection<V> values(); }
CacheManager
public interface CacheManager { public <K, V> Cache<K, V> getCache(String name) throws CacheException; }
CacheManagerAware: 将CacheManagerAware用于注入CacheManager