带有项目/实体集合的Spring Cache [英] Spring Cache with collection of items/entities

查看:126
本文介绍了带有项目/实体集合的Spring Cache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Cache,我在其中传递一组键,返回是一个实体列表。我想让缓存框架理解返回列表中的每个元素都要使用相应的代码进行缓存。目前,似乎关键是整个列表,如果我在后续调用中缺少一个键,它将尝试再次重新加载整个集合。

I am using Spring Cache, where I pass in a collection of keys, and the return is a list of entities. I would like to have the caching framework understand that each element in the return list is to be cached with the corresponding code. At the moment, it seems that the key is the whole list, and if I am missing a key in the subsequent call, it'll try to reload the whole collection again.

@Override
@Cacheable(value = "countries")
public List<Country> getAll(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}

另一种可能是返回是地图,同样我想要缓存要足够聪明,只能查询以前从未查询过的项目,还要用关键字对每个项目进行缓存。

another possibility is that the return is a map, similarly I would like the cache to be intelligent enough to only query for items that were never queried before, also to cache them each item with its key.

@Override
@Cacheable(value = "countries")
public Map<String,Country> getAllByCode(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}

假设国家类看起来像这样:

Suppose the country class looks like this:

class Country{
  String code; 
  String fullName;
  long id;

... // getters setters constructurs etc.. 
}

这可以用Spring Cache吗?

Is this possible with Spring Cache?

推荐答案

事实上,即使用 Spring的缓存抽象 ,但不是开箱即用(OOTB)。基本上,您必须自定义 Spring的缓存基础结构(在下面进一步说明)

In fact, it is possible, even with Spring's Caching Abstraction, but not out-of-the-box (OOTB). Essentially, you must customize Spring's caching infrastructure (Explained further below)

默认 Spring的缓存基础结构使用整个 @Cacheable 方法参数参数作为缓存key,如 here 。当然,您也可以使用 SpEL表达式或自定义 KeyGenerator 实现自定义密钥解析,如这里

By default, Spring's caching infrastructure uses the entire @Cacheable method parameter arguments as the cache "key", as explained here. Of course you can also customize the key resolution using either a SpEL Expression or with a custom KeyGenerator implementation, as explained here.

仍然, 分解参数参数的集合或数组以及 @Cacheable 方法的返回值到单个缓存条目中(即基于数组/集合或Map的键/值对)。

Still, that does not break up the collection or array of parameter arguments along with the @Cacheable method's return value into individual cache entries (i.e. key/value pairs based on the array/collection or Map).

为此,您需要自定义实现 Spring的 CacheManager (取决于您的缓存策略/提供程序)和缓存接口。

For that, you need a custom implementation of Spring's CacheManager (dependent on your caching strategy/provider) and Cache interfaces.


注意:具有讽刺意味的是,这将是我第三次回答同样的问题,首先此处,然后此处现在在这里,:-)。无论如何......

NOTE: Ironically, this will be the 3rd time I have answered nearly the same question, first here, then here and now here, :-). Anyway...

我已经更新/清理了我的例子(有点)。

I have updated/cleaned up my example (a bit) for this posting.

请注意我的示例扩展并自定义 Spring Framework 本身提供了/cache/concurrent/ConcurrentMapCacheManager.htmlrel =noreferrer> ConcurrentMapCacheManager

Notice that my example extends and customizes the ConcurrentMapCacheManager provided in the Spring Framework itself.

从理论上讲,您可以扩展/自定义任何 CacheManager 实施,例如 Redis的 em> Spring Data Redis ,此处 source ),或 Pivotal GemFire的 CacheManager Spring Data GemFire 中的code>,这里 source )。 Pivotal GemFire 的开源版本是 Apache Geode ,它具有相应的 Spring Data Geode 项目,( CacheManager的来源 in Spring Data Geode ,它与SD GemFire基本相同。当然,您可以将此技术应用于其他缓存提供商... Hazelcast,Ehcache等。

Theoretically, you could extend/customize any CacheManager implementation, like Redis's in Spring Data Redis, here (source), or Pivotal GemFire's CacheManager in Spring Data GemFire, here (source). The open source version of Pivotal GemFire is Apache Geode, which has a corresponding Spring Data Geode project, (source for CacheManager in Spring Data Geode, which is basically identical to SD GemFire). Of course, you can apply this technique to other caching providers... Hazelcast, Ehcache, etc.

然而,工作的真正内容由 custom实现(或更具体地说,基类 Spring的 缓存界面。

However, the real guts of the work is handled by the custom implementation (or mores specifically, the base class) of Spring's Cache interface.

无论如何,希望来自我的示例,您将能够弄清楚您的应用程序需要做什么来满足您的应用程序的缓存要求。

Anyway, hopefully from my example, you will be able to figure out what you need to do in your application to satisfy your application's caching requirements.

此外,您可以应用相同的方法来处理地图,但我会把它作为练习留给你,; - )。

Additionally, you can apply the same approach to handling Maps, but I will leave that as an exercise for you, ;-).

希望这会有所帮助!

干杯,
John

Cheers, John

这篇关于带有项目/实体集合的Spring Cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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