javax.cache按引用存储与按值存储 [英] javax.cache store by reference vs. store by value

查看:131
本文介绍了javax.cache按引用存储与按值存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java缓存的新手,我试图了解按值存储按引用存储之间的区别。

I am new to java caching, I try to understand the difference between store by value vs. store by reference.

我在下面引用了java.cache文档中的段落

复制条目的目的是将它们存储在Cache中,当它们再次存储时从缓存返回的值是为了允许应用程序继续更改键和值的状态而不会对缓存所持有的条目造成副作用。

I have below cited paragraph in java.cache documentation " The purpose of copying entries as they are stored in a Cache and again when they are returned from a Cache is to allow applications to continue mutating the state of the keys and values without causing side-effects to entries held by a Cache. "

上面提到的副作用是什么?以及我们如何选择实际存储方式?

What is the "side-effects" mentioned above? And how do we choose how to store in practice?

推荐答案

这个问题很好,因为答案并不容易。实际的语义在缓存实现中略有不同。

The question is great, since the answer isn't an easy one. The real semantics vary slightly across cache implementations.

按引用存储

缓存存储并返回相同的对象引用。

The cache stores and returns the identical object references.

Object key = ...
Object value = ...
cache.put(key, value);
assert cache.get(key) == value;
assert cache.iterator().next().getKey() == key;

如果在存储值后对键进行了变异,则情况就很模糊。这与使用 HashMap ConcurrentHashMap 相同。

If you mutate the key after storing the value, you have an ambiguous situation. This is identical to using a HashMap or ConcurrentHashMap.

使用按引用存储,以:


  • 最大化性能/最小化处理开销

  • 当数据适合Java堆时

  • 如果要在存储值后对其进行突变。这可能对性能很有用,但不建议这样做,因为您必须注意并发问题,并且用法依赖于按引用存储语义。

  • Maximize performance / minimize processing overhead
  • When the data is fitting into the Java heap
  • If you want to mutate a value after storing it. This can be useful for performance, but isn't a recommended practice, since you have to take care of concurrency issues and the usage relies on the store by reference semantics.

按值存储

似乎也很明显,事情并非如此清楚按值存储的真正含义。根据JCache的Spec负责人:Brian Oliver表示它可以防止缓存数据损坏,Greg Luck表示它是所有内容,但不是按引用存储

Also it seems obvious, things are not so clear what store by value really means. According to the Spec leads of JCache: Brian Oliver said it's protection against cache data corruption, Greg Luck said it's everything but not store by reference.

为此,我确实分析了不同的兼容(通过TCK的方法)JCache实现。键和值对象在传递到缓存时会被复制,但是您不能依赖于缓存中的对象在返回到应用程序时即被复制的事实。

For that matter I did analyze different compliant (means passing the TCK) JCache implementations. Key and value objects are copied when passed to the cache, but you cannot rely on the fact that an object in the cache is copied when returned to the application.

因此假设并非对所有JCache实现都是正确的:

So this assumption isn't true for all JCache implementations:

assert cache.get(key) != cache.get(key);

JCache实现的细节可能会更多。示例:

JCache implementations may even vary more, when it gets into detail. An example:

Map map = cache.getAll(...);
assert map.get(key) != map.get(key);

在期望的语义上这是一个矛盾。我们希望地图内容是稳定的,OTOH缓存将需要在每次访问时返回值的副本。 JCache规范对此没有强制执行具体的语义。

Here is a contradiction in the expected semantics. We would expect that the map contents are stable, OTOH the cache would need to return a copy of the value on every access. The JCache spec doesn't enforce concrete semantics for this. The devil is in the details.

由于密钥是由每个缓存实现在存储时复制的,因此您将获得额外的安全性,即缓存内部数据结构是健全的,但是应用程序仍然

Since the key is copied upon storage by every cache implementation you will get additional safety that the cache internal data structures are sane, but applications still have the chance to break because of shared value references.

我的个人结论(我愿意讨论):

My personal conclusion (I am open for discussion):

由于按引用存储是可选的JCache功能,因此请求它意味着您将限制应用程序使用的缓存实现的数量。如果您不依赖按引用存储语义,请始终使用按值存储

Since store by reference is an optional JCache feature, requesting it, would mean you limit the number of cache implementations your application works with. Use store by value always, if you don't rely on store by reference semantics.

但是,不要不能使您的应用程序依赖于您认为按值存储可能获得的语义。在将其引用交给高速缓存或从高速缓存中检索其引用之后,切勿更改任何对象。

However, don't make your application depend on the semantics you think you might get with store by value. Never mutate any object after handing its reference to the cache or after retrieving its reference from the cache.

如果仍有疑问,请咨询您的缓存供应商。恕我直言,记录实施细节的良好做法。一个很好的例子(因为我花了很多心思……)是 JCache cache2k用户指南中的章节

If there is still doubt, ask your cache vendor. IMHO its good practice to document implementation details. A good example (since I spent much thought in it...) is the JCache chapter in the cache2k user guide

这篇关于javax.cache按引用存储与按值存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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