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.
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.
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.
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.
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.
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With