指定全局EhCache容量

发布于 2024-10-07 18:02:32 字数 689 浏览 5 评论 0原文

我正在尝试将我的项目代码从 OSCache 迁移到 EhCache。

我们不仅使用 OSCache 作为二级 Hibernate 缓存提供程序,还用于存储其他不同性质的对象。他们都很高兴地共享同一个缓存实例,没有由于不重叠的缓存键而发生任何冲突。

转向 EhCache 时的一大区别是每个区域都有不同的缓存实例。这可能是好的,因为它可以提高查找速度,因为不同性质的数据分别驻留。不幸的是,这有一个配置地狱的代价。让我解释一下。

在 OSCache 世界中,我会将缓存容量配置为 10000。现在,如果特定安装需要/能够负担更多 RAM,我会轻松地将其增加到 50000,这样就可以了。现在,在 EhCache 中,我必须根据每个区域的增量部分更改设置!

此外,一个安装可能对 X 类型的对象有更高的使用率,而另一个安装可能更喜欢对 Y 类型的对象进行更高的改动。我们有几十个安装,每个安装将有数百个不同的缓存。为此,我们必须雇用一群人,除了监视缓存模式和调整设置之外什么都不做!

我期望 CacheManager 具有某种全局缓存容量设置,并且每个内部缓存都会根据条目使用情况争取更多容量。然而,我发现设置缓存容量的唯一方法是通过 CacheConfiguration ,它与 CacheManager 是多对一的。

到目前为止,我能看到的唯一选择是尝试强制 Hibernate 对所有实体使用一个全局缓存。有人知道该怎么做吗?对于我的情况,还有其他更好的解决方案吗?

I am trying to migrate my project code from OSCache to EhCache.

We have used OSCache not only as a second-level Hibernate cache provider but also to store other objects of a different nature. They all happily shared the same cache instance without any collisions due to non-overlapping cache keys.

One big difference when moving towards EhCache is that each region has its different cache instance. This is potentially good as it can improve lookup speed as data of the different nature resides separately. Unfortunately, this has a price of configuration hell. Let me explain.

In the OSCache world, I would configure my cache capacity to be, let's say, 10000. Now if a particular installation would require/could afford more RAM, I would easily beef it up to 50000 and that would do. Now in EhCache I have to go and change the setting by portion of this delta for every region!

Moreover, one installation might have higher usage of objects of type X whereas another installation might prefer higher churn of objects of type Y. We have dozens of installations and each installation would have hundreds of different caches. For this, we would have to hire bunch of people just doing nothing but monitoring cache patterns and tweaking the settings!

I was expecting CacheManager to have some sort of a global cache capacity setting and each internal cache would fight for more capacity, depending on entry usage. However the only way I found to set the cache capacity is via CacheConfiguration which is many-to-one against CacheManager.

So far the only option I can see is to try to force the Hibernate to use one global cache for all the entities. Does anybody know how to do that? Are there any other, better, solutions for my scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2024-10-14 18:02:32

您可以尝试使用一个缓存并在其周围添加装饰器。装饰器的名称可以与您的区域名称相匹配,以便 hibernate 可以使用这些缓存,但这些装饰器将在下面使用相同的缓存。因此只需管理一个缓存配置。
您可以通过实现自定义缓存装饰器并设置装饰缓存的名称来实现此目的。

你可以有这样的 ehcache.xml :
<代码>

<defaultCache maxElementsInMemory="10000" eternal="false"
    overflowToDisk="false"/>

<cache name="singleSharedCache" maxElementsInMemory="2000"
    eternal="false" overflowToDisk="false">
    <cacheDecoratorFactory class="com.xyz.util.CustomEhcacheDecoratorFactory"
        properties="name=org.hibernate.tutorial.domain.Person" />
    <cacheDecoratorFactory class="com.xyz.util.CustomEhcacheDecoratorFactory"
        properties="name=org.hibernate.tutorial.domain.Event" />
</cache>

<代码>

“com.xyz.util.CustomEhcacheDecoratorFactory”是一个自定义的 ehcache 装饰器工厂类,用于创建装饰的 ehcache。你可以使用“properties”属性以任何你想要的方式设置装饰的ehcache,这里你只使用name属性来配置新装饰的ehcache的名称。所有其他操作都可以委托给底层缓存。

在这里提供了一个适用于此用例的自定义缓存装饰器,它重用了 ehcache jar 中的 EhcacheDecoratorAdapter 并且仅重写了 getName()。 EhcacheDecoratorAdapter 将所有操作委托给您在构造函数中传递的底层 ehcache:

package com.xyz.util;

import java.util.Properties;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.constructs.CacheDecoratorFactory;
import net.sf.ehcache.constructs.EhcacheDecoratorAdapter;

public class CustomEhcacheDecoratorFactory extends CacheDecoratorFactory {

    public Ehcache createDecoratedEhcache(final Ehcache cache,
            final Properties properties) {
        return new EhcacheDecoratorAdapter(cache) {
            private final String name = properties.getProperty("name");

            public String getName() {
                return name;
            }
        };
    }

    public Ehcache createDefaultDecoratedEhcache(final Ehcache cache,
            final Properties properties) {
        return new EhcacheDecoratorAdapter(cache) {
            private final String name = properties.getProperty("name");

            public String getName() {
                return name;
            }
        };
    }
}

You can try having one single cache and adding decorators around it. The decorators can have names matching your region names so that hibernate can use those caches but those decorators would be using the same cache underneath. So theres only one cache config to manage.
You can achieve this by implementing Custom cache decorators and set up the names of your decorated caches.

You can have ehcache.xml something like this:

<defaultCache maxElementsInMemory="10000" eternal="false"
    overflowToDisk="false"/>

<cache name="singleSharedCache" maxElementsInMemory="2000"
    eternal="false" overflowToDisk="false">
    <cacheDecoratorFactory class="com.xyz.util.CustomEhcacheDecoratorFactory"
        properties="name=org.hibernate.tutorial.domain.Person" />
    <cacheDecoratorFactory class="com.xyz.util.CustomEhcacheDecoratorFactory"
        properties="name=org.hibernate.tutorial.domain.Event" />
</cache>

The "com.xyz.util.CustomEhcacheDecoratorFactory" is a custom ehcache decorator factory class which is used to create the decorated ehcaches. You can use the "properties" attribute to set up the decorated ehcache in any way you want, here you only use a name property to configure the name of the new decorated ehcache. All other operations can be delegated to the underlying cache.

Providing one custom cache decorator that would work for this use-case here, it reuses the EhcacheDecoratorAdapter that comes in the ehcache jar and just overrides getName(). EhcacheDecoratorAdapter delegates all operations to an underlying ehcache which you pass in the constructor:

package com.xyz.util;

import java.util.Properties;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.constructs.CacheDecoratorFactory;
import net.sf.ehcache.constructs.EhcacheDecoratorAdapter;

public class CustomEhcacheDecoratorFactory extends CacheDecoratorFactory {

    public Ehcache createDecoratedEhcache(final Ehcache cache,
            final Properties properties) {
        return new EhcacheDecoratorAdapter(cache) {
            private final String name = properties.getProperty("name");

            public String getName() {
                return name;
            }
        };
    }

    public Ehcache createDefaultDecoratedEhcache(final Ehcache cache,
            final Properties properties) {
        return new EhcacheDecoratorAdapter(cache) {
            private final String name = properties.getProperty("name");

            public String getName() {
                return name;
            }
        };
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文