Ehcache(1)

2024-08-30 10:08
文章标签 ehcache

本文主要是介绍Ehcache(1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    Ehcache是一个Java的缓存框架,当应用需要使用到缓存的时候,可以使用,它的操作也方便,下面介绍一下它的用法

一.CacheManager

    1.配置文件

        首先在src下面放一个ehcache.xml文件,内容如下
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" ><diskStore path="java.io.tmpdir"/><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"maxElementsOnDisk="10000000"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"/><cache name="cache1"maxElementsInMemory="10000"maxElementsOnDisk="1000"eternal="false"overflowToDisk="true"timeToIdleSeconds="20"timeToLiveSeconds="20"memoryStoreEvictionPolicy="LFU"/></ehcache>
        后面再仔细介绍这个文件

    2.cacheManager的创建

    CacheManager,顾名思义,是用来管理Cache的,每个CacheManager可以对应多个Cache,所以第一步是获取一个CacheManager的对象,CacheManager有几个构造方法

    

    CacheManager(Configuration):该构造方法接收一个Configuration对象

    CacheManager(String):该构造方法接收一个有配置文件路径信息的字符串

    CacheManager(URL):该构造方法接收一个指向配置文件的URL对象

    CacheManager(URL):该构造方法接收一个指向配置文件的输入流

    CacheManager():该构造方法无参数,默认加载路径下的名为ehcache.xml的配置文件


    创建一个CacheManager的对象有几种方法:

        1).使用new创建一个对象

            该方法创建一个全新的对象

        2).使用CacheManager的静态方法newInstance方法创建一个对象

            CacheManager中有5个newInstance方法,参数类似和构造方法一样,newInstance方法首先会看是否存在同名的CacheManager对象,如果             存在,则返回,没有则创建一个新的对象

           源码如上,所有的newInstance方法都会调用这个newInstance方法,其中判断是否有同名的对象,没有则使用new创建            一个新的对象

        3).使用CacheManager的静态方法create方法创建一个对象

            create方法也有5个,和newInstance对应,CacheManager中有一个名为singleton的对象,每次调用create方法时,首先判断该对象是否             已经存在,如果已经存在则返回,如果不存在,则调用newInstance,并把返回的值赋给singleton,再返回该对象

        4).使用CacheManager的静态方法getInstance方法创建一个对象

            这个方法调用create方法

    3.cacheManager操作Cache   

	public static void main(String[] args) throws IOException {CacheManager cacheManager = new CacheManager();//通过name获取对应的cacheCache cache1 = cacheManager.getCache("cache1");// 参数为配置文件中<cache>中的name// 添加一个cache到CacheManager中cacheManager.addCache("cache2");// 添加一个name为cache2的节点,属性继承defaultCacheSystem.out.println(cacheManager.getActiveConfigurationText());// 打印出ehcache.xml文件中的内容,会发现多出一个cache2节点Cache cache3 = new Cache("cache3", 10001, true, true, 100, 100);cacheManager.addCache(cache3);//必须添加到CacheManager才有效System.out.println(cacheManager.getActiveConfigurationText());// 打印出ehcache.xml文件中的内容,会发现多出一个cache3节点cache3.put(new Element("test",new Integer(1)));// 将一个对象放到cache3中,名为testcache1.put(new Element("test",new Integer(1)));// 将一个对象放到cache1中,名为test// 清除所有cache中的所有对象cacheManager.clearAll();System.out.println(cache1.get("test"));// 清除后为null,下同System.out.println(cache3.get("test"));//移除名为cache2的cachecacheManager.removeCache("cache2");//移除所有cachecacheManager.removalAll();}





这篇关于Ehcache(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1120512

相关文章

Ehcache(2)

二.配置文件ehcache.xml     首先,我们需要一个配置文件ehcache.xml(名字可以改变),主要内容如下 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"></ehca

SpringBoot进阶篇4:Spring Boot中EhCache应用

1、SpringBoot Cache       Spring Boot 本身提供了一个基于ConcurrentHashMap 的缓存机制,也集成了EhCache2.x、JCache CJSR-107、EhCache3.x、Infinispan ),还有Couchbase、Redis 等。Spring Boot应用通过注解的方式使用统一的缓存,只需在方法上使用缓存注解即可。 【注意】在Spri

spring+ehcache 缓存

1.在web.xml中添加 <context-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath:resources/application.xml</param-value>   </context-param> 2.在application.xml中添加 <?xml ve

spring集成redis(ehcache缓存改成redis)

1.先准备要需要的四个和redis相关的jar包 commons-pool2-2.4.2.jar、jedis-2.9.0.jar、spring-data-redis-1.8.16.RELEASE.jar、spring-data-commons-1.13.16.RELEASE.jar 把这个三个jar引进项目里 2.增加一个redis.propertes #redis的服务器地址red

Ehcache Java 缓存框架

详解 下图是 Ehcache 在应用程序中的位置: Ecache 是一个广泛使用的 Java 缓存框架,能够有效提升应用性能,并减少与后端数据库的交互次数。它采用了一系列高级缓存策略,包括内存缓存、磁盘缓存、分布式缓存等,并提供了丰富的 API 和工具类,可以方便地完成缓存的读写和管理。快速:Ecache 采用了一系列高效的缓存策略,能够实现快速的数据访问和读写,从而提高应用程

Mybatis与ehcache缓存框架整合

1 分布式缓存 目的:将缓存数据进行分布式的管理作用:通过mybatis和ehcache整合,可以把缓存数据的管理托管ehcache在mybatis中提供一个cache接口,只要实现cache接口就可以把缓存数据灵活的管理起来。 2 整合步骤  下载jar包  配置ehcache.xml <ehcache xmlns:xsi="http://www.w3.org/200

关于EHcache缓存

1、简介 非常简单,而且易用。     ehcache 是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,而且是hibernate 默认的缓存provider。ehcache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 ehcache可以直接使用。也可以和Hibernate对象/关系框架结合使用。还可以做S

springboot 整合EHcache 实现缓存技术

一 ehcache的概述 1.@Cacheable注解的作用 @Cacheable 对当前的对象做缓存处理 @Cacheablepublic List<Users> findUserAll() {return this.userRepository.findAll();}@Cacheable 作用:把方法的返回值添加到 Ehcache 中做缓存 Value 属性:指定一个 Ehcache

springboot的ehcache的@CacheEvict清除缓存

一 @CacheEvict的作用 1.1 总体概述     @CacheEvict是用来标注在需要清除缓存元素的方法或类上的。 当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。 @CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的语义与@Cache

解决springboot中@ehcache不起作用的问题

一 问题描述 问题描述前,请允许我,吐槽一下,这个问题困扰了一天,下面在业务层下的代码中,@cache注解标注了value的名称,但是没有设置key,且传递的参数为 CrawlRecordParamVo crawlRecordParamVo ,是一个对象,每次调用都要查询一次数据库,没有启动缓存的作用 原因在于没有制定key,因为:对于使用@Cacheable标注的方法,Spring在每