博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
memcached与PostgreSQL缓存命中机制
阅读量:7234 次
发布时间:2019-06-29

本文共 1575 字,大约阅读时间需要 5 分钟。

  hot3.png

memcached缓存命中机制与PostgreSQL非常相似,都是对数据缓存进行分区,根据哈希码在对应分区内匹配键值。不同的是PG分区使用常数(早先为16、9.5开始改为128,可以支持更大缓存),分区数量较少;而memcached根据线程数确定初始分区数,数量相对较大,并且有后台维护程序动态管理。

分区数量确定,在线程初始化部分,thread.c中函数thread_init:

/* Want a wide lock table, but don't waste memory */    if (nthreads < 3) {        power = 10;    } else if (nthreads < 4) {        power = 11;    } else if (nthreads < 5) {        power = 12;    } else {        /* 8192 buckets, and central locks don't scale much past 5 threads */        power = 13;    }    item_lock_count = hashsize(power);    item_lock_hashpower = power;

默认线程数为4,hashpower为12。

thread.c中的store_item函数,以键值计算哈希码:

hv = hash(ITEM_key(item), item->nkey);

items.c中的item_get函数调用  assoc中的assoc_find函数, 根据哈希码确定键值取得分区链表后,从中查找匹配的键值:
while (it) {        if ((nkey == it->nkey) && (memcmp(key, ITEM_key(it), nkey) == 0)) {            ret = it;            break;        }        it = it->h_next;        ++depth;    }

分区扩展机制,:

/* We are done expanding.. just wait for next invocation */            mutex_lock(&cache_lock);            started_expanding = false;            pthread_cond_wait(&maintenance_cond, &cache_lock);            /* Before doing anything, tell threads to use a global lock */            mutex_unlock(&cache_lock);            slabs_rebalancer_pause();            switch_item_lock_type(ITEM_LOCK_GLOBAL);            mutex_lock(&cache_lock);            assoc_expand();            mutex_unlock(&cache_lock);

这是后台维护线程的部分代码,当条目数达到临界值(hashsize(hashpower) * 3) / 2)时触发maintenance_cond,条件触发在assoc.c中的assoc_start_expand函数中。

转载于:https://my.oschina.net/quanzl/blog/345815

你可能感兴趣的文章
第154天:canvas基础(一)
查看>>
第三届高校SDN大赛决赛鸣锣!新华三鼎力支持SDN技术普及
查看>>
路测之外,沃尔沃还拉上了普通家庭一起参与自动驾驶项目
查看>>
2017品途NBI影响力评选四大榜单震撼揭晓
查看>>
飘刃 v0.0.10 首次发布,超快执行速度的 Vue 项目构建工具
查看>>
JS监听DOM结构变化
查看>>
浅谈URLEncoder编码算法
查看>>
Spring中Bean的后置处理器
查看>>
2017 CCS云计算渠道伙伴合作高峰论坛即将盛大开幕
查看>>
PHP调用MYSQL存储过程实例
查看>>
Spring/Spring MVC/Spring Boot实现跨域
查看>>
国内土豪真给力,特斯拉一季度中国销量同比暴涨250%
查看>>
论文解读:华盛顿大学教授Pedro Domingos技术论文:机器学习中一些有用的知识(二)...
查看>>
对Swoole、Workerman和php自带的socket的理解
查看>>
VR视频创作,想说爱你不容易
查看>>
ROS-CLI命令行源码及使用方法整理
查看>>
jdbc的数据库连接信息在属性文件中的配置(db.properties)
查看>>
VC++中的char,wchar_t,TCHAR
查看>>
java中如何将JScrollPane的垂直滚动条自动移动到最下端
查看>>
远程桌面端口修改
查看>>