Merge branch 'develop' of gitlab.antadis.net:dev-antadis/bebeboutik into develop

This commit is contained in:
Christophe Latour 2017-09-25 12:30:47 +02:00
commit 441c1f3134
6 changed files with 405 additions and 6 deletions

View File

@ -30,6 +30,11 @@ function __autoload($className)
if (function_exists('smartyAutoload') AND smartyAutoload($className))
return true;
if (function_exists('Predis\predisAutoload') AND Predis\predisAutoload($className)) {
return true;
}
$className = str_replace(chr(0), '', $className);
$classDir = dirname(__FILE__).'/../classes/';
$overrideDir = dirname(__FILE__).'/../override/classes/';

View File

@ -96,6 +96,7 @@ define('_PS_SWIFT_DIR_', _PS_TOOL_DIR_.'swift/');
define('_PS_FPDF_PATH_', _PS_TOOL_DIR_.'fpdf/');
define('_PS_TAASC_PATH_', _PS_TOOL_DIR_.'taasc/');
define('_PS_PEAR_XML_PARSER_PATH_', _PS_TOOL_DIR_.'pear_xml_parser/');
define('_PS_PREDIS_DIR_', _PS_TOOL_DIR_.'predis/');
/* settings php */
define('_PS_TRANS_PATTERN_', '(.*[^\\\\])');

View File

@ -3,6 +3,8 @@
if (!class_exists('Sale')) {
class Sale {
const CONTROLLER_NAME = 'sale';
var $id;
var $date_start;
var $date_end;
@ -1644,7 +1646,12 @@ class Sale {
$query .= ' LIMIT '.$limit;
}
if($sales = Db::getInstance()->ExecuteS($query)) {
if (false === ($sales = CacheRedis::getInstance()->getQuery($query, self::CONTROLLER_NAME))) {
$sales = Db::getInstance()->ExecuteS($query);
CacheRedis::getInstance()->setQuery($query, self::CONTROLLER_NAME, $sales);
}
if($sales) {
if($lite) {
foreach($sales AS $sale) {
$result[] = $sale['id_sale'];

View File

@ -0,0 +1,287 @@
<?php
use Predis\Collection\Iterator;
class CacheRedis
{
/*
* remember to add a definition for the 4 following parameters in settings.inc.php
define('_REDIS_AUTH_STRING_','')
define('_REDIS_HOST_STRING_','127.0.0.1')
define('_REDIS_PORT_STRING_','6379')
define('_REDIS_DEFAULT_TTL_','60')
* remember to add this line in defines.inc.php
define('_PS_PREDIS_DIR_', _PS_TOOL_DIR_.'predis/')
* autoload call to should be in autoload.php
*/
protected static $_instance;
private $auth;
private $port;
private $host;
private $db;
private $redis_client;
private $default_ttl;
private $short_ttl;
private $medium_ttl;
private $long_ttl;
private $is_connected = false;
const DEFAULT_CONTROLLER_NAME = "none";
const HASHING_ALGORITHM = "sha1";
const SHORT_TTL = '_REDIS_SHORT_TTL_';
const MEDIUM_TTL = '_REDIS_MEDIUM_TTL_';
const LONG_TTL = '_REDIS_LONG_TTL_';
public function __construct()
{
$this->auth = _REDIS_AUTH_STRING_;
$this->host = _REDIS_HOST_STRING_;
$this->port = _REDIS_PORT_STRING_;
$this->db = _REDIS_DB_STRING_;
$this->default_ttl = _REDIS_DEFAULT_TTL_;
$this->short_ttl = defined('_REDIS_SHORT_TTL_') ? _REDIS_SHORT_TTL_ : _REDIS_DEFAULT_TTL_;
$this->medium_ttl = defined('_REDIS_MEDIUM_TTL_') ? _REDIS_MEDIUM_TTL_ : _REDIS_DEFAULT_TTL_;
$this->long_ttl = defined('_REDIS_LONG_TTL_') ? _REDIS_LONG_TTL_ : _REDIS_DEFAULT_TTL_;
if ($this->auth !== '') {
$this->redis_client = new Predis\Client(['scheme' => 'tcp',
'host' => $this->host,
'port' => $this->port,
'password' => $this->auth,
],
['parameters' => [
'database' => (int)$this->db,
],
]);
} else {
$this->redis_client = new Predis\Client(['scheme' => 'tcp',
'host' => $this->host,
'port' => $this->port,
],
['parameters' => [
'database' => (int)$this->db,
],
]);
}
try {
$this->redis_client->connect();
$this->is_connected = $this->redis_client->isConnected();
} catch (Exception $e) {
$this->is_connected = false;
}
}
public static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new CacheRedis();
}
return self::$_instance;
}
public function get($key, $controller, $ignoreCheckIp = false)
{
if (!$this->isConnected()) {
return false;
}
if (!$ignoreCheckIp && $this->checkIpDisableCache()) {
return false;
}
if ($controller === '') {
$controller = self::DEFAULT_CONTROLLER_NAME;
}
$value = $this->redis_client->get($this->createKey($key, $controller));
if (null === $value) {
return false;
} else {
return $this->decode($value);
}
}
public function delete($key)
{
if (!$this->isConnected()) {
return false;
}
return $this->redis_client->del($key);
}
public function del($key) {
return $this->delete($key);
}
public function set($key, $controller, $value, $expire = null)
{
if (!$this->isConnected()) {
return false;
}
if ($controller === '') {
$controller = self::DEFAULT_CONTROLLER_NAME;
}
if (null === $expire) {
$expire = (int)$this->default_ttl;
} else {
switch ($expire) {
case self::SHORT_TTL:
$expire = (int)$this->short_ttl;
break;
case self::MEDIUM_TTL:
$expire = (int)$this->medium_ttl;
break;
case self::LONG_TTL:
$expire = (int)$this->long_ttl;
break;
default:
$expire = (int)$expire;
break;
}
}
$this->redis_client->setex($this->createKey($key, $controller), $expire, $this->encode($value));
}
public function expire($key, $controller, $expire)
{
if (!$this->isConnected()) {
return false;
}
if ($controller === '') {
$controller = self::DEFAULT_CONTROLLER_NAME;
}
$this->redis_client->expire($this->createKey($key, $controller), $expire);
}
public function isConnected()
{
return $this->is_connected;
}
public function flush()
{
if (!$this->isConnected()) {
return false;
}
$this->redis_client->flushall();
}
public function setQuery($query, $controller, $value, $expire = null)
{
if (!$this->isConnected()) {
return false;
}
if (null === $expire) {
$expire = (int)$this->default_ttl;
} else {
switch ($expire) {
case self::SHORT_TTL:
$expire = (int)$this->short_ttl;
break;
case self::MEDIUM_TTL:
$expire = (int)$this->medium_ttl;
break;
case self::LONG_TTL:
$expire = (int)$this->long_ttl;
break;
default:
$expire = (int)$expire;
break;
}
}
if ($controller === '') {
$controller = self::DEFAULT_CONTROLLER_NAME;
}
if (is_string($query) && trim($query) !== '') {
$this->redis_client->setex($this->createKey($query, $controller), $expire, $this->encode($value));
}
}
public function getQuery($query, $controller, $ignoreCheckIp = false)
{
if (!$this->isConnected()) {
return false;
}
if (!$ignoreCheckIp && $this->checkIpDisableCache()) {
return false;
}
if ($controller === '') {
$controller = self::DEFAULT_CONTROLLER_NAME;
}
$value = null;
if (is_string($query) && trim($query) !== '') {
$value = $this->redis_client->get($this->createKey($query, $controller));
}
if (is_null($value)) {
return false;
} else {
return $this->decode($value);
}
}
public function scan($key, $count = 1024)
{
$result = array();
foreach (new Iterator\Keyspace(
$this->redis_client,
$key,
$count
) as $result_key) {
$result[] = $result_key;
}
return $result;
}
public function clear($key)
{
$keys = $this->scan($key);
$replies = false;
foreach (array_chunk($keys, 256) as $data) {
$pipe = $this->redis_client->pipeline();
foreach ($data as $item) {
$pipe->del($item);
}
$replies = $pipe->execute();
}
return $replies;
}
private function encode($value)
{
return serialize($value);
}
private function decode($value)
{
return unserialize($value);
}
private function createKey($key, $controller)
{
return $controller . '-' . hash(self::HASHING_ALGORITHM, $key);
}
private function checkIpDisableCache()
{
$disableCache = false;
$no_cache_ip = Configuration::get('PS_REDIS_NOCACHE_IP');
if ($no_cache_ip) {
$no_cache_ip_list = explode(',', $no_cache_ip);
if (isset($_SERVER['REMOTE_ADDR']) && in_array($_SERVER['REMOTE_ADDR'], $no_cache_ip_list)) {
$disableCache = true;
}
}
return $disableCache;
}
}

View File

@ -0,0 +1,92 @@
<?php
class MySQL extends MySQLCore
{
const REDIS_CONTROLLER_KEY_MAX_LENGTH = 16;
const DEFAULT_SLICE_QUERY_STEP = 1000;
/**
* ExecuteS return the result of $query as array,
* or as mysqli_result if $array set to FALSE
*
* @param string $query query to execute
* @param boolean $array return an array instead of a mysql_result object
* @param bool|int $use_cache if query has been already executed, use its result
* @param string|bool $controller name used to limit cache collision
* @return array or result object
*/
public function ExecuteS($query, $array = TRUE, $use_cache = FALSE, $controller = FALSE, $cache_expire = null)
{
$this->_result = FALSE;
$this->_lastQuery = $query;
if ($controller) {
$controller = strtoupper(substr($controller, 0, self::REDIS_CONTROLLER_KEY_MAX_LENGTH));
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
if (FALSE !== ($result = CacheRedis::getInstance()->getQuery($query, $controller))) {
return $result;
}
}
if ($this->_link && $this->_result = mysql_query($query, $this->_link)) {
$this->_lastCached = FALSE;
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
if (!$array) {
return $this->_result;
}
$resultArray = array();
// Only SELECT queries and a few others return a valid resource usable with mysql_fetch_assoc
if ($this->_result !== TRUE) {
while ($row = mysql_fetch_assoc($this->_result)) {
$resultArray[] = $row;
}
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
CacheRedis::getInstance()->setQuery($query, $controller, $resultArray, $cache_expire);
}
return $resultArray;
}
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
return FALSE;
}
public function getRow($query, $use_cache = FALSE, $controller = FALSE, $cache_expire = null)
{
$query .= ' LIMIT 1';
$this->_result = FALSE;
$this->_lastQuery = $query;
if ($controller) {
$controller = strtoupper(substr($controller, 0, self::REDIS_CONTROLLER_KEY_MAX_LENGTH));
}
if ($use_cache && $controller && class_exists('CacheRedis')) {
if (FALSE !== ($result = CacheRedis::getInstance()->getQuery($query, $controller))) {
return $result;
}
}
if ($this->_link) {
if ($this->_result = mysql_query($query, $this->_link)) {
$this->_lastCached = FALSE;
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
$result = mysql_fetch_assoc($this->_result);
if ($use_cache && $controller && class_exists('CacheRedis')) {
CacheRedis::getInstance()->setQuery($query, $controller, $result, $cache_expire);
}
return $result;
}
}
if (_PS_DEBUG_SQL_) {
$this->displayMySQLError($query);
}
return FALSE;
}
}

View File

@ -92,9 +92,7 @@ class CategoryController extends CategoryControllerCore {
foreach($this->cat_products as $key => $p) {
$id_products[] = $p['id_product'];
}
$attributes = array();
$id_attributes = array();
foreach(Db::getInstance()->ExecuteS('
$query = '
SELECT pa.`id_product`, pa.`quantity`, ag.`public_name` as `group`, al.`name`, al.`id_attribute`
FROM `'._DB_PREFIX_.'product_attribute` pa, `'._DB_PREFIX_.'product_attribute_combination` ac, `'._DB_PREFIX_.'attribute` a, `'._DB_PREFIX_.'attribute_lang` al, `'._DB_PREFIX_.'attribute_group_lang` ag
WHERE pa.`id_product` IN ('.implode(', ', $id_products).')
@ -107,7 +105,16 @@ class CategoryController extends CategoryControllerCore {
AND pa.`quantity` > 0
AND ag.`id_attribute_group` IN(75, 9, 272, 172)
ORDER BY pa.`id_product` ASC, ag.`public_name` ASC, al.`name` ASC
') as $attr) {
';
if (false === ($result = CacheRedis::getInstance()->getQuery($query, "CategoryController"))) {
$result = Db::getInstance()->ExecuteS($query);
CacheRedis::getInstance()->setQuery($query, "CategoryController", $result);
}
$attributes = array();
$id_attributes = array();
foreach($result as $attr) {
if(!isset($attributes[$attr['id_product']])) {
$attributes[$attr['id_product']] = array();
}