PHP Web site redis cache mode Analysis _php tutorial

Source: Internet
Author: User

PHP Web site redis Cache mode analysis


PHP Web site redis Cache mode analysis

Author: wucl

Date: 2014-02-05

Chapter Content: Basic background, analysis content, personal experience (this person is very not moral character, think of what to write. )。

1. Basic background:

To improve page access speed, reduce access to database pressure.

2. Redis Cache Analysis:

First, 3 questions are raised:

1) Do you cache data for the entire Web site middleware?

2) If data Redis that caches the entire middleware can be pressurized?

3) does PHP cache Redis have an impact on how the middleware is cached?

There are two types of plans available:

A scenario: Cache time is short, generally 120s or less,

B Scenario: Cache time is longer, generally 84600s.

Programme A

Development angle: Cache operation is more frequent, but can share the pressure of the middleware part.

Edit angle: You can see the effect for up to 2-3 minutes after editing the data, so you don't have to clear the cache with actions.

User perspective: Assuming a time period of 10 minutes, the page load performance is fast and slow during this time period.

Programme b

Development perspective: Cache is not frequent, can share a large part of the middleware pressure, recommended in this way.

Edit angle: The previous cache must be purged with specific actions after editing.

User angle: Page load is stable and fast.

The cache is associated with an existing project:

1) Redis establishes a connection (using a long connection):

Pconnect: Static Variables for classes

private static function getredisobject ($ip = ' 127.0.0.1 ', $port = ' 6379 ') {

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) The main application of 3 method:

$conn->delete ($key)

$conn->get ($key)

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

3) attention to abnormal redisexception

4) Master-slave synchronization Just do one operation:

Modifying slaveof from redis.conf is similar to:

slaveof 127.0.0.1 6379

5) Master Redis can be added, modified, slave redis do query. Link blocking is resolved with sleep, and the following is how the actual project is linked (the parameters are not explained more):

Private Function Redisconn () {

if (!empty (static:: $memInstance [' Redis ']) && static:: $memInstance [' Redis '] instanceof Redis) {

$cacheConn = static:: $memInstance [' Redis '];

try{

$cacheConn->ping (); Link does not have an exception, the link 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 * 0.3);

$tryI + +;

$cacheConn = null;

}

}

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

return $cacheConn;

}

http://www.bkjia.com/PHPjc/962644.html www.bkjia.com true http://www.bkjia.com/PHPjc/962644.html techarticle PHP Web site Redis Caching method Analysis of the PHP Web site Redis Cache Author: Wucl time: 2014-02-05 Chapter Content: Basic background, analysis content, personal experience (this person is very not moral character ...)

  • 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.