Home  >  Article  >  Backend Development  >  Analysis of redis caching method of PHP website_PHP tutorial

Analysis of redis caching method of PHP website_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:46822browse

Analysis of redis caching method of PHP website

Analysis of redis caching method of PHP website

Author: Wucl

Time: 2014-02-05

Chapter content: basic background, analysis content, personal experience (this person is very immoral and writes whatever comes to mind.).

1. Basic background:

In order to improve page access speed and reduce database access pressure.

2. Redis cache analysis:

First ask 3 questions:

1) Whether to cache the data of the entire website middleware?

2) If redis caches the entire middleware data, can redis bear the pressure?

3) Will PHP caching redis have an impact on the caching method of middleware?

There are two plans:

Plan A: The cache time is shorter, generally within 120s,

Plan B: The cache time is longer, generally 84600s.

Plan A

Development perspective: Caching operations are relatively frequent, but they can share the pressure on the middleware part.

Editing perspective: It takes up to 2-3 minutes to see the effect after editing the data, so there is no need to clear the cache.

User perspective: Assuming that a time period of 10 minutes is used, the page loading performance will be fast and slow during this time period.

Plan B

Development perspective: Caching is infrequent and can share a large part of the pressure on the middleware. It is recommended to use this method.

Editing perspective: After editing, the previous cache must be cleared through specific operations.

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

The cache is associated with an existing project:

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

pconnect: static variable of class

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) Mainly apply 3 methods:

$conn->delete ( $key )

$conn->get($key)

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

3) Pay attention to the exception RedisException

4) Master-slave synchronization only requires one operation:

Modify slaveof from redis.conf as follows:

slaveof 127.0.0.1 6379

5) Master redis can be used for additions and modifications, and slave redis can be used for queries. Link blocking is solved by sleep. The following is the link method of the actual project (no explanation of parameters):

private function redisConn(){

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

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

try{

$cacheConn->ping(); //If there is no exception in the link, return the link instance

return $cacheConn;

}catch(RedisException $e){}

}

$cacheConn = null;

$tryI = 0;

while ( $cacheConn == null && $tryI

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

$cacheConn->select ( $redisDB );

} else {

$cacheConn->select ( 0 );

}

} catch (Exception $e) {

sleep ( $tryI * 0.3 );

$tryI ++;

$cacheConn = null;

}

}

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

return $cacheConn;

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/962644.htmlTechArticleAnalysis of redis cache method of PHP website Analysis of redis cache method of PHP website Author: Wucl Time: 2014-02-05 Chapter content : Basic background, analysis content, personal experience (this person is very immoral...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn