Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Cache Redis Expiration to 1 year

How to set Redis Cache expiration to 1 year?

I tried to set the long value on the xml configuration to : 31556952000 (1 year), but then it caught an exception that type Integer doesn't recognize the value as Integer. I tried to search at Google, and it says that Integer maximum value is up to 2147483647, which means, if I set to that maximum value, I only get my cache expires on 24 days.

Here is my applicationContext.xml (I omitted unnecessary code) :

    ...
    <cache:annotation-driven />

    <bean id="redisCacheMap" class="java.util.HashMap">
        <constructor-arg index="0" type="java.util.Map">
            <map key-type="java.lang.String" value-type="java.lang.Integer">
                <entry key="ruleCache" value="86400"/>
            </map>
        </constructor-arg>
    </bean>
    ...

The code above is configured to set the expiration of ruleCache to only 1 day (86400 in ms).

Is it possible to do that? Thanks.

like image 342
mrjimoy_05 Avatar asked Jan 22 '15 09:01

mrjimoy_05


People also ask

How do I change the expiration date on Redis cache?

First, create a key in Redis and set some value in it. Now, set timeout of the previously created key. In the above example, 1 minute (or 60 seconds) time is set for the key tutorialspoint. After 1 minute, the key will expire automatically.

How do I set the expiry for cache?

To set output-cache expirations for a page programmatically If you set expirations for a page programmatically, you must set the Cache-Control header for the cached page as well. To do so, call the SetCacheability method and pass it the HttpCacheability enumeration value Public.

Can we set TTL in Redis?

Redis TTL command is used to get the remaining time of key expiry in seconds. Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.

Does Redis cache expire?

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.


1 Answers

Redis accepts integer value (maximum is up to 2 147 483 647) for expire command. The unit is second, not ms, so 1 year is 31556952 instead of 31556952000, and it fits into integer.

If you want your map to access Long, maybe you can adapt your config:

<map key-type="java.lang.String" value-type="java.lang.Long">
like image 81
Renly Avatar answered Oct 05 '22 13:10

Renly