多级进程内缓存的更好选择是什么? [英] What is the better option for a multi-level, in-process cache?

查看:162
本文介绍了多级进程内缓存的更好选择是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的spring启动应用程序中,我需要实现一个进程内多级缓存
这是一个需要缓存的数据示例:

In my spring boot application, I need to implement an in-process multi-level cache Here is an example of the data, which needs to be cached:

客户名称(密钥,字符串)
- 数据实体名称(密钥,字符串)
--config-1(value,JSONObject)
--config-2(value ,JSONObject)

customer name (key, string) --data entity name (key, string) --config-1 (value, JSONObject) --config-2 (value, JSONObject)

我计划有几百个客户条目,每条条目最多有一百个配置JSONObjects

I'm planning on having a few hundreds customer entries, each having up to a hundred "config" JSONObjects

我目前正在查看ehcache:

I'm currently looking at ehcache:

Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);

在这种情况下,我会使用Customer_Name代替key1和我的客户 代替value1,但我需要构建一个层次结构:

In this context, I would use "Customer_Name" in place of "key1", and "My Customer" in place of "value1", but them I would need to build a hierarchy:

customer
 -data entity
   -config

我不知道如何用ehcache做到这一点。
我也不确定我是否有更好的选择。

I'm not sure how to do it with ehcache. I'm also not sure whether there are better choices for what I'm trying to do.

有没有人实现过这样的多级分层缓存ehcache或任何其他库?

Has anyone implemented such a multi-level hierarchical cache with ehcache or any other library?

推荐答案

对于表示法,我使用类似地图的缓存: value = Cache .get(key)这比EHCache2更常见元素

For notation I use a map-like cache: value = Cache.get(key) which is more common then the EHCache2 Element

选项1:构造复合关键对象

 class CustomerConfigurationKey {
    String customerKey;
    String dataEntityKey;
    // equals() and hashCode()
 }

这很漂亮标准键/值存储包括普通地图。我确实在 cache2k快速入门

This is pretty standard key/value stores including plain maps. I did address this in cache2k Quick Start.

选项2:使用多级缓存

将缓存放入缓存中并访问如下: data.get(customerKey).get(dataEntityKey)

Put a cache inside a cache and access like: data.get(customerKey).get(dataEntityKey).

您可以在 cache2k基准DateFormattingBenchmark

如果你在第一级有一个小集合,这只能很好地工作。在您的情况下,每个客户最终会得到一个单独的缓存,这会花费很多。所以,这只是为了完整性,在您的方案中没有真正的选择。

This only works nicely if you have a small set at the first level. In your case you would end up with a separate cache per customer, which is to costly. So, this is only for completeness, no real option in your scenario.

选项3:使用第二级地图

使用 Cache< String,Map< String,JSONObject> 构建单个缓存。

如果通常所有客户数据都在短时间内使用,那么在更精细的层面上缓存是没有意义的,因为客户的所有数据通常都会在内存中。另一个例子:当客户不再活动时,缓存将过期,所有客户数据都可以从内存中删除。

If typically all the customer data is used in a short interval, it does not make sense to cache on a finer level, since all data of a customer will be typically in memory anyways. Another example: when a customer is not active any more, the cache would expire and all of the customer data could be removed from memory.

更新地图的单个条目将有你需要正确解决的并发问题,例如通过在缓存中复制和放置不可变的映射,或者使用 ConcurrentHashMap

Updating single entries of the map will have concurrency issues that you need to properly address, e.g. by copying and putting only an immutable map in the cache or by using a ConcurrentHashMap.

这篇关于多级进程内缓存的更好选择是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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