Analysis on redis Cache mode of PHP website

Source: Internet
Author: User
Tags pconnect
Analysis of redis Cache methods on the PHP website

Author: Wucl

Time: 2014-02-05

Chapter content: basic background, analysis content, and personal experience (this person has no moral character and writes anything he thinks .).

1. basic background:

To speed up page access and reduce the pressure on accessing the database.

2. Redis Cache analysis:

First, three questions are raised:

1) is the Middleware data of the entire website cached?

2) if the data of the entire middleware is cached, can redis be under pressure?

3) does PHP Cache redis affect the cache mode of Middleware?

There are two existing plans:

Solution A: The cache time is short, generally within S,

Solution B: the cache time is long, generally 84600 s.

Solution

Development Perspective: cache operations are frequent, but the pressure on the middleware can be shared.

Editing angle: after you edit the data, you can view the results at most 2-3 minutes. Therefore, you do not need to clear the cache through operations.

User's point of view: if the time is 10 minutes, page loading during this time period is slow.

Solution B

From the development perspective: the cache is not frequent, which can relieve a large amount of pressure on the middleware. we recommend that you use this method.

Editing angle: after editing, you must clear the previous cache through a specific operation.

User perspective: the page loading speed is stable and fast.

Association between cache and existing projects:

1) establish a connection with Redis (using persistent connections ):

Pconnect: static variables of the class

Private static function getRedisObject ($ ip = '192. 0.0.1 ', $ port = '20160901 '){

Try {

If (isset (static: $ pconnect ['redis '. $ ip. $ port]) {

$ Redis = static: $ pconnect ['redis '. $ ip. $ port];

} Else {

$ Redis = new Redis ();

$ Redis-> pconnect ($ ip, $ port );

$ Redis-> select (1 );

Static: $ pconnect ['redis '] = $ redis;

}

Try {

$ Redis-> ping ();

} Catch (\ RedisException $ e ){

$ Redis-> pconnect ($ ip, $ port );

$ Redis-> select (1 );

Static: $ pconnect ['redis '] = $ redis;

}

} Catch (\ RedisException $ e ){

Echo $ e-> getMessage ().'
';

}

Return $ redis;

}

2) three methods are mainly used:

$ Conn-> delete ($ key)

$ Conn-> get ($ key)

$ Conn-> setex ($ key, $ expire, $ data)

3) note that the RedisException

4) only one operation is required for master-slave synchronization:

Modify slaveof from redis. conf as follows:

Slaveof 127.0.0.1 6379

5) you can add and modify master redis and query slave redis. Link blocking is solved by sleep. The following is the link mode of the actual project (the parameters are not explained ):

Private function redisConn (){

If (! Empty (static: $ memInstance ['redis ']) & static: $ memInstance ['redis'] instanceof redis ){

$ CacheConn = static: $ memInstance ['redis '];

Try {

$ CacheConn-> ping (); // if the link is not abnormal, the linked instance is returned.

Return $ cacheConn;

} Catch (\ RedisException $ e ){}

}

$ CacheConn = null;

$ TryI = 0;

While ($ cacheConn = null & $ tryI <10 ){

Try {

$ CacheConn = new Redis ();

$ ServerSetting = Config: $ redis;

If (! $ CacheConn-> pconnect ($ serverSetting [$ this-> _ serverType] ['IP'], $ serverSetting [$ this-> _ serverType] ['port']) {

$ This-> _ serverType = "default ";

$ CacheConn-> pconnect ($ serverSetting [$ this-> _ serverType] ['IP'], $ serverSetting [$ this-> _ serverType] ['port']);

}

$ CacheConn-> setOption (Redis: OPT_SERIALIZER, Redis: SERIALIZER_NONE );

// Select DB

$ RedisDB = $ serverSetting [$ this-> _ serverType] ['redisdb'];

If ($ redisDB> 0 & $ redisDB <= 16 ){

$ CacheConn-> select ($ redisDB );

} Else {

$ CacheConn-> select (0 );

}

} Catch (\ Exception $ e ){

Sleep ($ tryI * 1, 0.3 );

$ TryI ++;

$ CacheConn = null;

}

}

Static: $ memInstance ['redis '] = $ cacheConn;

Return $ cacheConn;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.