memcached LRU 算法淘汰数据

 

/*************************************************************************
* memcache LRU 算法,摘录自《PHP核心技术与最佳实践》 362页
* File Name : memcacheLru.c
* Author : unasm
* Mail : unasm@sina.cn
* Last_Modified: 2014-12-30 10:08:48
************************************************************************/

//申请内存
it = slabs_alloc(ntotal, id);

//返回值为0,表示没有申请到
if(it == 0){
int tries = 50;
...
//清除没有被引用的数据
for(search = tail[id]; tries > 0 && search != NULL; tries-- , search = search->prev){
if(search->refcount == 0){
...
do_item_link(search);
break;
}
}
//再次申请内存,
it = slabs_alloc(ntotal, id);
if(it == 0){
tries = 50;
// 查找三个小时以内都没有被访问的数据,淘汰掉
for(search = tail[id]; tries > 0 && search != NULL; tries--, search = search->prev){
if (search->refcount != 0 && search->time + 10800 < current_time) {
search->refcount = 0;
do_item_link(search);
break;
}
}
//再次申请分配内存
it = slabs_alloc(ntotal, id);
if(it == 0){
return NULL;
}
}
}

Leave a comment

Your email address will not be published.

*