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

Two .Memcache

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

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):

start-up memcache service

Front desk mode memcache Start the service

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

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

memcache Related parameter settings :

Start by setting specific parameters memcache The format of :

to memcache Set boot service

Set boot service :

Generate boot service :

Start the service by naming :

php Install in memcache Expand

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

modify php.ini, Turn it on memcache Expand :

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

php Yes memcache Use

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

php Middle link memcache The server :

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”

Yes weather Of key And then query :

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 .

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

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 )

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 :

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

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

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 :

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 .

Using terminal operation memcache

see memcache The state of :

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 .

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 :

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 .

Data read failed :

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 .

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.

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 )

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 .

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 :

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

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

Distributed cluster settings :

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 .

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

Take you in memcache, More articles on old memory caching technology

  1. picture _ _ Image cache memory cache technology LruCache, Soft citation

    Whenever you come across some big pictures , If we don't process the pictures, we will report OOM abnormal , This problem used to annoy me , It was finally solved , Now let me share it with you . The picture caching mechanism in this blog post , I've been exposed to two clocks , One ...

  2. Image cache memory cache technology LruCache, Soft citation

    Whenever you come across some big pictures , If we don't process the pictures, we will report OOM abnormal , This problem used to annoy me , It was finally solved , Now let me share it with you . The picture caching mechanism in this blog post , I came into contact with two ...

  3. turn Image cache memory cache technology LruCache, Soft citation

    Whenever you come across some big pictures , If we don't process the pictures, we will report OOM abnormal , This problem used to annoy me , It was finally solved , Now let me share it with you . The picture caching mechanism in this blog post , I've been exposed to two clocks , One ...

  4. 【 turn 】 Image cache memory cache technology LruCache、 Soft citation Compare

    Whenever you come across some big pictures , If we don't process the pictures, we will report OOM abnormal , This problem used to annoy me , It was finally solved , Now let me share it with you . The picture caching mechanism in this blog post , I've been exposed to two clocks , One ...

  5. Five minutes to learn back-end technology : How to learn Redis、memcache And so on

    Original statement The author of this article : Huang Xiaoxie For reprint, please indicate the source and the author at the beginning of the article . This article's mind map What is caching The cache in the computer Do back-end development students , It must be familiar with caching , Usually we may use Redis,MemCache This kind of ...

  6. Memcached Memory caching technology

    Memcached What is it? , What's the role ? Memcached It's an open source . High performance memory caching Software , In terms of name Mem It means memory , and Cache It means cache . Memcached By planning the memory space in advance ...

  7. be based on .net The current limiting component of distributed system C# DataGridView binding List Object time , utilize BindingList To add, delete, check and change .net in ThreadPool And Task Knowledge summary of C# Sorting technology research and comparison be based on .net General memory cache model component of Scala Learning notes : Important grammatical features

    be based on .net The current limiting component of distributed system   In Internet applications , Peak flow is a common occurrence . In response to peak discharge , The general processing mode is queuing . Current limiting , This can protect the system directly and effectively , Prevent the system from exploding . in addition , By means of current limiting technology , can ...

  8. be based on .net General memory cache model component of

    When it comes to caching , Naturally, we think about the benefits of caching , such as : Reduce the system pressure of high concurrency data reading : Static data access . Dynamic data access Store preprocessed data , Improve system response speed and TPS Reduce the system pressure of high concurrent data writing Improve system availability , backstage ...

  9. Android LruCache(Picasso Memory cache )

    Cache Save a strong reference to limit the amount of content , whenever Item When interviewed , this Item It moves to the head of the queue , When cache Add a new one when it's full item when , At the end of the queue item Will be recycled . If you cache Some value of needs to be ...

  10. Android The memory cache of picture level 3 cache ( Farewell to soft citation (SoftRefrerence) And weak quotes (WeakReference))

    Because my project colleagues used the picture level 3 cache before , Today, I found that my colleagues still used soft reference when sorting out the project (SoftRefrerence) And weak quotes (WeakReference), To manage the cache in memory . I don't feel right seeing this . mind ...

Random recommendation

  1. Spark Add in the baseline

    def baseLine(sc: SparkContext): Unit = { println("--------------------baseLine start----------- ...

  2. subprocess Implementation pipeline

    # shell last | cut -d ' ' -f 1 | sort -u #python from subprocess import Popen,PIPE p1 = Popen('last' ...

  3. 【Python Study 】 Calculate the distance between the longitude and latitude of two specified points

    Calculate the distance between the longitude and latitude of two specified points #coding=utf-8 from math import * # input Lat_A latitude A # input Lng_A longitude A # input Lat_B ...

  4. Use C# Of aforge Class library identification verification code instance

    One : Verification code processing 1.  General treatment principles Why is this kind of verification code the same as none , The first point : The font is standard and neat , Second point : No rotation, twist, adhesion , The third point : The font color is single , Let's look at the processing steps . What we should pay attention to here is ,aforge Only accept pixel format as ...

  5. 02-cookie Easy to use

    @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletExcep ...

  6. xfs Some of the tools used

    [root@localhost caq]# xfs_db -c frag -r /dev/sdaw actual , ideal , fragmentation factor 82.56% Note, ...

  7. python scrapy At the same time spiders Multiple reptiles

    hypothesis spiders Multiple files in the folder : name.py     name = 'name' name1.py    name = 'name1' name2.py    name = 'name2' . ...

  8. java in URL and File The mutual transformation of

    First , I want to compare the two .URL For the Internet , So there's an obvious protocol, And the support for Chinese and symbols is not good .File It's the file path in our normal system , Support for both Chinese and symbols , But no more protocol ...

  9. CocoaPods error

    1 Error: pod search Masonry /usr/local/lib/ruby/gems/2.3.0/gems/cocoapods-1.4.0.beta.2/lib/cocoapods ...

  10. Decompressed version of Chinese garbled problem MYSQL Chinese garbled

    The installation is a decompressed version of MYSQL, Refer to :https://jingyan.baidu.com/article/9c69d48f85032f13c9024e15.html . 1: After decompression copy One ...