Ehcache

Spring @Cacheable methods with lists

我的梦境 提交于 2019-12-05 20:29:50
问题 I'm using latest Ehcache in my Spring 4.1.4 application. What I have is: class Contact{ int id; int revision; } @Cacheable("contacts") public List<Contact> getContactList(List<Integer> contactIdList) { return namedJdbc.queryForList("select * from contact where id in (:idlist)", Collections.singletonMap("idlist", contactIdList)); } @CachePut(value="contact", key = "id") public void updateContact(Contact toUpdate) { jdbctemplate.update("update contact set revision = ? where id = ?", contact

MyBatis(第三方缓存整合原理&ehcache适配包下载)

你。 提交于 2019-12-05 20:18:54
EhCache是一个纯java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。   MyBatis是一个与数据库交互的框架,不是专业做缓存的,但是留了接口Cache;我们可以自定义缓存或者使用其他缓存。 第三方缓存整合:jar包可以在 https://github.com/mybatis/ 这个网址上下载   1)、导入第三方缓存jar包即可;     ehcache-core-2.6.8.jar:ehcache的核心包     slf4j-api-1.6.1.jar:日志有关的包     slf4j-log4j12-1.6.2.jar:日志有关的包   2)、导入与第三方缓存整合的适配包;官方有;     mybatis-ehcache-1.0.3.jar   3)、编写 ehcache.xml 配置文件,放在类路径下。   4)、在mapper.xml中使用自定义缓存 <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache> 若想在命名空间中共享相同的缓存配置和实例。可以使用cache-ref元素来引用另一个缓存。 <!--引用缓存:namespace指定和哪个名称空间下的缓存一样--> <cache-ref namespace="com.atguigu

How to change a EhCache members expiry time programmatically

守給你的承諾、 提交于 2019-12-05 20:00:52
I would like to change the expiry or set the expiry time of a member of an EhCache via Java code. I know when the object should expire, but I'm unsure how to achieve this. I know I can set it for the whole cache, e.g. Cache cache = manager.getCache("sampleCache"); CacheConfiguration config = cache.getCacheConfiguration(); config.setTimeToIdleSeconds(60); config.setTimeToLiveSeconds(120); config.setMaxEntriesLocalHeap(10000); config.setMaxEntriesLocalDisk(1000000); Can someone suggest how I do this for a specific member? In Ehcache 2.x, you can set expiry time on the Element you insert in the

The configured limit of 1.000 object references was reached while attempting to calculate the size of the object graph

て烟熏妆下的殇ゞ 提交于 2019-12-05 15:36:35
问题 I have a jhipster project and I added some entities. My services are very slow because this warning message: The configured limit of 1.000 object references was reached while attempting to calculate the size of the object graph. Severe performance degradation could occur if the sizing operation continues. This can be avoided by setting the CacheManger or Cache <sizeOfPolicy> elements maxDepthExceededBehavior to "abort" or adding stop points with @IgnoreSizeOf annotations. If performance

缓存雪崩 穿透 击穿

 ̄綄美尐妖づ 提交于 2019-12-05 11:46:51
缓存雪崩   对于系统 A,假设每天高峰期每秒 5000 个请求,本来缓存在高峰期可以扛住每秒 4000 个请求,但是缓存机器意外发生了全盘宕机。缓存挂了,此时 1 秒 5000 个请求全部落数据库,数据库必然扛不住,它会报一下警,然后就挂了。此时,如果没有采用什么特别的方案来处理这个故障,DBA 很着急,重启数据库,但是数据库立马又被新的流量给打死了。这就是缓存雪崩。   缓存雪崩的事前事中事后的解决方案如下。 事前:redis 高可用,主从+哨兵,redis cluster,避免全盘崩溃。 事中:本地 ehcache 缓存 + hystrix 限流&降级,避免 MySQL 被打死。 事后:redis 持久化,一旦重启,自动从磁盘上加载数据,快速恢复缓存数据。      用户发送一个请求,系统 A 收到请求后,先查本地 ehcache 缓存,如果没查到再查 redis。如果 ehcache 和 redis 都没有,再查数据库,将数据库中的结果,写入 ehcache 和 redis 中。   限流组件,可以设置每秒的请求,有多少能通过组件,剩余的未通过的请求,怎么办?走降级!可以返回一些默认的值,或者友情提示,或者空白的值。   好处: 数据库绝对不会死,限流组件确保了每秒只有多少个请求能通过。 只要数据库不死,就是说,对用户来说,2/5 的请求都是可以被处理的。 只要有 2/5

When does the CPU benefit of having an Hibernate 2nd level cache outweigh the initial hit

你说的曾经没有我的故事 提交于 2019-12-05 11:22:34
When does the CPU benefit of having an object added to Hibernate 2nd level object cache outweigh the initial hit. I am currently using Hibernate without 2nd level cache. This is for an application that processes music files ( www.jthink.net/songkong ) and it uses Hibernate so it can scale with more data, i.e it can process 100,000 songs with little more memory than 1000 songs. Once the songs have been processed then those songs are of no interest (unless the user runs Undo) As I understand it if I enable 2nd level cache (for my song class) then the initial write of the song to cache will use

Grails ehcache plugin - Another unnamed CacheManager already exists in the same VM

戏子无情 提交于 2019-12-05 10:43:32
I run my Grails application using ehcache for my 2nd level Cache and it works. I installed the ehcache plugin + cache plugin and then it doesn't. I tried almost all solutions from the internet and found no solution I keep getting Another unnamed CacheManager already exists in the same VM . One of the possible solutions is to set p:shared=true in the EhCacheManagerFactoryBean , this works if I use an old plugin "springcache plugin from grails" but with the new plugin they use a modified version of this manager and the property shared is not available. I tried defining a new ehcache.xml file but

如何设计一个本地缓存

耗尽温柔 提交于 2019-12-05 09:55:35
前言 最近在看Mybatis的源码,刚好看到缓存这一块,Mybatis提供了一级缓存和二级缓存;一级缓存相对来说比较简单,功能比较齐全的是二级缓存,基本上满足了一个缓存该有的功能;当然如果拿来和专门的缓存框架如ehcache来对比可能稍有差距;本文我们将来整理一下实现一个本地缓存都应该需要考虑哪些东西。 考虑点 考虑点主要在数据用何种方式存储,能存储多少数据,多余的数据如何处理等几个点,下面我们来详细的介绍每个考虑点,以及该如何去实现; 1.数据结构 首要考虑的就是数据该如何存储,用什么数据结构存储,最简单的就直接用Map来存储数据;或者复杂的如redis一样提供了多种数据类型哈希,列表,集合,有序集合等,底层使用了双端链表,压缩列表,集合,跳跃表等数据结构; 2.对象上限 因为是本地缓存,内存有上限,所以一般都会指定缓存对象的数量比如1024,当达到某个上限后需要有某种策略去删除多余的数据; 3.清除策略 上面说到当达到对象上限之后需要有清除策略,常见的比如有LRU(最近最少使用)、FIFO(先进先出)、LFU(最近最不常用)、SOFT(软引用)、WEAK(弱引用)等策略; 4.过期时间 除了使用清除策略,一般本地缓存也会有一个过期时间设置,比如redis可以给每个key设置一个过期时间,这样当达到过期时间之后直接删除,采用清除策略+过期时间双重保证; 5.线程安全

How Can I Get Ehcache To Keep Heap Size Bytes Statistics For Unbounded Caches?

不想你离开。 提交于 2019-12-05 08:56:30
I'm using Ehcache (version 2.7.1) and would like to periodically retrieve stats such as number of elements in the cache and the size of the cache in bytes. The problem that I'm running into though is that using net.sf.ehcache.statistics.StatisticsGateway.getLocalHeapSizeInBytes() (StatisticsGateway retrieved by calling net.sf.ehcache.Ehcache.getStatistics() ) takes a very long time with 15000 elements with a total size of about 536MB. In one example on my local machine, it took over 21 seconds to get this statistic. After I experienced this I thought, "How in the world does the

阿里一面:关于【缓存穿透、缓存击穿、缓存雪崩、热点数据失效】问题的解决方案

微笑、不失礼 提交于 2019-12-05 08:08:27
1 前言 在我们的平常的项目中多多少少都会使用到缓存,因为一些数据我们没有必要每次查询的时候都查询到数据库。 特别是高QPS的系统,每次都去查询数据库,对于你的数据库来说将是灾难。 今天我们不牵涉多级缓存的知识,就把系统使用到的缓存方案,不管是一级还是多级的都统统成为缓存,主要是为了讲述使用缓存的时候可能会遇到的一些问题以及一些解决办法。 我们缓存时,我们的业务系统大概的调用流程如下图片: 当我们查询一条数据时,先去查询缓存,如果缓存有就直接返回,如果没有就去查询数据库,然后返回。这种情况下就可能会出现一些现象。 2 缓存穿透 2.1 什么是缓存 正常情况下,我们去查询数据都是存在的。 那么请求去查询一条压根儿数据库中根本就不存在的数据,也就是缓存和数据库都查询不到这条数据,但是请求每次都会打到数据库上面去。 这种查询不存在数据的现象我们称为缓存穿透。 2.2 穿透带来的问题 试想一下,如果有黑客会对你的系统进行攻击,拿一个不存在的id 去查询数据,会产生大量的请求到数据库去查询。可能会导致你的数据库由于压力过大而宕掉。 2.3 解决办法 2.3.1 缓存空值 之所以会发生穿透,就是因为缓存中没有存储这些空数据的key。从而导致每次查询都到数据库去了。 那么我们就可以为这些key对应的值设置为null 丢到缓存里面去。后面再出现查询这个key 的请求的时候,直接返回null。 这样