如果你清楚知道这一点,那就跳过吧!如果你不太清楚原因,那就往下看吧
我们都在用MemCached,缓存有其过期时间,我们经常在配置中设置,如果有业务场景需要动态设置过期时间的时候,就可以通过接口直接设置过期时间
client.set(key, value, new Date(expireTime));
但这个时间如何设置,还是有一点区别,比如设置10分钟后过期,是应该设置date为System.currentTimeInMillis()+10*60*1000
还是10*60*1000
刚经过测试,两种竟然都是可以的,这是咋回事,那就看源码呗,网络上介绍的一些文档有时候需要鉴别,更可靠的还是自己下源码看,别被误导了.
/** Stores data on the server; the key, value, and an expiration time are specified.
* The server will automatically delete the value when the expiration time has been reached.
* If the value is not a String or numeric type, or if {@link #set_serial(boolean) set_serial(true)}
* has been called; the data will be Serialized prior to storage.
*
* If compression is enabled, and the data is longer than the compression threshold,
* and compresses by at least the compression savings, the data will be stored in
* compressed form.
*
* @param key key to store data under
* @param value value to store
* @param expiry when to expire the record
* @return true, if the data was successfully stored
*/
public boolean set(String key, Object value, Date expiry) {
return set("set",key,value,expiry,null);
}
这里过期时间只是简单的说when,没明确说明,在具体实现里面
try {
String cmd = cmdname + " " + key + " " + flags + " " +
expiry.getTime() / 1000 + " " + val.length + "\r\n";
sock.out().writeBytes(cmd);
sock.out().write(val);
sock.out().writeBytes("\r\n");
sock.out().flush();
String tmp = sock.in().readLine();
if (tmp.equals("STORED")) {
if (debug) {
System.out.println("MemCache: " + cmdname + " " + key + " = " + val);
}
return true;
} else {
System.out.println("MemCache:" + cmd + tmp);
}
实现中也是expiry.getTime() / 1000 传入到服务端
在客户端看不出到底怎么回事了,那就看服务端了,(网络上介绍的东西是copy来copy去的,有些都掉东西了,还是自己下代码来看)
#define REALTIME_MAXDELTA 60*60*24*30
/*
* given time value that's either unix time or delta from current unix time, return
* unix time. Use the fact that delta can't exceed one month (and real time value can't
* be that low).
*/
static rel_time_t realtime(const time_t exptime) {
/* no. of seconds in 30 days - largest possible delta exptime */
if (exptime == 0) return 0; /* 0 means never expire */
if (exptime > REALTIME_MAXDELTA) {
/* if item expiration is at/before the server started, give it an
expiration time of 1 second after the server started.
(because 0 means don't expire). without this, we'd
underflow and wrap around to some large value way in the
future, effectively making items expiring in the past
really expiring never */
if (exptime <= process_started)
return (rel_time_t)1;
return (rel_time_t)(exptime - process_started);
} else {
return (rel_time_t)(exptime + current_time);
}
}
看了这段c的代码,就明白服务端是两种方式都兼容的,一个是多少秒后过期,一个是什么时候过期,
但后者因为设置时间是在客户端,存储在服务端,假如两台服务器时间差别很大,就会导致数据的过期时间和我要求的时间点不符合。
个人觉得使用前者比较好!
分享到:
相关推荐
Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力
memcache1.2.8源码分析 压缩包里有带注释的1.2.8的源码 有分析的ppt 有整理的网络上对memcache分析比较好的word文档
memcache 监控工具,可以实现实时对内存中的memcache进行监控 获取值等等
Java开发中的Memcache原理及实现
memcache图形监控工具phpmemcache,尽是一个PHP文件就可以实现对memcache的监控。 使用方法:本地测试监控机安装Apache或者下载XAMPP(Apache+MySQL+PHP+PERL),安装后把memcachephp.zip中的memcache.php文件放到...
windows下memcache安装包 附带php扩展包
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问。
Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从...
最新windows的memcache模块下载 这个模块是平和php5.3的,在我的windowsxp php5.3.5上安装成功 里面有两个php库,一个php_memcache.dll.vc6 和一个php_memcache.dll.vc9 另外一个windows的memcache.exe文件,都是网上...
将信息保持memcache中
C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档
4、过期策略--memcache在set时就指定,例如set key1 0 0 8,即永不过期。Redis可以通过例如expire 设定,例如expire name 10; 5、分布式--设定memcache集群,利用magent做一主多从;redis可以做一主多从。都可以一主...
Memcache就不用多介绍了,做开发的人都知道。 但要用得好,却并不是那么容易的事。 如果用得不好,反而得不偿失。 这篇文档短小精悍,囊括了使用过程中需要要注意的方方面面。值得一读。
MemCache.0.2.0.zip Memcached Client for Delphi 客户端调用类 MemCache.0.2.0.zip Show all LinksExternal links Memcached Project This project is a delphi unit which implements a thread safe client for ...
Memcache的内存分配机制分析及优化策略的设计,张宏,于琳琳,本文介绍了Memcache软件在Linux下的内存分配机制-Slab内存分配器,并对该机制进行了分析,通过对比不同参数下该分配机制的性能特点, �
简单的memcache命令
windows memcache 安装服务,php_memcache.dll所有版本扩展dll 安装说明 在命令行下安装Memcache,输入 ‘c:/memcached/memcached.exe -d install’。 3.启动Memcache,再输入: ‘c:/memcached/memcached.exe -d ...
php的memcache扩展,linux下的,php的memcache扩展分为两种,一种是memcache,一种是基于libmemcached的memcached,这个是memcache版本的beta版本
Erlang_Memcache