1.1 Database Comparison
Cache: Stores data in memory. Caching is enabled only when the disk is inadequate
Disadvantages: Data loss due to power outages (dual power). The purpose of using cache to store data is only to cope with large concurrent services.
Database: mysql(relational database, can ensure data consistency, ensure that data is not lost, when too many functions, resulting in poor performance) === data reference
Cache database: memcache Redis (non-relational database, very high performance, but no guarantee of data integrity) === data provider for business
Memcachedb writes memory data to disk
Redis mainly works in memory, but regularly backs up memory data to hard disk
1.1.1 Database Selection
Data storage, data warehouse select mysql disk database
Applications with high concurrency and large services choose memcache as an in-memory database
1.1.2 Database Classification
Relational database mysql
Database (NOSQL) memcached redis MongoDB
1.2 memcached is introduced
Memcached is an open source, high-performance, pure memory caching service. Mem for memory, cache for cache and D for daemon.
Memcache is the name of the project and is also a piece of software with a C/S**** architecture.
Memcached website: memcached.org/
1.2.1 memcache advantages
① For users, users access the website faster, better experience.
② For websites, the database pressure is reduced. The database is requested only when there is no data in memory. Data written to the database for the first time is also requested. Companies typically do not warm up and will only put the database into Memcached if the user has read it.
② Improve the concurrent access of the website and reduce the number of servers.
1.3 Application Scenarios of Memcached in Enterprises
1.3.1 Front-end cache application for database
When the database (mysql) cannot handle large concurrent requests, it can cache the data in memory (cache the database) and then it can be resolved
The biggest purpose of front-end caching as a database is to reduce the pressure on the database to be accessed in large numbers
1.3.2 Session as the back-end of the cluster Session persistence
The session is stored on the server side of files, databases, memcache, or memory, etc.
Cookies are stored in the client browser.
A session is a file stored on the server, similar to a hash table. It contains the information we need, and we can pull it out when we need it.
Session depends on cookie. After the request client arrives at the server, the server will randomly generate a string as the user’s identity. The string is returned to the client through cookie, and the browser of the client will store the string in the session ID with the key. Random string keys can have no values in them. If the user submits the request again, the user name password and other user information in the request information is stored in the value of the random string, the request reaches the server, the user name password is correct, the random string will be authorized, and a mark is given to the value of the random string in the sessionID to prove that the user has logged in. The client visits the server again with the random string, and the server knows that the user is logged in and returns the requested information without authentication.
sessionAnd cookiesThe difference between
1. Cookie data is stored on the user’s browser, and session data is stored on the server
2. Cookies can be extracted and analyzed in the local browser, resulting in poor security. For security, information such as login accounts can be cached in the session.
3. Session will be stored on the server for a certain period of time, and the increase of traffic will bring pressure to the server. You can use cache tools, such as memcache
1.3.3 How to determine user information for website development
The initial technique: the server writes a cookie to your browser that contains your username and login information. Because cookies are stored in a local browser, third-party tools can easily steal cookies.
At the beginning:
Cookies Cookies name: Content (user name, login information)
After the improvement:
Local browser:
Cookies Name: Content (Session ID number)
Server storage:
Session Session ID: content (user name, login information)
Main usage scenario: Cookies + Session
1.3.4 Different Solutions for Session Sharing
1. Session files are shared using NFS
Session files are shared with rsync SCP
3. Store the session contents in the database (mysql), all machines can read through IP: port
4. Store the session contents in the cache database, all machines can read through IP: port
Benefits: Power off, restart lost data features. Periodically clear data; Improve the concurrent
1.3.5 Advantages of the memcache Principle
If you start the Memcached inch, it will be allocated a memory space based on the specified memory size parameter. When we read all kinds of service data from the database, the data will be stored in Memcached together. The next time a user requests the same data, the program will fetch the data directly from Memcached and return it to the user.
Advantages:
① For users, users access the website faster, better experience. #
② For websites, the database pressure is reduced. The database is requested only when there is no data in memory. Data written to the database for the first time is also requested. Typically, the company does not warm up until the user reads the database and puts it into Memcached.
③ Improve the site’s concurrent access, reduce the number of servers.
Schematic diagram
1.4 Memcached Distributed Cache cluster
Memcached does not naturally support distributed clustering; you need to programmatically support distributed storage
1.4.1 Memcached Distributed cache cluster features
1. The memory contents of all MC servers are different. The server content adds up to nearly the size of the database. For example, with a 1TB database, one cache database does not have that much memory, so it is divided into 10 cache servers.
2. Distribute the same content to an MC server by using the HASH algorithm on the client (Web) program or MC’s load balancer.
3. Common HASH algorithms cause a large amount of data flow (failure) when nodes go down, which may cause an avalanche effect.
4. Consistent HASH minimizes data flow (failure) on nodes when nodes are down.
*** Ordinary hash**** algorithm *
First, the key is processed as a 32-bit string, and the first eight bits are hashed into integers and returned. Then, the key is mapped to one of the servers to obtain the configuration of one of the servers, and the distributed deployment is completed using this configuration. A normal hash distribution works fine as long as the number of servers doesn’t change, and when the number of servers does, the problem starts. For example, if a server is added, the result of the same key being hashed with the server must be different from the result before the addition of the same key. As a result, the data saved before will be lost.
*** Consistent hash**** algorithm *
Consistent hashing algorithm
Advantages: In a distributed cache, when one of the caches breaks down, key migration is most efficient
Sort the list of servers, matching adjacent servers according to mHash($key)
The consistent hash**** algorithm minimizes data flow
The resources
http://blog.csdn.net/cywosp/article/details/23397179
http://blog.csdn.net/zhangskd/article/details/50256111
Copy the code
Chapter 2 using Memcached
2.1 installation memcached
2.1.1 Environment Description
[root@cache01 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@cache01 ~]# uname -r El7.x86_64 [root@cache01 ~]# getenForce Disabled [root@cache01 ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: Man: Firewalld (1) [root@cache01 ~]# hostname -I 10.0.0.21 172.16.1.21Copy the code
2.1.2 install memcached
[root@cache01 ~]# yum -y install memcached
Copy the code
2.1.3 Viewing configurations
[root@cache01 ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
Copy the code
2.1.4 Viewing the Startup Script
[root@cache01 ~]# cat /usr/lib/systemd/system/memcached.service
[Unit]
Description=Memcached
Before=httpd.service
After=network.target
[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS
[Install]
WantedBy=multi-user.target
Copy the code
2.1.5 Starting the Service
[root@cache01 ~]# systemctl start memcached.service
Copy the code
2.2 management memcached
2.2.1 Memcached Database Syntax format
set key 0 0 10
<command name> <key> <flags> <exptime> <bytes>\r\n
Copy the code
\n Wrap a line and move the cursor to the beginning of the line
The \r cursor moves to the beginning of the line without newline
parameter | instructions |
---|---|
Is any 16-bit unsigned integer (written in decimal) stored on the server along with the data and send block when the content is retrieved. Clients can use it as a “bitfield” to store specific information; It is opaque to the server. | |
Is the end time. If 0, the item never expires (although it may be deleted to make room for other cache items). If it is non-zero (the Unix timestamp or second offset from the current time), the client will no longer be able to retrieve this content after the end time is reached | |
Is the length of the subsequent data block in bytes, excluding “\r\n” for paging. It can be 0 (followed by an empty data block). | |
\r\n | Is a large 8-bit chunk of data whose length is specified on the previous command line. |
2.2.2 Database Use
Write read data
[root @ cache01 ~] # printf "set key008 0 0 10 \ r \ noldboy0987 \ r \ n" | nc 10.0.0.21 11211 STORED/root @ cache01 ~ # printf "get Key008 \ r \ n "| nc VALUE key008 10.0.0.21 11211 0 10 oldboy0987 ENDCopy the code
The length of the written data does not meet the requirement. The definition is too large
[root @ cache01 ~] # printf "set key009 0 0 11 \ r \ noldboy0987 \ r \ n" | nc 10.0.0.21 11211 / root @ cache01 ~ # printf "get Key009 \ r \ n "| nc 10.0.0.21 END of 11211Copy the code
The length of the data to be written is too small
[root @ cache01 ~] # printf "set key010 0 0 9 \ r \ noldboy0987 \ r \ n" | nc 10.0.0.21 11211 CLIENT_ERROR bad data, the chunk of the ERROR [root @ cache01 ~] # printf "get key010 \ r \ n" | nc 10.0.0.21 END of 11211Copy the code
timeliness
[root @ cache01 ~] # printf "set key011 0 10 10 \ r \ noldboy0987 \ r \ n" | nc 10.0.0.21 11211 STORED/root @ cache01 ~ # printf "get Key011 \ r \ n "| nc VALUE key011 10.0.0.21 11211 0 10 oldboy0987 END/root @ cache01 ~ # printf" get key011 \ r \ n "| nc 10.0.0.21 11211 ENDCopy the code
Delete the data
[root @ cache01 ~] # printf "delete key008 \ r \ n" | nc 10.0.0.21 11211 does/root @ cache01 ~ # printf "get key008 \ r \ n" | nc 10.0.0.21 11211 ENDCopy the code
2.3 Memcache PHP client installation
Command set
# compiler in php_mem tar ZXVF memcache - 2.2.5. TGZ CD memcache - 2.2.5 / application/PHP/bin/phpize. / configure -- enable - memcache --with-php-config=/application/ PHP /bin/php-config --with-zlib-dir make make install # extension=memcache.so' /application/php/lib/php.ini pkill php /application/php/sbin/php-fpm -t /application/php/sbin/php-fpm /application/php/bin/php -m|grep memcacheCopy the code
Checking the Current Environment
Look at PHP modules
View Code to View PHP modules
* Execution process *
Compile the installation
[root@web06 memcache-2.2.5]# make install shared extensions: - 5.5.32 / application/PHP/lib/PHP/extensions/no - debug - non - ZTS - 20121212 / [root @ web06 memcache -- 2.2.5] # ls / application/PHP/lib/PHP/extensions/no - debug - non - ZTS - 20121212 / memcache. So [root @ web06 memcache -- 2.2.5] # sed - I "$a The extension = memcache. So '/ application/PHP/lib/PHP ini [root @ web06 memcache -- 2.2.5] # pkill PHP [root @ web06 memcache -- 2.2.5] # /application/php/sbin/php-fpm -t [17-Nov-2017 11:39:13] NOTICE: Configuration file /application/php-5.5.32/etc/php-fpm.conf test is successful [root@web06 memcache-2.2.5]# / application/PHP/sbin/PHP - FPM [root @ web06 memcache -- 2.2.5] # / application/PHP/bin/PHP -m | grep memcache memcacheCopy the code
2.3.1 Writing test files
[root@web01 blog]# cat /application/nginx/html/blog/mc.php <? php $memcache = new Memcache; $memcache->connect('10.0.0.21', 11211) or die ("Could not connect"); $memcache->set('key20171117', 'hello,world'); $get_value = $memcache->get('key20171117'); echo $get_value; ? >Copy the code
Browser access
Database read test
/ root @ cache01 ~ # printf "get key20171117 \ r \ n" | nc 10.0.0.21 VALUE 11 hello key20171117 0, 11211 world ENDCopy the code
2.4 Web Management memcached
The software used is memadmin
Website: www.junopen.com/memadmin/
Put the package in the site directory for your browser to access
[root @ web06 tools] # tar xf memadmin - 1.0.12. Tar. Gz - C/application/nginx/HTML/blog /Copy the code
The default user name and password are admin
Add a new memcached server
Web interface management in Chinese, relatively simple
2.5 Memcached Data Cache
Programmatically
2.5.1 Memcached storage for blog sites
[root@web06 ~]# cat /application/nginx/html/blog/wp-content/object-cache.php
Copy the code
2.6 Memcached Session Sharing
Method 1:
Through the program implementation, Web01 only needs to write sessions to Memcahce, web02 reads sessions from Memcahce ** (more generality) **
Method 2:
Use PHP configuration files to make PHP store sessions in memcached by default
sed -i 's#session.save_handler = files#session.save_handler = memcache#; $a session. Save_path = "TCP: / / 10.0.0.21:11211" '/ application/PHP/lib/PHP iniCopy the code
To use this functionality, you need to use PHP’s session function
From: clsn. IO
Welcome to pay attention to the public number [code farming blossom] learn to grow together I will always share Java dry goods, will also share free learning materials courses and interview treasure book reply: [computer] [design mode] [interview] have surprise oh