我们都知道,业内的所有nosql数据库在存储的时候基本都可以对key设置一个ttl——(time to live),也就是失效时间。这个功能非常有用,特别是在缓存数据的更新,以及一些定时优惠等需求场景来说,更是如此。
今天在couchbase上踩到了一个有意思的坑,对某个key设置了一个一年的ttl,然后立刻获取该key ,居然莫名奇妙的消失了。
public static void testTTL() throws Exception {
int ttl = 90 * 24 * 60 * 60;
//ttl = (int)(System.currentTimeMillis()/1000)+ttl;
ICouchbaseBaseDao couchbaseLongDao = new CouchbaseRecallDao();
couchbaseLongDao.set("test_ttl", ttl, "sdfdsfdsf".getBytes());
System.out.println("===============");
byte[] bs = couchbaseLongDao.get("test_ttl");
if (bs !=null) {
System.out.println(String.valueOf(bs));
} else {
System.out.println("null...");
}
}
输出为null...
仔细排查,发现如果把ttl设置为一个月30*24*60*60,就没有问题。最后在couchbase的文档上说明:
Here is how to specify a TTL:
- To set a value of 30 days or less : If you want an item to live for less than 30 days, you can provide a TTL in seconds or as Unix time. The maximum value you can specify in seconds is the number of seconds in a month, namely 30 x 24 x 60 x 60. Couchbase Server removes the item the given number of seconds after it stores the item.
- To set a value over 30 days : If you want an item to live for more than 30 days, you must provide a TTL in Unix time.
原来,couchbase支持两种ttl时间,对于小于30天的ttl,可以直接写成秒;对于大于30天的,需要写成时间戳的形式。于是,改造代码成下面这样,就可以了
public static void testTTL() throws Exception {
int ttl = 90 * 24 * 60 * 60;
ttl = (int)(System.currentTimeMillis()/1000)+ttl;
ICouchbaseBaseDao couchbaseLongDao = new CouchbaseRecallDao();
couchbaseLongDao.set("test_ttl", ttl, "sdfdsfdsf".getBytes());
System.out.println("===============");
byte[] bs = couchbaseLongDao.get("test_ttl");
if (bs !=null) {
System.out.println(String.valueOf(bs));
} else {
System.out.println("null...");
}
}