将“缓存Redis过期"设置为1年 [英] Set Cache Redis Expiration to 1 year
问题描述
如何将Redis缓存的有效期设置为1年?
How to set Redis Cache expiration to 1 year?
我尝试将xml配置上的 long
值设置为: 31556952000
(1年),但随后捕获到一个异常,该异常是Integer类型无法识别值作为整数.我尝试在Google上进行搜索,结果显示Integer最大值不超过 2147483647
,这意味着,如果设置为该最大值,则我的缓存只会在24天后过期.
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.
这是我的 applicationContext.xml
(我省略了不必要的代码):
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>
...
上面的代码配置为将 ruleCache
的到期时间设置为仅1天(86400以毫秒为单位).
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.
推荐答案
Redis接受expire命令的整数值(最大为2147483647).单位是秒,而不是毫秒,因此1年是31556952,而不是31556952000,并且它适合整数.
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.
如果您希望地图访问Long,也许您可以修改配置:
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">
这篇关于将“缓存Redis过期"设置为1年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!