public bool Set(string Key, string Value,TimeSpan? expiry = default(TimeSpan?)) { return database.StringSet(Key, Value, expiry); }
想请教一下,这个方法的第3个参数是什么意思?如果我想设置缓存1分钟后过期,该怎么设置呢?
最近刚学Redis,很多地方都不懂,请教大神解答
最后一个参数是分钟,如果1分钟后过期,设置为1就好 Set("key","value",1)
public static bool Set(string Key, string Value, int expireMinutes = 0) { if (expireMinutes > 0) { database.StringSet(Key, Value, TimeSpan.FromMinutes(expireMinutes)); } else { database.StringSet(Key, Value); } return database.StringSet(Key, Value); }
你说的是这样写吗?
@小兵程序:
public static bool Set(string Key, string Value, int expireMinutes = 0) { if (expireMinutes > 0) { return database.StringSet(Key, Value, expireMinutes); } else { return database.StringSet(Key, Value); } }
@poplau: 这个时间是绝对过期时间吗?
redis中所有过期都是滑动过期,设置绝对过期在redis上是不可能的,这样会导致master,slave上数据不一致。
为啥我设置了5分钟的过期时间,然后等5分钟后,缓存就过期了。在这期间我从缓存中取了很多次数据,也没发现时间往后延
@小兵程序: 读不能往后延,要往后延就重写一次
@小兵程序: 可能描述不准确,我的意思是当你set的时候指定的expire实际就是从你set这刻开始后过多少时间后过期,这个不会后延的。你问题就直接设60秒就可以了。