在Java中实现定期刷新的Cache [英] Implementing a periodically refreshing Cache in Java

查看:247
本文介绍了在Java中实现定期刷新的Cache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是对存储在持久性数据库中的数据进行内存缓存。

My use case is to maintain an in-memory cache over the data stored in a persistent DB.

我使用数据填充条目列表/映射在用户界面上。在任何给定时间,UI上显示的数据都应尽可能进行更新(这可以通过缓存的刷新频率来完成)。

I use the data to populate a list/map of entries on the UI. At any given time, the data displayed on the UI should be as updated as it is possible (well this can be done by the refresh frequency of the cache).

主要常规高速缓存实现与该特定高速缓存之间的区别在于,它需要以固定的间隔大量刷新所有元素,因此与LRU类型的高速缓存完全不同。

Major difference between a regular cache implementation and this particular cache is that it needs a bulk refresh of all the elements at regular intervals and hence is pretty different from an LRU kind of cache.

我需要用Java来实现,如果有现成的框架可以用来围绕它们构建它,那就太好了。

I need to do this implementation in Java and it will be great if there are any existing frameworks which can be utilized to get this built around them.

我已经探索了Google Guava缓存库,但它更适合按条目刷新,而不是批量刷新。没有简单的API可以刷新整个缓存。

I have explored the Google Guava cache library but it is more suited to a per entry refresh rather than a bulk refresh. There are no simple APIs which do a refresh on the whole cache.

任何帮助将不胜感激。

此外,如果有可能增量执行刷新,那将是很棒的,因为刷新整个高速缓存时出现的唯一限制是,如果高速缓存的大小很大,则内存堆的大小至少应为两倍。缓存以加载新条目并将旧地图替换为新地图。如果缓存是增量缓存或分块刷新(刷新大小相等),那会很棒。

Also, if it is possible to incrementally do the refresh, it shall be great because the only limitation which arises while refreshing the whole cache is that if the cache is very big in size, then the memory heap should be atleast twice the size of the cache in order to load the new entries and replace the old map with the new one. If the cache is incremental or there is a chunked refresh (refresh in equal sizes) it will be great.

推荐答案

EHCache 是一个功能齐全的Java缓存库。我想它们会为您提供帮助。

EHCache is a pretty full-featured java caching library. i would imagine they have something which would work for you.

为了对缓存进行增量重装(在大多数缓存中都可以使用),只需遍历当前加载的条目并强制刷新它们。 (您可以在后台调度程序上运行此任务。)

In order to do an incremental reload of a cache (which would work on most any cache), just iterate through the currently loaded entries and force refresh them. (you could run this task on a background scheduler).

作为强制重新加载整个缓存的替代方法,EHCache可以指定直播,因此,如果条目太陈旧,则会自动重新加载。

As an alternative to forcing the entire cache to reload, EHCache has the ability to specify a "time-to-live" for an entry, so entries will automatically be reloaded if they are too stale.

这篇关于在Java中实现定期刷新的Cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆