Spring-EhCache配置实例

简介: 使用配置Maven依赖 <!--ehcache--> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations&

使用配置

Maven依赖

 <!--ehcache-->
 <dependency>
     <groupId>com.googlecode.ehcache-spring-annotations</groupId>
     <artifactId>ehcache-spring-annotations</artifactId>
     <type>jar</type>
     <version>1.2.0</version>
 </dependency>
 <dependency>
     <groupId>net.sf.ehcache</groupId>
     <artifactId>ehcache</artifactId>
     <version>2.10.1</version>
 </dependency>

 <!--guava-->
 <dependency>
     <groupId>com.google.guava</groupId>
     <artifactId>guava</artifactId>
     <version>19.0</version>
 </dependency>

 <!--spring-->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-beans</artifactId>
     <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
     <version>3.1.4.RELEASE</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>4.2.0.RELEASE</version>
 </dependency>

EhCache配置

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
        updateCheck="false">
    <defaultCache eternal="false"
                  maxElementsInMemory="100000"
                  overflowToDisk="false"
                  diskPersistent="false"
                  timeToIdleSeconds="86400"
                  timeToLiveSeconds="86400"
                  memoryStoreEvictionPolicy="FIFO" />
    <cache name="cityEhCache"
           eternal="false"
           maxElementsInMemory="100000"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="86400"
           timeToLiveSeconds="86400"
           memoryStoreEvictionPolicy="FIFO" />
    <cache name="areaEhCache"
           eternal="false"
           maxElementsInMemory="100000"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="86400"
           timeToLiveSeconds="86400"
           memoryStoreEvictionPolicy="FIFO" />
    <!--  
        name:缓存名称。  
        maxElementsInMemory:缓存最大个数。  
        eternal:对象是否永久有效,一但设置了,timeout将不起作用。  
        timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。  
        timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。  
        overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。  
        diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。  
        maxElementsOnDisk:硬盘最大缓存个数。  
        diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.  
        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。  
        memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。  
        clearOnFlush:内存数量最大时是否清除。  
    -->  
</ehcache>

ehcache-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"></bean>
    <bean id="cityEhCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean" p:name="cityEhCache" p:cacheManager-ref="cacheManager" />
    <bean id="areaEhCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean" p:name="areaEhCache" p:cacheManager-ref="cacheManager" />
</beans>

代码实例

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class CityCacheService {

    @Autowired
    @Qualifier("cityEhCache")
    Ehcache cityEhCache;

    public Object get(Long key) {
        Element element = cityEhCache.get(key);
        if (element == null) {
            return null;
        }
        return element.getObjectValue();
    }

    public boolean refreshCache() {
        //refresh cache
        //cityEhCache.put(new Element(objectId, new Object()));
    }
}
目录
相关文章
|
机器学习/深度学习 人工智能 算法
通义千问Qwen-72B-Chat大模型在PAI平台的微调实践
本文将以Qwen-72B-Chat为例,介绍如何在PAI平台的快速开始PAI-QuickStart和交互式建模工具PAI-DSW中高效微调千问大模型。
Cron表达式每隔两小时执行一次
Cron表达式每隔两小时执行一次
622 1
|
监控 NoSQL Java
云服务器Redis集群部署及客户端通过公网IP连接问题
目录 1、配置文件 2、启动服务并创建集群 (1)启动6个Redis服务 (2)通过客户端命令创建集群 3、客户端连接 (1)客户端配置 (2)测试用例 (3)错误日志分析 4、问题解决 (1)查redis.conf配置文件 (2)修改配置文件 (3)重新启动Redis服务并创建集群 5、故障转移期间Lettuce客户端连接问题 (1)测试用例 (2)停掉其中一个master节点,模拟宕机 (3)解决办法 1)更换Redis客户端 2)Lettuce客户端配置Redis集群拓扑刷新
|
5月前
|
人工智能 自然语言处理 文字识别
快手封号多久能恢复正常?
快手账号封禁恢复全流程技术解析
ly~
|
数据库 数据库管理
数据库的事务处理机制有哪些优点?
数据库的事务处理机制具备多种优势:首先,它能确保数据一致性,通过原子性保证所有操作全成功或全失败,利用完整性约束维护数据的有效性;其次,增强了系统可靠性,提供故障恢复能力和正确处理并发操作的功能;最后,简化了应用程序开发工作,将操作封装为逻辑单元并集中处理错误,降低了开发复杂度。
ly~
312 1
|
自然语言处理 C# 图形学
​一款开源的.NET程序集反编译、编辑和调试神器
本文介绍了.NET反编译和调试工具dnSpyEx的使用方法。dnSpyEx是dnSpy的非官方Fork版本,支持.NET Framework、.NET Core和Unity程序集的调试和编辑,具有多种语言界面。主要功能包括:浅色、蓝色和深色主题,调试支持,代码编辑以及多语言支持。用户可以从GitHub下载并直接运行dnSpyEx,无需安装。通过创建测试项目,编译成dll文件,然后使用dnSpyEx进行调试和编辑程序集中的代码和IL指令。此外,文章还提供了项目源码地址和相关优秀项目的链接。
459 0
|
消息中间件 存储 监控
RocketMQ的性能优势?
【8月更文挑战第29天】RocketMQ的性能优势?
466 2
|
消息中间件 Java Spring
实现Spring Boot与RabbitMQ消息中间件的无缝集成
实现Spring Boot与RabbitMQ消息中间件的无缝集成
|
人工智能 程序员 测试技术
AI程序员Devin在软件开发中的性能评估
【2月更文挑战第29天】AI程序员Devin在软件开发中取得突破,成功解决SWE-bench基准测试13.86%的问题,超出未辅助基线1.96%。展示强大编程能力,但处理复杂任务成功率仅4.80%,表明局限性。Devin能执行多步计划和自我纠错,但在理解复杂逻辑和用户偏好上需改进。在测试驱动开发场景下,成功通过率提升至23%,显示出合作潜力。然而,AI在软件工程领域仍有很大改进空间。
393 1
AI程序员Devin在软件开发中的性能评估
|
前端开发 JavaScript API
Github 上 8 个很棒的 React 项目
Github 上 8 个很棒的 React 项目
6070 0