Memcached source code analysis -- Analysis of change of state--reference

<p align="left">This article mainly introduces the process of Memcached,libevent structure of the main thread and worker thread based on the processing of the connection state of mutual conversion (not involving data access operations),the main business logic is the drive_machine. State transition process does not involve all the state,at the same time,because of his ability,some state transition may be wrong,also please predecessors. Conversion conditions: TCP,ASCII protocol. (not including conn_swallow,binary protocols will use this state)

1 Overview

First introduces the connection,connection is connected to the conn Memcached definition of their own. All state,drive_machine () is the main conversion of the state of operation:

enum conn_states { conn_listening,/**

In the Memcached.c (main),the main thread and worker thread to complete the necessary initialization,bind the corresponding libevent events,then the main thread monitor bind monitoring ports,do a good job recycling for formal. Below is the total conversion connection state graph.

2 receive connections are distributed to the worker thread

  1. The main thread initialization,connecting into conn_listening state,waiting for the connection.
  2. When there is a connection,the main thread through the dispatch_conn_new () to pipeline writes characters C,connect the distributed to a worker thread worker thread connection,receive,enter the conn_new_cmd wait state.

3 data read

  1. Conn_new_cmd is waiting for the new connection,when the connection is written,if the current request too many,may cause starvation on other worker threads,change event type is EV_WRITE,exit.
  2. If the connection is not much,and have not read data,shows the pending command,a trip to the conn_parse_cmd,in order to continue processing the command.
  3. If the connection is not much,and is a new connection,showed no unread data,is connected into the conn_waiting state waiting for data (command).
  4. conn_waiting,Threads are waiting for subsequent data connection,if a data set to the current socket FD for reading EV_READ,ensure the following content of reading. In conn_read,call try_read_network () to read from socket FD,if no data is returned to conn_waiting,if the data is entered into the conn_parse_cmd,waiting for processing command.

The 4 command parsing

  1. After entering the conn_parse_cmd,received the order,call try_read_command () to analyze the user command,this analysis of the ASCII protocol commands,which can call process_command () to handle user command.
  2. According to the user's command,will call for different types of orders. Such as set,replace,update_command); incr (command corresponds,decr corresponds to process_arithmetic_command (gets); process_get_command (corresponding) etc.
  3. If the update type command,also need to read the updated content,enter the conn_nread state.
  4. Other types of command has to wait for the results.

The 5 command returns

  1. There will be in response to a command execution,if the command only need feedback an execution results,through the out_string () function to output the results,enter conn_write.
  2. If the get command,you need to print the contents of get,into the conn_mwrite stage.
  3. The case statement conn_write without break,so the normal print will be going to conn_mwrite.

The 6 result output

  1. Conn_mwrite,call transmit () the response results are output. Transmit () to check the current state of the writing.
  2. If the data is finished,then change the state of waiting for the new command is conn_new_cmd.
  3. If not finished,then continue to wait,until finished.

7 Summary

Above is the conversion and connection status,analysis is not in place or wrong place please. In general,feel the Memcached application of libevent writes very standard,follow up with libevent,but also can learn from Memcached to a lot of knowledge.

reference from:http://www.programering.com/a/MzM3MjMwATU.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、缓存击穿与失效时的雪崩效应。缓存穿透缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失
在192.168.80.100要联网关闭防火墙及SElinuxsystemctlstopfirewalld//关闭防火墙setenforce0//关闭监控memcached服务器:上传源码包和插件包:yuminstalllrz*-y把memcached-1.5.6.tar.gzlibevent-2.1.8-stable.tar.gz软件包拉入
Redis与Memcache对比:1.Memcache是一个分布式的内存对象缓存系统而redis是可以实现持久存储2.Memcache是一个LRU的缓存redis支持更多的数据类型3.Memcache是多线程的redis是单线程的4.二者性能几乎不相上下,实际上redis会受到硬盘持久化的影响,但是性能仍然保持在与Memcache不相上下,
安装Mencache:关闭防火墙及SElinuxsystemctl  stop  firewalldsetenforce  0memcached服务器:上传源码包和插件包:yum install  lrz*  -ymemcached-1.5.9.tar.gzlibevent-2.1.8-stable.tar.gz解压tarxfmemcached-1.5.6.tar.gz-C/optarxflibevent-2.1.8-stabl
#安装php的yaf模块,参考https://www.cnblogs.com/shifu204/p/6743578.htmlhttps://www.cnblogs.com/jiqing9006/p/9646872.htmlCentOS用phpize安装PHP扩展出现Can'tfindPHPheadersin/usr/include/php原因:https://wangzq-phper.iteye.com/blog/2297792#php的memcache的
win10下安装配置apache、php、mysql、redis、memcache资源官网下载亲测可用。名称版本下载地址网盘提取码apache2.4.38-x64下载地址33h4mysql5.5.62下载地址zicrphp7.1.26-ts-x64下载地址6ar6redis4.0.2.3-x64下载地址bnpkmemcache1.4.
使用理由:就是为了频繁查询数据库。并且数据有多!那么存到memcached内存就能够避免数据库的不断被訪问,从而提速一、安装memcachememcached在windows7上的安装问题 如今安装包:http://download.csdn.net/detail/qq1355541448/7587663或者 http://www.jb51.net/softs/448
https://www.cnblogs.com/xrq730/p/4948707.htmlMemCache是什么理解memcached的内存存储机制SlabAllocator内存分配机制Memcached默认情况下采用了名为SlabAllocator的机制分配、管理内存。0.之前内存分配的弊端在该机制出现以前,内存的分配是通过对所有记录简单地进行malloc和
 转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10923221.html 一:Memcache是什么,为什么要用它 二:Memcache指令有哪些 三:Memcache使用实例 四:Memcache深入解读
memcache讲解和在.net中初使用2017年10月17日22:51:36 等待临界 阅读数:503  前言传统数据库面临的问题数据库死锁磁盘IO正文了解memcache原理基本命令memcache与memcachedmemcache的适用范围memcache的客户端和服务端memcache与redis异同
前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、缓存击穿与失效时的雪崩效应。缓存穿透缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失
一、概念讲解1.memcache是一个高性能的分布式的内存对象缓存系统,用于动态web应用以减轻数据库负担2.memcache通过在内存里维护一个统一的巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率。3.memcache是一种内存缓存技术,是一种缓存手段,要看情况
前戏Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象减少读取数据库的次数,从而减小数据库的压力,提高动态,数据库网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程是用C编写的,但是客户端可以用任
一.配置环境1.安装jdk和tomcat并将其放至/usr/local目录下[root@server1~]#ls[root@server1~]#tarzxfjdk-7u79-linux-x64.tar.gz-C/usr/local[root@server1~]#tarzxfapache-tomcat-7.0.37.tar.gz-C/usr/local2.为了升级的时候只升级软链接,所以对jdk和tom
一、    缓存系统静态web页面:1、在静态Web程序中,客户端使用Web浏览器(IE、FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服务器我现在需要得到哪个页面,所有的请求交给Web服务器,之后WEB服务器根据用户的需要,从文件系统(存放了所有静态页面
session//php文件中ini_set("session.save_handler","memcache");ini_set("session.save_path","tcp://127.0.0.1:11211");session_start();$_SESSION['TEST']='AreyouOK?';echosession_id();不建议在php.
废话少说,直接上代码<?php//实例化memcache类$mem=newmemcache();//连接memcache$mem->connect('localhost','11211');/**判断缓存中是否有数据如果有,就读取,没有,在查询时候,就把查询的数据存入memcache中*///获取缓存数据$
一、概念1、什么是memcacheMemcached是一个自由开源的,高性能,分布式内存对象缓存系统Memcached是以LiveJournal旗下DangaInteractive公司的BradFitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要
关于Redis和Memcache在应用中,都可以实现缓存的功能,但是,具体使用情况需要根据具体业务场景,尤其是对缓存数据有特性要求时,需要选择对应的缓存机制。共同点:都是基于内存的数据库,可用作缓存机制区别:1、关于数据类型:Memcache只支持简单的Key/Value数据结构;Redis支持的数据结构相
ThedefaultcachegroupisloadedbasedontheCache::$defaultsetting.Itissettothefiledriverasstandard,howeverthiscanbechangedwithinthe/application/boostrap.phpfile//ChangethedefaultcachedrivertomemcacheCache::$default='me