start redis

This commit is contained in:
Thibault UBUNTU 2017-05-04 14:46:23 +02:00
parent 0cafafe3dd
commit 34cd65f6f1
3 changed files with 256 additions and 1 deletions

26
classes/Redis.php Normal file
View File

@ -0,0 +1,26 @@
<?php
require _PS_ROOT_DIR_.'/tools/predis/autoload.php';
class RedisCore
{
protected static $_instance = null;
/**
* @return Predis instance
*/
public static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new Predis\Client(
array(
'scheme' => 'tcp',
'host' => _REDIS_HOST_,
'port' => _REDIS_PORT_,
)
);
}
return self::$_instance;
}
}

220
classes/cache/CacheRedis.php vendored Normal file
View File

@ -0,0 +1,220 @@
<?php
class CacheRedisCore extends CacheCore
{
protected static $_instance;
private $is_connected;
private $redis_client;
/**
* Construct CacheRedis Instance
* @param $redis Predis Object
*
* check if is_connected
*
*/
private function __construct($redis)
{
$this->redis_client = $redis;
try {
$this->redis_client->connect();
$this->is_connected = $this->redis_client->isConnected();
} catch (Exception $e) {
$this->is_connected = false;
}
}
/**
* @return CacheRedis
*/
public static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new self(Redis::getInstance());
}
return self::$_instance;
}
/**
* Cache a data
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
protected function _set($key, $value, $ttl = 0)
{
if (!$this->isConnected()) {
return false;
}
$this->redis_client->setex($this->createKey($key), $ttl, $this->encode($value));
}
/**
* 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 cached data by key
*
* @param string $key
* @return mixed
*/
protected function _get($key)
{
if (!$this->isConnected()) {
return false;
}
$value = $this->redis_client->get($this->createKey($key));
if (is_null($value)) {
return false;
} else {
return $this->decode($value);
}
}
/**
* Retrieve a data from cache
*
* @param string $key
* @return mixed
*/
public function get($key)
{
return $this->_get($key);
}
/**
* Check if a data is cached by key
*
* @param string $key
* @return bool
*/
protected function _exists($key)
{
return true;
}
/**
* Check if a data is cached
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return $this->_exists($key);
}
/**
* Delete a data from the cache by key
*
* @param string $key
* @return bool
*/
protected function _delete($key)
{
if (!$this->isConnected()) {
return false;
}
return $this->redis_client->del($key);
}
public function delete($key)
{
return $this->_delete($key);
}
/**
* Write keys index
*/
protected function _writeKeys()
{
return true;
}
/**
* Clean all cached data
*
* @return bool
*/
public function flush()
{
return $this->redis_client->flushall();
}
private function _expire($key, $expire)
{
return $this->redis_client->expire($this->createKey($key), $expire);
}
public function expire($key, $expire)
{
if (!$this->isConnected()) {
return false;
}
return $this->_expire($key, $expire);
}
public function isConnected()
{
return $this->is_connected;
}
/*
* Ping Redis connexion
*
* return pong if is ok
*/
public function ping()
{
if (!$this->isConnected()) {
return false;
}
return $this->redis_client->ping();
}
public function createKey($key)
{
return $key;
}
/**
* 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

@ -7,7 +7,16 @@ class IndexController extends IndexControllerCore
public function initContent()
{
$id_lang = Context::getContext()->language->id;
// $redis = CacheRedis::getInstance();
// $key = 'index_controller_edito'.$id_lang;
// if ($result_cache = $redis->get($key)) {
// $edito = $result_cache;
// } else {
// $edito = CmsPsEdito::getLast($id_lang);
// $redis->set($key, $edito, 60);
// }
$edito = CmsPsEdito::getLast($id_lang);
$posts = CmsPsPost::getPostHome($id_lang);