3
votes

Is there any way to load a map entry with a custom TTL using a MapStore?

Use Case: My map entries each have a custom expiration, at which point the entry is no longer valid (the TTL is not just for limiting the size of the in-memory map, and the TTL is applied to each entry, not to the map config). I set this TTL when I initially put my entry in the map, the expiration is persisted in my underlying persistent map datastore, but I cannot reset this TTL when loading my entries from the database.

public class MyMapStore implements MapStore<MayKey, MapValue> {

    @Override
    public MapValue load(MayKey key) {
        MapValue value = datstore.lookup(key);
        Date ttl = value.getExpiration();
        // the value has it's own entry-specific TTL, but it seems this can't be used from the MapStore
        return value;
    }

    // . . .

}

The MapLoader documentation seems to indicate this may not be possible:

Loaded entries will be placed into the distributed map and they will stay in-memory until they are explicitly removed or implicitly evicted (if eviction is configured).

If there is no way to accomplish this with MapStore, some remaining options seem to be:

  • Do not use MapStore and instead explicitly lookup entries from my persistent datastore, loading these values with the desired TTL
    • This may be feasible, but with this approach, I'm no longer able to take advantage of the benefits of MapStore
  • Run a background thread which periodically explicitly evicts these expired entries, rather than relying on Hazelcast's automatic eviction
    • This is just a lot of overhead (additional thread, querying and entry processing) and requires that my application code checks the validity of the entry prior to use (since expired entries may exist in the map)

Is there any other way to accomplish this cleanly/easily with Hazelcast that I'm overlooking?

2

2 Answers

1
votes

As of version 3.6 of Hazelcast, there is not a good way to accomplish this, so I will need to use one of the alternatives identified in the original post.

Hazelcast Issue 7728 has been logged to address this.

See the Hazelcast Google Group for related discussions as well.

0
votes

Did you check : http://docs.hazelcast.org/docs/latest/manual/html-single/index.html#understanding-map-eviction

Evicting Specific Entries The eviction policies and configurations explained above apply to all the entries of a map. The entries that meet the specified eviction conditions are evicted.

But you may want to evict some specific map entries. In this case, you can use the ttl and timeunit parameters of the method map.put(). An example code line is given below.

myMap.put( "1", "John", 50, TimeUnit.SECONDS )

The map entry with the key "1" will be evicted 50 seconds after it is put into myMap.