This is the first day of my participation in Gwen Challenge

Today, I will share the introduction and implementation of memcache.

The introduction of memcache

Memcahce is known as a distributed cache system, which can effectively improve access speed on some large database websites that need to be accessed frequently.

There are two application scenarios:

  1. As a front-segment cache for a database:

Memcached(memory is volatile storage), redis, memCacheDB, HANA

  1. As session server

The working process

In the case of a cache, the request goes through the Web server to memcache and is viewed. If there is a cache, the result is returned directly without accessing the database.

Interview question: Differences between Memcache and Redis

In terms of storage mode, memcache stores all data in memory. After power failure, memcache will hang up. Data cannot exceed the size of memory. The advantage of Redis is that part of the data is stored on the hard disk, which ensures data persistence and supports data persistence.

Redis has more data support than Memcache in terms of data support types.

But memcache’s biggest advantage is that it supports a wide variety of platforms. Redis is currently only available on Linux.

Installed memcache

Install libevent

Upload the software package: libevent-2.1.12-stables. Tar. gz

Unpack the

[root@itlaoxin164 ~]# tar ZXVF libevent-2.1.12-stables. Tar. gz [root@itlaoxin164 ~]# CD libevent-2.1.12-stable [root@itlaoxin164 libevent-2.1.12-stable]#./configure --prefix=/usr/local/libevent --disable-opensslCopy the code

The installation

[root@itlaoxin164 libevent-2.112.-stable]# make  -j  4 

[root@itlaoxin164 libevent-2.112.-stable]# make installCopy the code

View installed files:

[root@itlaoxin164 libevent-2.1.12-stable]# ls /usr/local/libevent/bin include lib

Installed memcache

[root@itlaoxin164 ~]# tar zxvf memcached-1.69..tar.gz 
[root@itlaoxin164 ~]# cd memcached-1.69.
[root@itlaoxin164 memcached-1.69.]# ./configure  --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@itlaoxin164 memcached-1.69.]# make -j 4
[root@itlaoxin164 memcached-1.69.]# make install
[root@itlaoxin164 memcached-1.69.]# echo $?
0
Copy the code

Start memcached

[root@itlaoxin164 memcached-1.69.]# /usr/local/memcached/bin/memcached -u root -p 11211 -l 192.1681.164. -P /var/run/memcached.pid -m 128m -c 2048 -d
[root@itlaoxin164 memcached-1.69.]# echo $?
0

Copy the code

Introduction of parameters

parameter role
-u user
-p port
-l listen
-P pid
-m Memory cache size
-c Maximum concurrent
-d Runs in the background as a daemon

View ports:

[root@itlaoxin164 ~]# netstat -antup | grep 11211
tcp        0      0 192.1681.164.:11211          0.0. 0. 0:*                   LISTEN      9801/memcached      
udp        0      0 192.1681.164.:11211          0.0. 0. 0: *9801/memcached   
Copy the code

Test, connect memecached to read data

Use Telnet to connect memcache11211
[root@itlaoxin164 ~]# 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%]

Copy the code

-m Allocating memory -p Listening port -d daemon process -c Number of connections -u Running user -l Listening IP address

Actual combat: Use Telnet to connect memcache and test

[root@itlaoxin164 ~]# telnet 192.1681.164. 11211
Trying 192.1681.164.. Connected to192.1681.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.
Copy the code
parameter instructions
mkinge The input
get name The get command reads cached data
END This mark represents successful deposit
quit exit
STORED # This symbol represents a successful deposit

Set name 4 300 6 # Store a key value in the memcached cache, flag 4. Cache time 300 seconds. String length is 6 bytes

conclusion

Preview: Tomorrow we’ll share memcache for MySQL acceleration