PlayFramework 2.2.6. Default cache expiration

1.2k views Asked by At

I would like to store Java object in cache for user session. I would like to know what is default cache storage timeout.

I user log in on 8.00 am I will store his user object in cache. Will it keep f.e. 10 hours until user will logout? Will be data stored in default play 2.2.6 cache implementation for 10 hours?

I would like to not keep to much user data in cookie, cause it's content is visible for user from web browser.

Here is the documentation that I based on:

https://www.playframework.com/documentation/2.2.x/JavaCache

But there is no word about it.

Here are my cache related methods:

    public static Object getCacheValue(String key) {
        return Cache.get(getSessionValue("id") + "_" + key);
    }
    public static void setCacheValue(String key, Object value) {
        Cache.set(getSessionValue("id") + "_" + key, value);
    }
    public static void removeCacheValue(String key) {
        Cache.remove(getSessionValue("id") + "_" + key);
    }

Here are my session related methods:

public static String getSessionValue(String key) {
     return session().get(key);
}
public static void setSessionValue(String key, String value) {
     session(key, value);
}
1

There are 1 answers

2
Anton Sarov On BEST ANSWER

You have the option to specifiy an expiration for the cache - in seconds (https://www.playframework.com/documentation/2.2.x/JavaCache). If you don't do this, then the default will be 0, which means that the data is going to stay in the cache forever (for indefinite time):

https://github.com/playframework/playframework/blob/2.2.x/framework/src/play-cache/src/main/scala/play/api/cache/Cache.scala#L50-L59

Of course as this is only a cache, so you cannot really rely on the "forever". Always keep in mind that cache is cache and your data may disappear - you have to have a backup strategy to retrieve it.