6

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.

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95

1 Answers1

11

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">
Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
Renly
  • 126
  • 1
  • 2
  • Hey great, thanks! I just realized that it's in seconds, not ms. – mrjimoy_05 Jan 23 '15 at 01:11
  • 1
    The year has 365 days on a normal year (not leap) which makes it `31,536,000` (365 X 24 X 60 X 60). So if you want to average a year actually is 365.25 which gives `31,557,600`. Even using a time library [moment.js](https://momentjs.com/docs/#/displaying/difference/) I got `31,622,400`. Where/How did you get the `31,556,952`? – Azteca Jul 01 '19 at 18:46
  • @Azteca - An average Gregorian year has 365.2425, ref: https://www.conversion-metric.org/time/years-to-seconds – Jorge Sampayo Aug 09 '21 at 13:56
  • 2
    The max is `9223372036854775807` not `2147483647` for `expire`, try it. – Parth Mehrotra Jan 21 '22 at 21:16