Use cache method if enabled

This commit is contained in:
Michael RICOIS 2017-05-10 12:57:07 +02:00
parent 67531c5c6c
commit 1a7b94993d
2 changed files with 17 additions and 229 deletions

View File

@ -1,221 +0,0 @@
<?php
use Predis\Client;
class RedisCache
{
/**
* @var Predis\Client
*/
protected $redis;
/**
* @var bool Connection status
*/
protected $is_connected = false;
public function __construct()
{
$this->connect();
}
public function __destruct()
{
$this->close();
}
/**
* Connect to server
*/
public function connect()
{
if (class_exists('\\Predis\\Client') && extension_loaded('redis')) {
$this->redis = new Predis\Client([
'scheme' => 'tcp',
'host' => _REDIS_HOST_,
'port' => _REDIS_PORT_,
]);
} else {
return;
}
// Don't work
//$this->is_connected = $this->redis->isConnected();
// Don't work
$info = $this->redis->info('server');
if (array_key_exists('Server', $info)) {
$this->is_connected = true;
}
}
/**
* @see Cache::_set()
*/
protected function _set($key, $value, $ttl = 0)
{
if (!$this->is_connected) {
return false;
}
return $this->redis->setex($key, $ttl, $this->encode($value));
}
/**
* @see Cache::_get()
*/
protected function _get($key)
{
if (!$this->is_connected) {
return false;
}
return $this->decode($this->redis->get($key));
}
/**
* @see Cache::_exists()
*/
protected function _exists($key)
{
if (!$this->is_connected) {
return false;
}
return ($this->redis->get($key) !== false);
}
/**
* @see Cache::_delete()
*/
protected function _delete($key)
{
if (!$this->is_connected) {
return false;
}
return $this->redis->del($keys);
}
/**
* @see Cache::_writeKeys()
*/
protected function _writeKeys()
{
if (!$this->is_connected) {
return false;
}
return true;
}
/**
* @see Cache::flush()
*/
public function flush()
{
if (!$this->is_connected) {
return false;
}
return $this->redis->flushdb();
}
/**
* Store a data in cache
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl = 0)
{
return $this->_set($key, $value, $ttl);
}
/**
* Retrieve a data from cache
*
* @param string $key
* @return mixed
*/
public function get($key)
{
return $this->_get($key);
}
/**
* Check if a data is cached
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return $this->_exists($key);
}
/**
* Delete one or several data from cache (* joker can be used, but avoid it !)
* E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');
*
* @param string $key
* @return bool
*/
public function delete($key)
{
if ($key == '*') {
$this->flush();
} elseif (strpos($key, '*') === false) {
$this->_delete($key);
} else {
$pattern = str_replace('\\*', '.*', preg_quote($key));
$keys = $this->redis->keys($pattern);
foreach ($keys as $key => $data) {
if (preg_match('#^'.$pattern.'$#', $key)) {
$this->_delete($key);
}
}
}
return true;
}
/**
* Close connection to server
*
* @return bool
*/
protected function close()
{
if (!$this->is_connected) {
return false;
}
return $this->redis->quit();
}
/**
* Encode data
*
* @param $data anything
* @return text value encode
*/
public function encode($data)
{
return json_encode($data);
}
/**
* Decode data
*
* @param $data anything
* @return text value decode
*/
public function decode($data)
{
return json_decode($data);
}
}

View File

@ -13,7 +13,7 @@ class IndexController extends IndexControllerCore
$posts = CmsPsPost::getPostHome($id_lang); $posts = CmsPsPost::getPostHome($id_lang);
*/ */
$redis = new RedisCache(); /*$redis = new RedisCache();
$key = 'index:cmspsedito:getlast:'.$id_lang; $key = 'index:cmspsedito:getlast:'.$id_lang;
if ($cache = $redis->get($key)) { if ($cache = $redis->get($key)) {
$edito = $cache; $edito = $cache;
@ -28,18 +28,27 @@ class IndexController extends IndexControllerCore
} else { } else {
$posts = CmsPsPost::getPostHome($id_lang); $posts = CmsPsPost::getPostHome($id_lang);
$redis->set($key, $posts, 360); $redis->set($key, $posts, 360);
}*/
$cache = Cache::getInstance();
$key = 'cmspsedito:getlast'.$id_lang;
if (!$edito = $cache->get($key)) {
$edito = CmsPsEdito::getLast($id_lang);
$cache->set($key, $edito);
}
$key = 'cmspsedito:getposthome:'.$id_lang;
if (!$posts = $cache->get($key)) {
$posts = CmsPsPost::getPostHome($id_lang);
$cache->set($key, $posts);
} }
$constructor = Hook::exec('displayConstructorLink'); $constructor = Hook::exec('displayConstructorLink');
if (empty($constructor)) { if (empty($constructor)) {
$key = 'index:cmspsedito:gethomeedito:'.$id_lang; $key = 'cmspsedito:gethomeedito:'.$id_lang;
if ($cache = $redis->get($key)) { if (!$homeEdito = $cache->get($key)) {
$homeEdito = $cache;
} else {
$homeEdito = CmsPsPost::getHomeEdito($id_lang); $homeEdito = CmsPsPost::getHomeEdito($id_lang);
$redis->set($key, $homeEdito, 360); $cache->set($key, $homeEdito);
} }
/* /*
$homeEdito = CmsPsEdito::getHomeEdito($id_lang); $homeEdito = CmsPsEdito::getHomeEdito($id_lang);
$homeEdito->smallTitle = strlen($homeEdito->title) > 30; $homeEdito->smallTitle = strlen($homeEdito->title) > 30;