couchbase 设置ttl失效的bug

我们都知道,业内的所有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...");
		}
	}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值