缓存工具EhCache使用

EhCache是一种广泛使用的Java分布式缓存,常用于内存和磁盘存储,支持Hibernate。配置包括添加依赖、配置文件设置、测试缓存。Shiro可以通过shiro-ehcache实现缓存整合,提高认证效率。另外,Shiro的SessionManager负责会话管理,提供不同环境下的实现方式。
摘要由CSDN通过智能技术生成

实现缓存

  1. 缓存工具EhCache

① EhCache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存, Java·EE和轻量级容器。可以和大部分Java项目无缝整合,例如:Hibernate中的缓存就是基于EhCache实现的。

② EhCache支持内存和磁盘存储,默认存储在内存中,如内存不够时把缓存数据同步到磁盘中。EhCache支持基于Filter的Cache实现,也支持Gzip压缩算法。

③ EhCache直接在JVM虚拟机中缓存,速度快,效率高;

④ EhCache缺点是缓存共享麻烦,集群分布式应用使用不方便

  1. EhCache搭建使用

(1)创建模块

(2) 添加依赖

<dependencies>    
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.6.11</version>
            <type>pom</type>
        </dependency>
</dependencies>

(3)添加配置文件 ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--
       diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:
       user.home – 用户主目录
       user.dir  – 用户当前工作目录
       java.io.tmpdir – 默认临时文件路径
     -->
    <!--磁盘的缓存空间-->
    <diskStore path="java.io.tmpdir/Tmp_EhCache"/>
    <!--
       defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。
     -->
    <!--
      name:缓存名称。
      maxElementsInMemory:缓存最大数目
      maxElementsOnDisk:硬盘最大缓存个数。
      eternal:对象是否永久有效,一但设置了,timeout将不起作用。
      overflowToDisk:是否保存到磁盘,当系统当机时
      timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
      timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
      diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
      diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
      diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
      memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
      clearOnFlush:内存数量最大时是否清除。
      memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
      FIFO,first in first out,这个是大家最熟的,先进先出。
      LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
      LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
   -->
<!--默认缓存-->
    <defaultCache
            maxEntriesLocalHeap="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap"/>
    </defaultCache>

<!--helloworld缓存-->
    <cache
            name="HelloWorldCache"
            maxElementsInMemory="5000"
            eternal="false"
            timeToIdleSeconds="5"
            timeToLiveSeconds="5"
            overflowToDisk="false"
            memoryStoreEvictionPolicy="LRU"/>

</ehcache>

(4)测试EhCache

public class TestEH{
    public static void main(String[] args){
        //获取编译目录下的资源的流对象
        InputStream input = TestEH.class.getClassLoader().getResourceAsStream("ehcache.xml");
        //获取EhCache的缓存管理对象
        CacheManager cacheManager = new CacheManager(input);
        //获取缓存对象
        Cache cache = cacheManager.getCache("HelloWorldCache");
        //创建缓存数据
        Element element = new Element("name","zhang3");
        //存入缓存
        cache.put(element);
        //从缓存中取出数据输出
        Element element1 = cache.get("name");
        System.out.println("element1 = " + element1.getObjectValue());

shiro整合EhCache

Shiro官方提供了shiro-ehcaehe,实现了整合EhCache作为Shiro的缓存工具。可以缓存认证执行的Realm方法,减少对数据库的访问,提高认证效率。

(1)添加依赖

<!--shiro 整合 EhCache-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

(2)在resources 下添加配置文件 ehcache/ehcache-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="ehcache" updateCheck="false">
    <!--磁盘的缓存位置-->
    <diskStore path="java.io.tmpdir"/>
    <!--默认缓存-->
    <defaultCache
                maxEntriesLocalHeap="1000"
                eternal="false"
                timeToIdleSeconds="3600"
                timeToLiveSeconds="3600"
                overflowToDisk="false">
    </defaultCache>
    
    <!--登录认证信息缓存:缓存用户角色权限-->
    <cache name="loginRolePsCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true"/>

    </ehcache>

(3)在配置类里面添加 缓存管理器

@Configuration
public class ShiroConfig {
    @Autowired
    private MyRealm myRealm;

    //配置 SecurityManager
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager() {
//1 创建  defaultWebSecurityManager 对象
        DefaultWebSecurityManager defaultWebSecurityManager = new
                DefaultWebSecurityManager();
//2 创建加密对象,并设置相关属性
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//2.1 采用  md5 加密
        matcher.setHashAlgorithmName("md5");
//2.2 迭代加密次数
        matcher.setHashIterations(3);
//3 将加密对象存储到  myRealm 中
        myRealm.setCredentialsMatcher(matcher);
//4 将  myRealm 存入 defaultWebSecurityManager 对象
        defaultWebSecurityManager.setRealm(myRealm);
//4.5 设置  rememberMe
        defaultWebSecurityManager.setRememberMeManager(rememberMeManager());
//4.6 设置 缓存管理器
        defaultWebSecurityManager.setCacheManager(getEhCacheManager);

//5 返回
        return defaultWebSecurityManager;
    }
    //就是添加这个
    //缓存管理器
    public EhCacheManager getEhCacheManager(){
        EhCacheManager ehCacheManager = new EhCacheManager();
        InputStream is = null;
        try{
            is = ResourceUtils.getInputStreamForPath("classpath:ehcache-shiro.xml");
        }catch (IOException e){
            e.printStackTrace();
        }
        CacheManager cacheManager = new CacheManager(is);
        ehCacheManager.setCacheManager(cacheManager);
        return ehCacheManager;
    }

    //cookie 属性设置
    public SimpleCookie rememberMeCookie() {
        SimpleCookie cookie = new SimpleCookie("rememberMe");
//设置跨域
//cookie.setDomain(domain);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(30 * 24 * 60 * 60);
        return cookie;
    }

    //创建 Shiro 的  cookie 管理对象
    public CookieRememberMeManager rememberMeManager() {
        CookieRememberMeManager cookieRememberMeManager = new
                CookieRememberMeManager();
        cookieRememberMeManager.setCookie(rememberMeCookie());
        cookieRememberMeManager.setCipherKey("1234567890987654".getBytes());
        return cookieRememberMeManager;
    }

    //配置 Shiro 内置过滤器拦截范围
    @Bean
    public DefaultShiroFilterChainDefinition
    shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new
                DefaultShiroFilterChainDefinition();
//设置不认证可以访问的资源
        definition.addPathDefinition("/myController/userLogin", "anon");
        definition.addPathDefinition("/myController/login", "anon");
//设置需要进行登录认证的拦截范围
        definition.addPathDefinition("/**", "authc");
//添加存在用户的过滤器(rememberMe)
        definition.addPathDefinition("/**", "user");
        return definition;
    }
}

Shiro会话管理

  1. SessionManagere

会话管理器,负责创建和管理用户的会话(会话)生命周期,它能够在任何环境中在本地管理用户会话,即使没有Web/Servlet/ejb容器,也一样可以保存会话。默认情况下,Shiro会检测当前环境中现有的会话机制(比如Servlet容器)进行适配,如果没有(比如独立应用程序或者非Web环境),它将会使用内置的企业会话管理器来提供相应的会话管理服务,其中还涉及一个名为SessionDAO的对象。SessionDA0负责Session的持久化操作(CRUD),允许Session数据写入到后端持久化数据库。

  1. 会话管理实现

SessionManager由SecurityManager管理。Shiro提供了三种实现。

(1)→DefaultSessionManager:用于JavaSE环境

(2)→ServletContainerSessionManager:用于web环境,直接使用Servlet容器的会话

(3)→DefaultWebSessionManager:用于web环境,自己维护会话(不使用Servlet容器的会话管理)

  1. 获得session方式

(1) 实现

Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("key","value");

(2)说明

Controller中的request,在shiro过滤器中的doFilerInternal方法,被包装成ShiroHttpSerxletRequest。

SecurityManager.和SessionManager会话管理器决定session来源于ServletRequest还是由Shiro管理的会话。

无论是通过request.getSession或subject..getSession,获取到session,操作session,两者都是等价的。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值