[Middleware from Bronze to King] The first article: Memcache first met

Hello everyone, I am the old Xin from the Internet, this is the first day I participated in the more essay challenge;

Today I will mainly share the introduction and deployment of memcache, as well as simple use, one article per day, let you learn middleware systematically

Introduction to memcache

memcahce is called a distributed high-speed cache system, which can effectively improve the access speed on some large-scale database websites that need to be accessed frequently.

There are currently two types of application scenarios:

As the front-end cache of the database:
Memcached (memory is a volatile storage), redis, memcachedb, HANA

As a session server

work process

image.png

In the case of cache, the request arrives at memcache through the web server, check, if there is cache, the result will be returned directly, and the database will not be accessed.

image.png

Interview question: the difference between memcache and redis

In terms of storage method: memcache stores all the data in the memory, and it will hang up after a power failure, and the data cannot exceed the memory size.
The advantage of redis is that part of the data is stored on the hard disk, which can ensure the persistence of the data and support the persistence of the data.

From the perspective of data support types, redis has more data support than memcache.

But the biggest advantage of memcache is that it supports more platforms, and redis can only be used on linux at present.

Install memcache

Install libevent

Upload the software package: libevent-2.1.12-stable.tar.gz

Unzip

[[email protected] ~]# tar zxvf libevent-2.1.12-stable.tar.gz
[[email protected] ~]# cd libevent-2.1.12-stable
[[email protected] libevent-2.1.12-stable]# ./configure  --prefix=/usr/local/libevent --disable-openssl #检查系统编译环境

installation

[[email protected] libevent-2.1.12-stable]# make  -j  4 

[[email protected] libevent-2.1.12-stable]# make install  #安装

View the installed files:

[[email protected] libevent-2.1.12-stable]# ls /usr/local/libevent/
bin include lib

Install memcache

[[email protected] ~]# tar zxvf memcached-1.6.9.tar.gz 
[[email protected] ~]# cd memcached-1.6.9
[[email protected] memcached-1.6.9]# ./configure  --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[[email protected] memcached-1.6.9]# make -j 4
[[email protected] memcached-1.6.9]# make install
[[email protected] memcached-1.6.9]# echo $?
0

Start memcached

[[email protected] memcached-1.6.9]# /usr/local/memcached/bin/memcached -u root -p 11211 -l 192.168.1.164 -P /var/run/memcached.pid -m 128m -c 2048 -d
[[email protected] memcached-1.6.9]# echo $?
0

Introduction of parameters

parametereffect
-uuser
-pport
-llisten
-Ppid
-mMemory cache size
-cMaximum concurrency
-dRun in the background as a daemon

View port:

[[email protected] ~]# netstat -antup | grep 11211
tcp        0      0 192.168.1.164:11211          0.0.0.0:*                   LISTEN      9801/memcached      
udp        0      0 192.168.1.164:11211          0.0.0.0:*                               9801/memcached   

Test, connect to memecached to read and write data

#使用telnet连接memcache,进行测试
语法:telnet IP 11211
[[email protected] ~]# rpm -ivh /mnt/Packages/telnet-0.17-47.el6.x86_64.rpm 
warning: /mnt/Packages/telnet-0.17-47.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:telnet                 ########################################### [100%]

-m allocate memory
-p monitor port
-d daemon
-c number of connections
-u run user
-l monitor ip

Actual combat: use telnet to connect to memcache for testing

[[email protected] ~]# telnet 192.168.1.164 11211
Trying 192.168.1.164...
Connected to 192.168.1.164.
Escape character is '^]'.
set name 4 300 6
mkinge
STORED
get name
VALUE name 4 6
mkinge
END
quit
Connection closed by foreign host.
parameterDescription
mkingeInput content
get nameget command to read cached data
ENDThis symbol indicates that the deposit is successful
quitdrop out
STORED# This symbol means that the deposit is successful

set name 4 300 6
#Store a key value in the memcached cache, mark 4, the cache time is 300 seconds, and the string length is 6 bytes

to sum up

Preview: Tomorrow we will share the use of memcache to accelerate MySQL