集群上的hibernate L2 缓存

Hibernate L2 Cache on cluster

Q1:
只有这些 vendor支持集群上的 Hibernate L2 缓存,我说得对吗?

  • Hibernate兵马俑(商业)
  • SwarmCache(自 2003 年以来未发布)
  • JBoss 缓存 1.x
  • JBoss 缓存 2

Q2:
Hibernate L2 缓存有什么替代品吗? (也许是一些数据库缓存?)


Q1。 EhCache 作为分布式的 Hibernate L2 Cache 工作得非常好。
我们将其用于我们的项目。

Q2。多个缓存是可能的。

  • 所有的数据库已经在内部做了很多缓存,所以不用担心这部分。

However, the problem with the database
cache is that it is physically on the
database server, so each query
involves a network call (latency,
bandwith ..). That's the whole point
of the caches on the application
server.

  • Hibernate L2 缓存有一些细节:

    • 非常容易使用(没有代码,很少配置)
    • 从概念上讲在实体级别(这是非常细粒度的,我们有时需要缓存更大的粒度,以减少数据库请求),
    • 通过 id 或 collection 工作(例如,默认情况下禁用缓存查询结果,因为在一般情况下无法使其有用)。
  • 当 Hibernate L2 不合适时,我们使用相同的 EhCache 库来缓存数据(不完全是实体)。用例示例:

    • 当一个表很大(记录长度和数量)时,内存使用不允许完全缓存它,但是只缓存所有记录的三个字段是可以的。这些字段可能是经常访问的字段,或者是不可变的......
    • 当我们对缓存进行多次读取访问时,并且每个都将触发计算(在 L2 缓存上)给定我们拥有的实体:计算结果可以存储在缓存中。 (典型示例,计算需要来自其他表的详细信息,但最终结果中未使用这些详细信息,因此缓存不存储这些详细信息)
    • 当表中的实体按类别进行逻辑分组时,我们希望一次请求和缓存一个类别,而不是一次只处理一个实体的常规 L2 缓存策略。

In a distributed context, this often
translates into invalidating a
category at a time when one of their
entities is modified, which is functionally
logical for us (and essential for
performance, as otherwise we would
have to invalidate all of these
entities ; this is because the cache
invalidates a whole region, or a
specific object, but in
between you have to loop which is bad for performance)

我确定还有其他人......

所以这种情况与数据库没有密切关系,它通常不存储我们的 Hibernate 实体。我们将它放在业务层(而不是数据访问或 Daos),使其直接可用于业务代码。请注意,对我们来说,它不是透明缓存,而是对执行操作或传递值的显式业务服务的调用(负责此缓存:如果数据不存在则加载它,根据需要使其无效)。

A fun Threading issue in this cache : because this cache is accessed by our hundred web threads, it needs to be thread-safe. You probably know why a thread-safe value is either immutable or cloned at each call (which often is a performance problem). So all our business caches use immutable objects, and performance is great.


您也可以使用[Infinispan(JBoss Cache 的演变)作为二级缓存提供程序!][1]

[1]:见 http://infinispan.blogspot.com/2009/10/infinispan-based-hibernate-cache.html


EhCache 具有分布式模式,但我不确定 Hibernate 是否支持这种模式。不过,我不明白为什么它不应该工作。

您是否因为某些特定原因而忽略了 JBossCache 3?


hibernate-redis lib 将是完美的选择。这是一个基于 Redis 的缓存。

为什么选择 Redis?它速度极快,可在云中运行,并具有 AWS Elasticache 等现成的云解决方案,因此您无需自行管理。