【发布时间】:2015-09-22 12:04:03
【问题描述】:
我们将<time-to-live-seconds> 配置为 1200(20 分钟),但缓存会在缓存创建时间一分钟后自动清除。
有人能告诉我如何让缓存在指定的时间段内有效吗?
【问题讨论】:
标签: spring caching hazelcast spring-cache hazelcast-imap
我们将<time-to-live-seconds> 配置为 1200(20 分钟),但缓存会在缓存创建时间一分钟后自动清除。
有人能告诉我如何让缓存在指定的时间段内有效吗?
【问题讨论】:
标签: spring caching hazelcast spring-cache hazelcast-imap
hazelcast.xml 中的 sn-p 下方将“simpleMap”中条目的 time-to-live-seconds 属性设置为 150 秒。超过 150 秒且超过 150 秒未更新的条目将自动从地图中删除。
<map name="simpleMap">
<backup-count>0</backup-count>
<max-idle-seconds>0</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<time-to-live-seconds>30</time-to-live-seconds>
<max-size>3000</max-size>
<eviction-percentage>30</eviction-percentage>
<merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
</map>
我已将条目监听器(如果使用ver3.5,请使用MapListener 接口)附加到“simpleMap”以进行测试,每当条目从地图中逐出时,它都会打印键和值。
public static void main(String[] args)
{
HazelcastInstance instance = HazelcastHelper.getHazelcastInstance();
IMap<String, String> map = instance.getMap("simpleMap");
map.addEntryListener(new SimpleMapEntryListener(), true);
map.put("evictme", "value");
printTime();
try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
}
printTime();
}
private static void printTime()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
class SimpleMapEntryListener implements EntryListener<String, String>
{
@Override
public void entryEvicted(EntryEvent<String, String> arg0)
{
System.out.println("Key : " + arg0.getKey() + " Value: " + arg0.getOldValue() + " evicted.");
// print current time here.
}
// Add other overriden methods.
}
如果您执行上面给出的代码,您可以看到条目 仅在 30 秒后被自动驱逐。
您可能还需要检查您的max-idle-seconds 配置(参见上面的示例,它设置为零,表示无限)。如果设置了缓存(即不为零),缓存将根据max-idle-seconds 被驱逐。
【讨论】: