Table of Contents
初期阅读
cache_create
cache_destroy
cache_alloc 与 cache_free
最后一个
原理
可以做什么
客户端
服务器
迟迟到来的原理
Home Database Mysql Tutorial memcached 源码阅读之原理篇

memcached 源码阅读之原理篇

Jun 07, 2016 pm 04:41 PM
memcached principle Source code read

初期阅读 初期我是一行一行的看,遇到很多库函数不认识,于是研究了一番。 后来发现方向跑错了,那些库函数是为了实现网络编程,与 memcached 的本质没有关系。 于是开始查看函数名了。 cache_create 初始化 memcached . 主要工作是生成一个 cache_t 对象。

初期阅读

初期我是一行一行的看,遇到很多库函数不认识,于是研究了一番。
后来发现方向跑错了,那些库函数是为了实现网络编程,与 memcached 的本质没有关系。
于是开始查看函数名了。

cache_create

初始化 memcached .

主要工作是生成一个 cache_t 对象。

cache_t* cache_create(const char *name, size_t bufsize, size_t align,
                      cache_constructor_t* constructor,
                      cache_destructor_t* destructor) {
    cache_t* ret = calloc(1, sizeof(cache_t));
    char* nm = strdup(name);
    void** ptr = calloc(initial_pool_size, sizeof(void*));
    ret->name = nm;
    ret->ptr = ptr;
    ret->freetotal = initial_pool_size;
    ret->constructor = constructor;
    ret->destructor = destructor;
    ret->bufsize = bufsize;
    return ret;
}
Copy after login

cache_destroy

回收 memcached.

void cache_destroy(cache_t *cache) {
    while (cache->freecurr > 0) {
        void *ptr = cache->ptr[--cache->freecurr];
        if (cache->destructor) {
            cache->destructor(get_object(ptr), NULL);
        }
        free(ptr);
    }
    free(cache->name);
    free(cache->ptr);
    pthread_mutex_destroy(&cache->mutex);
    free(cache);
}
Copy after login

cache_alloc 与 cache_free

申请一个节点和释放一个节点

最后一个

这样又看了结果函数,发现还是没有涉及到 memcached 的本质,这些在创建 服务器,还是网络编程的知识。
虽然我不会网络编程。

原理

可以做什么

memcached 是以 key-val 的方式储存的 map
而且key是字符串, val 是一个对象。

客户端

客户端连接上 memcached 的服务器后,可以set设置内容,get查询内容了。
内容是通过 socket 的方式传给服务器的。
数据分两部分,一部分是key,一部分是val的序列化。

服务器

对于服务器来说,接收到的数据其实就是两个字符串的映射 map 所以这时就简单了。
直接实现一个 hash table 就行了。
刚好前几天我实现了一个精简版的 hash table, 刚好可以用上。
只不过需要加一个字符串val字段,而key字段永远是字符串。
又由于 key 是字符串,所以字符串的 hash 函数就不需要用户提供了。
对于 val 同样是这样,字符串判断相等直接 cmp 即可。

这样 memcached 就很容易实现了。

迟迟到来的原理

服务器端分两部分功能。
第一步分是 hash table 用于存储 string 到 string 的映射。
第二部分是 服务器,用于接收客户端的请求:查询,增加,删除,更新等操作。

客户端实现只有一部了:向服务器发送查询,增加,删除,更新等操作。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

How to use Microsoft Reader Coach with Immersive Reader How to use Microsoft Reader Coach with Immersive Reader Mar 09, 2024 am 09:34 AM

In this article, we will show you how to use Microsoft Reading Coach in Immersive Reader on Windows PC. Reading guidance features help students or individuals practice reading and develop their literacy skills. You start by reading a passage or a document in a supported application, and based on this, your reading report is generated by the Reading Coach tool. The reading report shows your reading accuracy, the time it took you to read, the number of words correct per minute, and the words you found most challenging while reading. You will also be able to practice the words, which will help develop your reading skills in general. Currently, only Office or Microsoft365 (including OneNote for Web and Word for We

In-depth discussion of the principles and practices of the Struts framework In-depth discussion of the principles and practices of the Struts framework Feb 18, 2024 pm 06:10 PM

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

An in-depth analysis of the functions and working principles of the Linux chage command An in-depth analysis of the functions and working principles of the Linux chage command Feb 24, 2024 pm 03:48 PM

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

How to display the source code of PHP code in the browser without being interpreted and executed? How to display the source code of PHP code in the browser without being interpreted and executed? Mar 11, 2024 am 10:54 AM

How to display the source code of PHP code in the browser without being interpreted and executed? PHP is a server-side scripting language commonly used to develop dynamic web pages. When a PHP file is requested on the server, the server interprets and executes the PHP code in it and sends the final HTML content to the browser for display. However, sometimes we want to display the source code of the PHP file directly in the browser instead of being executed. This article will introduce how to display the source code of PHP code in the browser without being interpreted and executed. In PHP, you can use

An in-depth discussion of the functions and principles of Linux RPM tools An in-depth discussion of the functions and principles of Linux RPM tools Feb 23, 2024 pm 03:00 PM

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

See all articles