ehcache.xml
时间: 2023-12-16 16:24:47 浏览: 32
ehcache.xml是用来定义Ehcache的配置信息的,它是定义CacheManagerehcache.xml是用来定义Ehcache的配置信息的,它是定义CacheManager的配置信息的。在不指定配置信息参数创建CacheManager时,CacheManager将首先在类路径的根目录下寻找一个叫ehcache.xml的文件作为CacheManager的配置文件。如果不存在这样的文件则将使用封装在ehcache jar包中的ehcahce-failsafe.xml文件作为创建CacheManager的默认配置信息。除了使用Configuration作为参数外,使用其它参数构造CacheManager都是基于xml格式的配置信息的。当我们使用xml配置文件作为CacheManager的配置信息时,我们的文件名不一定叫ehcache.xml,这里只是把ehcache.xml文件作为这一类文件的一个代表,它们拥有共同的文档结构。在ehcache.xml文件中,我们可以定义CacheManager的属性和Cache的属性,比如缓存的最大容量、过期时间等等。同时,我们也可以在ehcache.xml文件中定义多个Cache,每个Cache都有自己的属性和配置。
相关问题
ehcache.xml配置文件
你可以使用以下示例来配置 Ehcache 的 ehcache.xml 文件:
```xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/ehcache" />
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskSpoolBufferSizeMB="30"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="true">
</defaultCache>
<!-- 可以在此处添加其他缓存的配置 -->
</ehcache>
```
在此示例中,`<diskStore>` 元素指定了磁盘存储路径。`<defaultCache>` 元素定义了默认缓存的属性,如最大堆内条目数(maxEntriesLocalHeap)、空闲时间(timeToIdleSeconds)、生存时间(timeToLiveSeconds)等。
你还可以在 `<ehcache>` 元素内添加其他缓存的配置,如下所示:
```xml
<cache name="myCache"
maxEntriesLocalHeap="1000"
eternal="true"
memoryStoreEvictionPolicy="LFU" />
```
在这个示例中,`<cache>` 元素定义了一个名为 "myCache" 的缓存,具有最大堆内条目数为 1000、永久有效(eternal=true)以及最近最少使用(LFU)的内存存储逐出策略。
请根据你的具体需求修改和配置 ehcache.xml 文件。
shiro-ehcache.xml 转为配置类
将 shiro-ehcache.xml 转换为配置类可以使用以下步骤:
1. 创建一个 EhCacheManager 的配置类 ShiroEhcacheConfig。
```java
@Configuration
public class ShiroEhcacheConfig {
@Bean
public EhCacheManager ehCacheManager() {
EhCacheManager ehCacheManager = new EhCacheManager();
ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
return ehCacheManager;
}
}
```
2. 在 shiro.ini 中使用配置类。
```ini
[main]
cacheManager = com.example.config.ShiroEhcacheConfig#ehCacheManager
```
这里使用了 JavaConfig 的方式,将 EhCacheManager 的配置文件路径设置为 `classpath:ehcache.xml`。在 shiro.ini 中通过配置类的方式引入 EhCacheManager。这样就可以将 shiro-ehcache.xml 转换为配置类了。