who*_*ami 3 spring caching hazelcast spring-cache hazelcast-imap
我们配置<time-to-live-seconds>为1200(20分钟)但缓存在缓存创建时间一分钟后自动逐出.
有人可以告诉如何在指定的时间段内使缓存生效吗?
在片段下方hazelcast.xml将time-to-live-seconds"simpleMap"中条目的属性设置为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>
Run Code Online (Sandbox Code Playgroud)
我已经将一个条目监听器(如果使用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.
}
Run Code Online (Sandbox Code Playgroud)
如果执行上面给出的代码,您可以看到条目<"evictme","value">仅在30秒后自动逐出.
您可能还需要检查max-idle-seconds配置(参见上面的示例,它设置为零,这意味着无限).max-idle-seconds如果设置(即不为零),则缓存将根据其被逐出.
| 归档时间: |
|
| 查看次数: |
4075 次 |
| 最近记录: |