当前位置:网站首页>Take you into Memcache, the old memory cache technology

Take you into Memcache, the old memory cache technology

2021-01-24 09:59:59 Code2020

One . Overview of core optimization

What is optimization : Support the operation of website with larger load with smaller resources , throw a sprat to catch a herring .

Ideas : Minimize user waiting time , Save system resources , Save bandwidth usage .

Optimize where ? There are three aspects :Memcache Memory caching technology 、 Static technology 、mysql Optimize

 Ten minutes to learn memcache, It's easier than you think

Two .Memcache

Memory caching technology :memcache It's the realization of php Language On memory The intermediate medium for operation .

 Ten minutes to learn memcache, It's easier than you think

memcache And redis The difference and connection

redis: Support more data types (String/list/set/sortset/hash),redis Support set computing (set Type of support ), Every key The maximum amount of data storage is 1G,redis It's a new memory caching technology , The support for all aspects is not perfect , Support persistent operations .

memcache: Old memory caching technology , It has rich support for related fields ,window and linux You can use , All kinds of frames (tp/yii wait ) All support the use of ,session It's very convenient to save your information to the memcache in , Every key The maximum amount of data saved is 1M, The data types supported are relatively single , Namely String type , Persistence is not supported .

The similarity between the two is that they keep the data in memory .

Be careful : The above differences must be known , When it comes to third party optimization , It can be decided according to the actual situation !

install memcache

1,  Copy the server file to the running directory

Copy memcached.exe File to run Directory ( Such as :H:/memcached.exe):

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

start-up memcache service

Front desk mode memcache Start the service

 Ten minutes to learn memcache, It's easier than you think

The default is to start the foreground ,Ctrl+C You can end the foreground process .

here , There's already... In the process memcache service :

 Ten minutes to learn memcache, It's easier than you think

memcache Related parameter settings :

 Ten minutes to learn memcache, It's easier than you think

Start by setting specific parameters memcache The format of :

 Ten minutes to learn memcache, It's easier than you think

to memcache Set boot service

 Ten minutes to learn memcache, It's easier than you think

Set boot service :

 Ten minutes to learn memcache, It's easier than you think

Generate boot service :

 Ten minutes to learn memcache, It's easier than you think

Start the service by naming :

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

php Install in memcache Expand

Copy the file as shown in the figure to php Extended Directory :

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

modify php.ini, Turn it on memcache Expand :

 Ten minutes to learn memcache, It's easier than you think

After the restart apache that will do .php Turn on memcache Expansion success

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

php Yes memcache Use

 Ten minutes to learn memcache, It's easier than you think

memcache stay php Is an operation class , Specific use : Instantiate objects 、 Object to call a member's procedure .

php Middle link memcache The server :

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

php in memcache Use , Its data model is similar to redis Agreement , by key - value.

stay php Middle feeding memcache Set up a key, The name is “weather”

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Yes weather Of key And then query :

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

key Name : The key The components of the name are related to redis More similar , All the information that can be entered by the basic keyboard can be used as key The name part of ,key The maximum length of the is 250 byte .

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Validity issues :$me -> set(key, value, Is it compressed? , The period of validity second );

There are two ways to set the validity period :

A. Time difference setting : From the current point in time, the time delayed by a specified second will expire ( Be overdue )

$me -> set(key, value, Is it compressed? , 120); // backward 120 Seconds expired , There are limitations to this approach : The biggest time difference is 30 God (2592000 second )

B. Timestamp settings : from 1970-1-1 0:0:0 The number of seconds passed by now ,$me -> set(key, value, Is it compressed? , time()+120); // backward 120 Seconds expired

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

The maximum validity period of time difference mode is 30 God :

( If you need to set key Is valid for more than 30 Days of , Must use “ Time stamp ” Mode setting )

 Ten minutes to learn memcache, It's easier than you think

Storage of various data types :php Data type of : Scalar type :int string boolean float The compound type :array object resource null

memcache Store scalar type data , Turn them all into ”String character string ” Type storage .

Store composite type data , direct “ Prototype ” Storage .

Scalar type storage :

 Ten minutes to learn memcache, It's easier than you think

Scalar type information in memcache All of them become “String character string ” Type information

 Ten minutes to learn memcache, It's easier than you think

Composite type information storage : stay memcache It's directly “ Prototype ” Storage .

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Prototype storage consumes a lot of resources , In order to save resources , You can store compound type information in string form , In this way, we need to analyze the composite information serialize operation : serialize() unserialize()

Compression options

$me -> set(key, value, Is it compressed? 0/1, The period of validity second );

Whether or not compression features : Compress : Memory space savings 、 Running speed is a little slow . non-compressed : Less memory space 、 Fast running speed .

The compression principle is zlib technology :

 Ten minutes to learn memcache, It's easier than you think

zlib---->php----->memcache

Other related operation methods can refer to the corresponding documents , I won't go into details here ! Go straight to the point

summary :

1. memcache Install and turn on services to use : Opening service :① Front desk open ② Set boot service

2. to php to open up memcache Expand :① Copy extension file php_memcache.dll、② php.ini Open extended parameters

3. php Inside operation memcache:① Instantiate objects 、② Object connection server 、③ Object calls related methods to implement specific operations

set(key,value, Compress , The period of validity ) ,get() delete() connect()

Terminal operation memcache

Connect memcache: Using protocols telnet( Remote connection protocol ), Realize to memcache The operation of .

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Using terminal operation memcache

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

see memcache The state of :

 Ten minutes to learn memcache, It's easier than you think

Distributed deployment memcache

Distributed : Put the original one memcache What the server does , Now it's spread to multiple memcache perform . This will reduce memcache Workload for .

for example : a memcache Need to store 100w Of key, If there is 5 Servers , Then each server stores 20w Can .

The distributed and redis The master-slave model is similar , But they are different

redis: It's a master-slave model , One redis Responsible for data writing , Other multiple redis Responsible for data reading

memcache: It's not a master-slave model , The distributed system is to share the work equally , Every sub server is level with each other , Every server has to write data 、 Read operation .

The following figure for memcache The schematic diagram of the distribution in the :

As you can see from the diagram ,memcache There are algorithms , Data can be guaranteed “ Average ” Stored in different servers ,php Language and each server are connected through the algorithm , Further talk ,php The internal implementation is consistent with the previous code style .

 Ten minutes to learn memcache, It's easier than you think

memcache Distributed concrete implementation type :

① In a server , Turn on multiple memcache service

② Buy multiple servers , One in each server memcache service

Use distributed : Turn on multiple memcache service :

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Be careful :memcache When clusters are used ,key Setting and getting , Their server order must be strictly consistent , Otherwise, there are errors in the use of data .

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Data read failed :

 Ten minutes to learn memcache, It's easier than you think

Cache invalidation

Expiration date : Specifically through “ lazy ” Mechanism to delete the expired data , And expired session The deletion of is similar to .

Be overdue session Delete mechanism :session It's on a hard disk in the form of a file , If there are session The file is out of date , Then session The file will not be deleted immediately , But later other users visit the website to use session At the same time, there will be a certain chance to trigger the deletion of expired session file .

memcache Overdue data deletion is also implemented by lazy mechanism , If there is a key Out of date , It itself will not be deleted immediately , But we call get Method to get the data and delete the expired data at the same time .

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

Out of cache space

If more than memcache The biggest storage limit ( The default is 64M), The data continues to be stored at this time , You'll take the ones you don't use very often recently key Is deleted . The name of the mechanism is LRU(least recently use) Give priority to delete those that are good to use recently key.

 Ten minutes to learn memcache, It's easier than you think

The LRU The mechanism can be disabled according to the actual situation , If you continue to use a full load of memcache Then the system will report an error .

( When the service is turned on, you can set -M Parameters , Ban LRU Mechanism )

 Ten minutes to learn memcache, It's easier than you think

session Deposit in memcache

session Can deposit mysql In the database .

demand : A large website has been developed , The internal servers are usually composed of multiple servers , Multiple servers need to share with each other session Information , This requires session It's bound to be deposited in mysql or memcache in .

session The information is stored as a file inside the server , Can't share multiple servers , Only deposited mysql or memcache In order to realize data sharing .

 Ten minutes to learn memcache, It's easier than you think

mysql or memcache Multiple servers can be shared with each other session Information .

Specific use :php.ini Is punctuated with session How to store and where to save set parameters :

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

session Store information to memcache Of key For the name of the session_id():

Put... In the terminal session Get the information :

 Ten minutes to learn memcache, It's easier than you think

Distributed cluster settings :

 Ten minutes to learn memcache, It's easier than you think

memcache Case study

The website has a page , A lot of data information is needed internally , The data information does not change in a short time , In order to reduce mysql The load of , Just get this data and put it in memacache In the past , For follow-up visits .

 Ten minutes to learn memcache, It's easier than you think

 Ten minutes to learn memcache, It's easier than you think

For the first time , The cache has no data , From mysql Databases get data , For users , At the same time, the data is stored in the cache for subsequent use

The second time ( follow-up ) Use , The cache has data , It can be used directly .

Finally, let's talk about redis The difference between :

redis Distributed : A master-slave mode

memcache Distributed : hold key Evenly distributed to each server ,addServer( Host name , port );

Be careful :key Set and read multiple memcache The order of loading should be consistent

Distributed types :① One server, multiple services 、② Multiple servers, multiple services

redis: Can store slightly more complex data (list、set、sortset、hash) For set computation

memcache: There are many areas of support (win Systems and linux The system can be used 、 Various frameworks support the use of 、 Allow to put session Information in memcache in ) Generally, the stored information is relatively simple , For example, string information , You can use memcache

版权声明
本文为[Code2020]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210124095916257s.html

随机推荐