当前位置:网站首页>Memcached full profiling – 1 Fundamentals of memcached

Memcached full profiling – 1 Fundamentals of memcached

2022-06-24 21:27:00 Hello,C++!

memcached What is it? ?

memcached In order to LiveJournal its Danga Interactive The company's Brad Fitzpatric A software developed first . Now it has become mixihatenaFacebookVox、LiveJournal And many other services Improve Web An important factor in application scalability .

many Web Applications save data to RDBMS in , The application server reads the data from it and displays it in the browser . But as the amount of data increases 、 The focus of the visit , Will appear RDBMS To increase the burden of 、 Database response deteriorated 、 Website display delay and other significant effects .

It's time to memcached Show off your skill .memcached It's a high-performance distributed memory cache server . The general purpose of use is , By caching the database query results , Reduce database access times , To improve the dynamics Web Speed of application 、 Improve scalability .

img

chart 1 In general memcached Use of

memcached Characteristics of

memcached As a high-speed distributed cache server , It has the following characteristics .

  • Simple protocol
  • be based on libevent Event handling
  • Built in memory storage
  • memcached Distributed without communication

Simple protocol

memcached The server client communication is not complicated XML Equiform , Using a simple text line based protocol . therefore , adopt telnet Can also be in memcached Save data on 、 Get data . Here is an example .

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
set foo 0 0 3     ( Save command )
bar               ( data )
STORED            ( result )
get foo           ( Get the command )
VALUE foo 0 3     ( data )
bar               ( data )

The agreement document is located at memcached In the source code , You can also refer to the following URL.

be based on libevent Event handling

libevent It's a library , It will Linux Of epoll、BSD Class operating system kqueue And so on Packaged into a unified interface . Even if the number of connections to the server increases , Can also play O(1) Performance of . memcached Use this libevent library , So it can be in Linux、BSD、Solaris And so on the operating system to play its high performance . Event handling will not be described in detail here , You can refer to Dan Kegel Of The C10K Problem.

Built in memory storage

To improve performance ,memcached The data stored in is stored in memcached Built in memory storage space . Because data only exists in memory , So restart memcached、 Restarting the operating system will cause all data to disappear . in addition , After the content capacity reaches the specified value , Based on LRU(Least Recently Used) The algorithm automatically deletes the unused cache . memcached It's a server designed for caching , So we don't think too much about the permanence of the data . Details about memory storage , The second lecture in this series will be introduced by maesaka , Please refer to at that time .

memcached Distributed without communication

memcached Even though it is “ Distributed ” Cache server , But there is no distributed function on the server side . each memcached Not communicating with each other to share information . that , How to do distributed ? It all depends on the implementation of the client . This series will also introduce memcached Distributed .

img

chart 2 memcached Distributed

Let's briefly introduce memcached How to use .

install memcached

memcached The installation of is relatively simple , Here's a little explanation .

memcached Support many platforms .

  • Linux
  • FreeBSD
  • Solaris (memcached 1.2.5 Above version )
  • Mac OS X

In addition, it can also be installed in Windows On . Use here Fedora Core 8 To illustrate .

memcached Installation

function memcached It needs to be introduced at the beginning of this article libevent library .Fedora 8 There are ready-made rpm package , adopt yum Command installation .

$ sudo yum install libevent libevent-devel

memcached The source code can be from memcached Download it on the website . The latest version of this article at the time of writing is 1.2.5. Fedora 8 Although it also includes memcached Of rpm, But the version is older . Because the source code installation is not difficult , We don't use it here rpm 了 .

memcached The installation is the same as the general application ,configure、make、make install That's it .

$ wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
$ tar zxf memcached-1.2.5.tar.gz
$ cd memcached-1.2.5
$ ./configure
$ make
$ sudo make install

By default memcached The installation to /usr/local/bin Next .

memcached Start of

Input the following commands from the terminal , start-up memcached.

$ /usr/local/bin/memcached -p 11211 -m 64m -vv
slab class   1: chunk size     88 perslab 11915
slab class   2: chunk size    112 perslab  9362
slab class   3: chunk size    144 perslab  7281
 Middle ellipsis 
slab class  38: chunk size 391224 perslab     2
slab class  39: chunk size 489032 perslab     2
<23 server listening
<24 send buffer was 110592, now 268435456
<24 server listening (udp)
<24 server listening (udp)
<24 server listening (udp)
<24 server listening (udp)

This shows the debugging information . So it starts at the front desk memcached, monitor TCP port 11211 The maximum memory usage is 64M. Debugging information is mostly about stored information , Specify in the next series .

As daemon When the background starts , just

$ /usr/local/bin/memcached -p 11211 -m 64m -d

the memcached The startup options are as follows .

Options explain
-p The use of TCP port . The default is 11211
-m Maximum memory size . The default is 64M
-vv use very vrebose mode , Debug information and errors are output to the console
-d As daemon Start in the background

The above four are common startup options , There's a lot more , adopt

$ /usr/local/bin/memcached -h

Commands can be displayed . Many options can be changed memcached All kinds of behaviors , Recommend reading .

Connect with the client

Many languages are connected memcached The client of , Among them Perl、PHP Mainly . only memcached The languages listed on the website are

  • Perl
  • PHP
  • Python
  • Ruby
  • C#
  • C/C++
  • Lua

wait .

Here we introduce how to use mixi In use Perl Library links memcached Methods .

Use Cache::Memcached

Perl Of memcached The client has

  • Cache::Memcached
  • Cache::Memcached::Fast
  • Cache::Memcached::libmemcached

Wait a few CPAN modular . Here's the introduction Cache::Memcached yes memcached The author of Brad Fitzpatric The works of , Should be memcached The most widely used module in the client .

Use Cache::Memcached Connect memcached

The following source code is through Cache::Memcached Connect the just started memcached Example .

#!/usr/bin/perl

use strict;
use warnings;
use Cache::Memcached;

my $key = "foo";
my $value = "bar";
my $expires = 3600; # 1 hour
my $memcached = Cache::Memcached->new({
    servers => ["127.0.0.1:11211"],
    compress_threshold => 10_000
});

$memcached->add($key, $value, $expires);
my $ret = $memcached->get($key);
print "$ret\n";

ad locum , by Cache::Memcached It specifies memcached Server's IP Address and an option , To generate an instance . Cache::Memcached Common options are as follows .

Options explain
servers Specify... With an array memcached Server and port
compress_threshold Values used in data compression
namespace Specifies the prefix to be added to the key

in addition ,Cache::Memcached adopt Storable The module can Perl The complex data is serialized and then saved , So hash 、 Array 、 Objects can be saved directly to memcached in .

Save the data

towards memcached There are several ways to save data

  • add
  • replace
  • set

They are all used in the same way :

my $add = $memcached->add( ' key ', ' value ', ' Time limit ' );
my $replace = $memcached->replace( ' key ', ' value ', ' Time limit ' );
my $set = $memcached->set( ' key ', ' value ', ' Time limit ' );

towards memcached When saving data, you can specify a period ( second ). When no deadline is specified ,memcached according to LRU The algorithm saves data . The differences between the three methods are as follows :

Options explain
add Save only if there is no data with the same key in the storage space
replace Save only if data with the same key exists in the storage space
set And add and replace Different , Save whenever

get data

To get data, you can use get and get_multi Method .

my $val = $memcached->get(' key ');
my $val = $memcached->get_multi(' key 1', ' key 2', ' key 3', ' key 4', ' key 5');

Use when getting multiple pieces of data at a time get_multi.get_multi Multiple key values can be obtained asynchronously at the same time , It is faster than the loop call get Dozens of times faster .

Delete data

Delete data use delete Method , But it has a unique function .

$memcached->delete(' key ', ' Blocking time ( second )');

Delete the data of the key specified by the first parameter . The second parameter specifies a time value , You can disable the use of the same key to save new data . This function can be used to prevent incomplete cache data . But be careful ,set Ignore the blocking function , Save data as usual

Add one and subtract one

Can be memcached The specific key value on the is used as a counter .

my $ret = $memcached->incr(' key ');
$memcached->add(' key ', 0) unless defined $ret;

Adding one and subtracting one are atomic operations , But when the initial value is not set , Will not be automatically assigned to 0. therefore , Error checking should be performed , Add initialization operation if necessary . and , The server side will not be right exceed 232 Check the behavior of .

summary

This is a brief introduction to memcached, And its installation method 、Perl client Cache::Memcached Usage of . As long as you know ,memcached The simple method of using is enough .

Next time, maesaka will explain memcached The internal structure of . understand memcached The inner structure of , You will know how to use memcached Can we make Web The speed of application goes to a higher level . Welcome to the next chapter .

Link to the original text :

memcached A complete analysis of –1. memcached The basis of

memcached Comprehensive analysis –2. understand memcached Memory storage

memcached Comprehensive analysis –3. memcached The deletion mechanism and development direction of

memcached Comprehensive analysis –4. memcached Distributed algorithm

memcached Comprehensive analysis –5. memcached Applications and compatible programs

原网站

版权声明
本文为[Hello,C++!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241432586068.html