hazelcast - 有没有办法修改已以声明方式声明的 JCache 的过期策略?
问题描述
我正在使用 Hazelcast 3.12 jCache 实现。我的缓存已声明(通过 hazelcast-config.xml),但是我希望能够动态更改 ExpiryPolicy 的持续时间,因为超时值只能以编程方式使用。
我查看了 ICache 的文档,但没有看到任何可以让我检索和/或修改整个缓存的过期策略的方法。
我的缓存声明为:
<cache name="toto">
<async-backup-count>1</async-backup-count>
<backup-count>1</backup-count>
<cache-entry-listeners>
<cache-entry-listener old-value-required="true">
<cache-entry-listener-factory class-name="cache.UserRolesEntryListenerStaticFactory"/>
</cache-entry-listener>
</cache-entry-listeners>
<key-type class-name="java.lang.String"/>
<management-enabled>true</management-enabled>
<statistics-enabled>true</statistics-enabled>
<quorum-ref>1</quorum-ref>
<partition-lost-listeners></partition-lost-listeners>
</cache>
我想在检索到到期政策时设置/更新到期政策:
@Produces
@Singleton
@UserRolesCache
public Cache<String, String[]> createUserRoleCache(@HazelcastDistributed CacheManager cacheManager) {
Cache cache = cacheManager.getCache("toto");
// get expiry timeout from a 3rd service
int timeout = configService.getCacheExpiry();
// how to set the expiry policy here???
// cache.setExpiryPolicy(.....) ?????
}
使用 jCache 或 Hazelcast API 是否可行?
解决方案
没有明确的方法可以更改 a 的默认到期策略Cache
(在 JCache 或 Hazelcast 特定的 API 中)。
您可以通过Cache
使用自定义配置来实现相同的效果Factory<ExpiryPolicy>
。在您的ExpiryPolicy
实现中,您可以咨询您的外部服务来查询当前的到期持续时间并返回,以便 JCache 实现应用它。请注意,由于在每个条目创建/访问/更新时都会查询到期策略,因此ExpiryPolicy
方法实现最好不要涉及任何远程服务查询或数据库访问,否则您将遇到高延迟。例如,最好将您的到期策略注册为外部服务的侦听器(如果支持),或者有一个单独的执行程序来安排对外部服务的查询以进行更新。
使用 JCache API 的示例实现:
class Scratch {
public static void main(String[] args)
throws InterruptedException {
MutableConfiguration<String, String[]> roleCacheConfig = new MutableConfiguration<>();
// other config here...
roleCacheConfig.setExpiryPolicyFactory(new CustomExpiryPolicyFactory());
CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager();
Cache<String, String[]> cache = manager.createCache("test", roleCacheConfig);
cache.put("a", new String[]{}); // consults getExpiryForCreation
Thread.sleep(1000);
cache.get("a"); // consults getExpiryForAccess
Thread.sleep(1000);
cache.put("a", new String[]{}); // consults getExpiryForUpdate
}
public static class CustomExpiryPolicyFactory implements Factory<ExpiryPolicy>, Serializable {
@Override
public ExpiryPolicy create() {
// initialize custom expiry policy: at this point
// the custom expiry policy should be registered as listener to
// external service publishing expiry info or otherwise configured
// to poll the external service for new expiry info
CustomExpiryPolicy expiryPolicy = new CustomExpiryPolicy(120, 30, 120);
return expiryPolicy;
}
}
public static class CustomExpiryPolicy implements ExpiryPolicy {
private volatile Duration expiryForCreation;
private volatile Duration expiryForAccess;
private volatile Duration expiryForUpdate;
public CustomExpiryPolicy(long secondsForCreation,
long secondsForAccess,
long secondsForUpdate) {
this.expiryForCreation = new Duration(TimeUnit.SECONDS, secondsForCreation);
this.expiryForAccess = new Duration(TimeUnit.SECONDS, secondsForAccess);
this.expiryForUpdate = new Duration(TimeUnit.SECONDS, secondsForUpdate);
}
// assuming this is invoked from external service whenever there is a change
public void onExpiryChanged(long secondsForCreation,
long secondsForAccess,
long secondsForUpdate) {
this.expiryForCreation = new Duration(TimeUnit.SECONDS, secondsForCreation);
this.expiryForAccess = new Duration(TimeUnit.SECONDS, secondsForAccess);
this.expiryForUpdate = new Duration(TimeUnit.SECONDS, secondsForUpdate);
}
@Override
public Duration getExpiryForCreation() {
return expiryForCreation;
}
@Override
public Duration getExpiryForAccess() {
return expiryForAccess;
}
@Override
public Duration getExpiryForUpdate() {
return expiryForUpdate;
}
}
}
您可以在声明性 Hazelcast XML 配置中提供自定义到期策略工厂类名称,如下所示:
<cache name="toto">
<async-backup-count>1</async-backup-count>
<backup-count>1</backup-count>
...
<expiry-policy-factory class-name="com.example.cache.CustomExpirePolicyFactory" />
...
</cache>
作为旁注,ICache
Hazelcast 特定的扩展Cache
接口中的一些方法允许您使用为每个键指定的自定义到期策略对一个键或一组键执行操作(但不更改缓存范围内的适用到期政策)。
推荐阅读
- ios - AppDelegate 和 SwiftUI 视图之间的通信
- javascript - 在 div 中包含可拖动元素而不会溢出
- docker - 安装 Docker 时包 moby-runc-*+azure-2.x86_64.rpm 未签名
- python-3.x - 如何使用预训练的 .caffemodel 在 python 上对单个图像执行预测
- kubernetes - 从集群内部调用时的 503 响应代码
- django - Django 管理界面不执行服务器端检查以确定 csrf 令牌的有效性
- css - 未显示的按钮轮廓
- comments - 在 pycharm 中评论
- netsuite - 在 SuiteScript 2.0 中使用外部“表”库
- module - 如何在 Prestashop 的类别页面上的产品名称旁边创建链接/按钮“更多信息”?(这应该重定向到产品页面)