Initial commit

This commit is contained in:
2019-11-20 07:44:43 +01:00
commit 5bf49c4a81
41188 changed files with 5459177 additions and 0 deletions

721
web/classes/cache/Cache.php vendored Normal file
View File

@@ -0,0 +1,721 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
abstract class CacheCore
{
/**
* Name of keys index.
*/
const KEYS_NAME = '__keys__';
/**
* Name of SQL cache index.
*/
const SQL_TABLES_NAME = 'tablesCached';
/**
* Store the number of time a query is fetched from the cache.
*
* @var array
*/
protected $queryCounter = array();
/**
* @var Cache
*/
protected static $instance;
/**
* Max number of queries cached in memcached, for each SQL table.
*
* @var int
*/
protected $maxCachedObjectsByTable = 10000;
/**
* If a cache set this variable to true, we need to adjust the size of the table cache object.
*
* @var bool
*/
protected $adjustTableCacheSize = false;
/**
* @var array List all keys of cached data and their associated ttl
*/
protected $keys = array();
/**
* @var array Store list of tables and their associated keys for SQL cache
*/
protected $sql_tables_cached = array();
/**
* @var array List of blacklisted tables for SQL cache, these tables won't be indexed
*/
protected $blacklist = array(
'cart',
'cart_cart_rule',
'cart_product',
'connections',
'connections_source',
'connections_page',
'customer',
'customer_group',
'customized_data',
'guest',
'pagenotfound',
'page_viewed',
'employee',
'log',
);
/**
* @var array Store local cache
*/
protected static $local = array();
/**
* Cache a data.
*
* @param string $key
* @param mixed $value
* @param int $ttl
*
* @return bool
*/
abstract protected function _set($key, $value, $ttl = 0);
/**
* Retrieve a cached data by key.
*
* @param string $key
*
* @return mixed
*/
abstract protected function _get($key);
/**
* Check if a data is cached by key.
*
* @param string $key
*
* @return bool
*/
abstract protected function _exists($key);
/**
* Delete a data from the cache by key.
*
* @param string $key
*
* @return bool
*/
abstract protected function _delete($key);
/**
* Delete multiple keys from the cache.
*
* @param array $keyArray
*/
protected function _deleteMulti(array $keyArray)
{
foreach ($keyArray as $key) {
$this->delete($key);
}
}
/**
* Write keys index.
*/
abstract protected function _writeKeys();
/**
* Clean all cached data.
*
* @return bool
*/
abstract public function flush();
/**
* @return int
*/
public function getMaxCachedObjectsByTable()
{
return $this->maxCachedObjectsByTable;
}
/**
* @param int $maxCachedObjectsByTable
*/
public function setMaxCachedObjectsByTable($maxCachedObjectsByTable)
{
$this->maxCachedObjectsByTable = $maxCachedObjectsByTable;
}
/**
* @return Cache
*/
public static function getInstance()
{
if (!self::$instance) {
$caching_system = _PS_CACHING_SYSTEM_;
self::$instance = new $caching_system();
}
return self::$instance;
}
/**
* Unit testing purpose only.
*
* @param $test_instance Cache
*/
public static function setInstanceForTesting($test_instance)
{
self::$instance = $test_instance;
}
/**
* If a cache set this variable to true, we need to adjust the size of the table cache object
* Useful when the cache is reported to be full (e.g. memcached::RES_E2BIG error message).
*
* @param bool $value
*/
protected function setAdjustTableCacheSize($value)
{
$this->adjustTableCacheSize = (bool) $value;
}
/**
* Unit testing purpose only.
*/
public static function deleteTestingInstance()
{
self::$instance = null;
}
/**
* Store a data in cache.
*
* @param string $key
* @param mixed $value
* @param int $ttl
*
* @return bool
*/
public function set($key, $value, $ttl = 0)
{
if ($this->_set($key, $value, $ttl)) {
if ($ttl < 0) {
$ttl = 0;
}
$this->keys[$key] = ($ttl == 0) ? 0 : time() + $ttl;
$this->_writeKeys();
return true;
}
return false;
}
/**
* Retrieve a data from cache.
*
* @param string $key
*
* @return mixed
*/
public function get($key)
{
if (!isset($this->keys[$key])) {
return false;
}
return $this->_get($key);
}
/**
* Check if a data is cached.
*
* @param string $key
*
* @return bool
*/
public function exists($key)
{
if (!isset($this->keys[$key])) {
return false;
}
return $this->_exists($key);
}
/**
* Delete several keys at once from the cache.
*
* @param array $keyArray
*/
public function deleteMulti(array $keyArray)
{
$this->_deleteMulti($keyArray);
}
/**
* Delete one or several data from cache (* joker can be used)
* E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');.
*
* @param string $key
*
* @return array List of deleted keys
*/
public function delete($key)
{
// Get list of keys to delete
$keys = array();
if ($key == '*') {
$keys = $this->keys;
} elseif (strpos($key, '*') === false) {
$keys = array($key);
} else {
$pattern = str_replace('\\*', '.*', preg_quote($key));
foreach ($this->keys as $k => $ttl) {
if (preg_match('#^' . $pattern . '$#', $k)) {
$keys[] = $k;
}
}
}
// Delete keys
foreach ($keys as $key) {
if (!isset($this->keys[$key])) {
continue;
}
if ($this->_delete($key)) {
unset($this->keys[$key]);
}
}
$this->_writeKeys();
return $keys;
}
/**
* Increment the query counter for the given query.
*
* @param string $query
*/
public function incrementQueryCounter($query)
{
if (isset($this->queryCounter[$query])) {
++$this->queryCounter[$query];
} else {
$this->queryCounter[$query] = 1;
}
}
/**
* Store a query in cache.
*
* @param string $query
* @param array $result
*/
public function setQuery($query, $result)
{
if ($this->isBlacklist($query)) {
return;
}
if (empty($result) || $result === false) {
$result = array();
}
// use the query counter to update the cache statistics
$this->updateQueryCacheStatistics();
$key = $this->updateTableToQueryMap($query);
// Store query results in cache
// no need to check the key existence before the set : if the query is already
// in the cache, setQuery is not invoked
$this->set($key, $result);
}
/**
* Return the hash associated with a query, used to store data into the cache.
*
* @param string $query
*
* @return string
*/
public function getQueryHash($query)
{
return Tools::hashIV($query);
}
/**
* Return the hash associated with a table name, used to store the "table to query hash" map.
*
* @param string $table
*
* @return string
*/
public function getTableMapCacheKey($table)
{
return Tools::hashIV(self::SQL_TABLES_NAME . '_' . $table);
}
/**
* This function extract all the tables involded in a query, and in the each table map the query hash.
*
* @param string $query
*
* @return string
*/
private function updateTableToQueryMap($query)
{
$key = $this->getQueryHash($query);
// Get all table from the query and save them in cache
if ($tables = $this->getTables($query)) {
foreach ($tables as $table) {
$this->addQueryKeyToTableMap($key, $table, $tables);
}
}
return $key;
}
/**
* Add the given query hash to the table to query key map.
*
* @param string $key query hash
* @param string $table table name
* @param array $tables the tables associated with the query
*/
private function addQueryKeyToTableMap($key, $table, $tables)
{
// the name of the cache entry which cache the table map
$cacheKey = $this->getTableMapCacheKey($table);
$this->initializeTableCache($table);
if (!isset($this->sql_tables_cached[$table][$key])) {
if ((count($this->sql_tables_cached[$table]) + 1) > $this->maxCachedObjectsByTable) {
$this->adjustTableCacheSize($table);
}
$otherTables = $tables;
unset($otherTables[array_search($table, $tables)]);
$this->sql_tables_cached[$table][$key] = array(
'count' => 1,
'otherTables' => $otherTables,
);
$this->set($cacheKey, $this->sql_tables_cached[$table]);
// if the set fails because the object is too big, the adjustTableCacheSize flag is set
if ($this->adjustTableCacheSize) {
$this->adjustTableCacheSize($table, $key);
$this->set($cacheKey, $this->sql_tables_cached[$table]);
}
}
}
/**
* Use the query counter to update the query cache statistics
* So far its only called during a set operation to avoid overloading / slowing down the cache server.
*/
protected function updateQueryCacheStatistics()
{
$changedTables = array();
foreach ($this->queryCounter as $query => $count) {
$key = $this->getQueryHash($query);
if ($tables = $this->getTables($query)) {
foreach ($tables as $table) {
$this->initializeTableCache($table);
if (isset($this->sql_tables_cached[$table][$key])) {
$this->sql_tables_cached[$table][$key]['count'] += $count;
$changedTables[$table] = true;
}
}
}
}
foreach (array_keys($changedTables) as $table) {
$this->set($this->getTableMapCacheKey($table), $this->sql_tables_cached[$table]);
}
$this->queryCounter = array();
}
/**
* Remove the first less used query results from the cache.
*
* @param string $table
* @param string $keyToKeep the keep we want to keep inside the table cache
*/
protected function adjustTableCacheSize($table, $keyToKeep = null)
{
$invalidKeys = array();
if (isset($this->sql_tables_cached[$table])) {
if ($keyToKeep && isset($this->sql_tables_cached[$table][$keyToKeep])) {
// remove the key we plan to keep before adjusting the table cache size
unset($this->sql_tables_cached[$table][$keyToKeep]);
}
// sort the array with the query with the lowest count first
uasort($this->sql_tables_cached[$table], function ($a, $b) {
if ($a['count'] == $b['count']) {
return 0;
}
return ($a['count'] < $b['count']) ? -1 : 1;
});
// reduce the size of the cache : delete the first entries (those with the lowest count)
$tableBuffer = array_slice(
$this->sql_tables_cached[$table],
0,
ceil($this->maxCachedObjectsByTable / 3),
true
);
foreach (array_keys($tableBuffer) as $fs_key) {
$invalidKeys[] = $fs_key;
$invalidKeys[] = $fs_key . '_nrows';
unset($this->sql_tables_cached[$table][$fs_key]);
}
$this->_deleteMulti($invalidKeys);
if ($keyToKeep) {
$this->sql_tables_cached[$table][$keyToKeep] = 1;
}
}
$this->adjustTableCacheSize = false;
}
/**
* Get the tables used in a SQL query.
*
* @param string $string
*
* @return array|bool
*/
public function getTables($string)
{
if (preg_match_all('/(?:from|join|update|into)\s+`?(' . _DB_PREFIX_ .
'[0-9a-z_-]+)(?:`?\s{0,},\s{0,}`?(' . _DB_PREFIX_ .
'[0-9a-z_-]+)`?)?(?:`|\s+|\Z)(?!\s*,)/Umsi', $string, $res)) {
foreach ($res[2] as $table) {
if ($table != '') {
$res[1][] = $table;
}
}
return array_unique($res[1]);
} else {
return false;
}
}
/**
* Delete a query from cache.
*
* @param string $query
*/
public function deleteQuery($query)
{
if ($this->isBlacklist($query)) {
return;
}
$invalidKeys = array();
$tableKeysToUpdate = array();
if ($tables = $this->getTables($query)) {
foreach ($tables as $table) {
$cacheKey = $this->initializeTableCache($table);
if (!empty($this->sql_tables_cached[$table])) {
foreach ($this->sql_tables_cached[$table] as $fs_key => $tableMapInfos) {
$invalidKeys[] = $fs_key;
$invalidKeys[] = $fs_key . '_nrows';
foreach ($tableMapInfos['otherTables'] as $otherTable) {
if ($this->removeEntryInTableMapCache($fs_key, $otherTable)) {
$tableKeysToUpdate[$otherTable] = 1;
}
}
}
unset($this->sql_tables_cached[$table]);
$this->deleteMulti($invalidKeys);
$this->delete($cacheKey);
}
}
$this->flushUpdatedTableKeyEntries($tableKeysToUpdate);
}
}
/**
* Flush into the cache the updated entries from the sql_tables_caches.
*
* @param array $tableKeysToUpdate
*/
private function flushUpdatedTableKeyEntries($tableKeysToUpdate)
{
foreach (array_keys($tableKeysToUpdate) as $tableKeyToUpdate) {
$cacheKey = $this->getTableMapCacheKey($tableKeyToUpdate);
if (empty($this->sql_tables_cached[$tableKeyToUpdate])) {
$this->delete($cacheKey);
} else {
$this->set($cacheKey, $this->sql_tables_cached[$tableKeyToUpdate]);
}
}
}
/**
* Initialize the table cache entry associated with $table.
*
* @param string $table
*
* @return string
*/
private function initializeTableCache($table)
{
$cacheKey = $this->getTableMapCacheKey($table);
if (!array_key_exists($table, $this->sql_tables_cached)) {
$this->sql_tables_cached[$table] = $this->get($cacheKey);
if (!is_array($this->sql_tables_cached[$table])) {
$this->sql_tables_cached[$table] = array();
}
}
return $cacheKey;
}
/**
* Remove $key from the tableMap.
*
* @param string $key
* @param string $table
*
* @return bool True is the key exists in the table
*/
private function removeEntryInTableMapCache($key, $table)
{
if (isset($this->sql_tables_cached[$table][$key])) {
unset($this->sql_tables_cached[$table][$key]);
return true;
}
return false;
}
/**
* Check if a query contain blacklisted tables.
*
* @param string $query
*
* @return bool
*/
protected function isBlacklist($query)
{
foreach ($this->blacklist as $find) {
if (false !== strpos($query, _DB_PREFIX_ . $find)) {
return true;
}
}
return false;
}
/**
* @param string $key
* @param string $value
*/
public static function store($key, $value)
{
// PHP is not efficient at storing array
// Better delete the whole cache if there are
// more than 1000 elements in the array
if (count(Cache::$local) > 1000) {
Cache::$local = array();
}
Cache::$local[$key] = $value;
}
public static function clear()
{
Cache::$local = array();
}
/**
* @param string $key
*
* @return mixed
*/
public static function retrieve($key)
{
return isset(Cache::$local[$key]) ? Cache::$local[$key] : null;
}
/**
* @return array
*/
public static function retrieveAll()
{
return Cache::$local;
}
/**
* @param string $key
*
* @return bool
*/
public static function isStored($key)
{
return isset(Cache::$local[$key]);
}
/**
* @param string $key
*/
public static function clean($key)
{
if (strpos($key, '*') !== false) {
$regexp = str_replace('\\*', '.*', preg_quote($key, '#'));
foreach (array_keys(Cache::$local) as $key) {
if (preg_match('#^' . $regexp . '$#', $key)) {
unset(Cache::$local[$key]);
}
}
} else {
unset(Cache::$local[$key]);
}
}
}

176
web/classes/cache/CacheApc.php vendored Normal file
View File

@@ -0,0 +1,176 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* This class requires the PECL APC extension or PECL APCu extension to be installed.
*
* @since 1.5.0
*/
class CacheApcCore extends Cache
{
/** @var bool Whether APCu is enabled */
public $apcu;
/**
* CacheApcCore constructor.
*/
public function __construct()
{
if (!extension_loaded('apc') && !extension_loaded('apcu')) {
throw new PrestaShopException('APC cache has been enabled, but the APC or APCu extension is not available');
}
$this->apcu = extension_loaded('apcu');
}
/**
* 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 Cache key
*
* @return bool Whether the key was deleted
*/
public function delete($key)
{
if ($key == '*') {
$this->flush();
} elseif (strpos($key, '*') === false) {
$this->_delete($key);
} else {
$pattern = str_replace('\\*', '.*', preg_quote($key));
$cache_info = (($this->apcu) ? apcu_cache_info('') : apc_cache_info(''));
foreach ($cache_info['cache_list'] as $entry) {
if (isset($entry['key'])) {
$key = $entry['key'];
} else {
$key = $entry['info'];
}
if (preg_match('#^' . $pattern . '$#', $key)) {
$this->_delete($key);
}
}
}
return true;
}
/**
* @see Cache::_set()
*/
protected function _set($key, $value, $ttl = 0)
{
$result = (($this->apcu) ? apcu_store($key, $value, $ttl) : apc_store($key, $value, $ttl));
if ($result === false) {
$this->setAdjustTableCacheSize(true);
}
return $result;
}
/**
* @see Cache::_get()
*/
protected function _get($key)
{
return ($this->apcu) ? apcu_fetch($key) : apc_fetch($key);
}
/**
* @see Cache::_exists()
*/
protected function _exists($key)
{
if (!function_exists('apc_exists') && !function_exists('apcu_exists')) {
// We're dealing with APC < 3.1.4; use this boolean wrapper as a fallback:
return (bool) apc_fetch($key);
} else {
return ($this->apcu) ? apcu_exists($key) : apc_exists($key);
}
}
/**
* @see Cache::_delete()
*/
protected function _delete($key)
{
return ($this->apcu) ? apcu_delete($key) : apc_delete($key);
}
/**
* @see Cache::_writeKeys()
*/
protected function _writeKeys()
{
}
/**
* @see Cache::flush()
*/
public function flush()
{
return ($this->apcu) ? apcu_clear_cache() : apc_clear_cache();
}
/**
* Store data in the cache.
*
* @param string $key Cache Key
* @param mixed $value Value
* @param int $ttl Time to live in the cache
* 0 = unlimited
*
* @return bool Whether the data was successfully stored
*/
public function set($key, $value, $ttl = 0)
{
return $this->_set($key, $value, $ttl);
}
/**
* Retrieve data from the cache.
*
* @param string $key Cache key
*
* @return mixed Data
*/
public function get($key)
{
return $this->_get($key);
}
/**
* Check if data has been cached.
*
* @param string $key Cache key
*
* @return bool Whether the data has been cached
*/
public function exists($key)
{
return $this->_exists($key);
}
}

286
web/classes/cache/CacheMemcache.php vendored Normal file
View File

@@ -0,0 +1,286 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* This class require PECL Memcache extension.
*/
class CacheMemcacheCore extends Cache
{
/**
* @var Memcache
*/
protected $memcache;
/**
* @var bool Connection status
*/
protected $is_connected = false;
public function __construct()
{
$this->connect();
}
public function __destruct()
{
$this->close();
}
/**
* Connect to memcache server.
*/
public function connect()
{
if (class_exists('Memcache') && extension_loaded('memcache')) {
$this->memcache = new Memcache();
} else {
return;
}
$servers = self::getMemcachedServers();
if (!$servers) {
return;
}
foreach ($servers as $server) {
$this->memcache->addServer($server['ip'], $server['port'], true, (int) $server['weight']);
}
$this->is_connected = true;
}
/**
* @see Cache::_set()
*/
protected function _set($key, $value, $ttl = 0)
{
if (!$this->is_connected) {
return false;
}
$result = $this->memcache->set($key, $value, 0, $ttl);
if ($result === false) {
$this->setAdjustTableCacheSize(true);
}
return $result;
}
/**
* @see Cache::_get()
*/
protected function _get($key)
{
if (!$this->is_connected) {
return false;
}
return $this->memcache->get($key);
}
/**
* @see Cache::_exists()
*/
protected function _exists($key)
{
if (!$this->is_connected) {
return false;
}
return $this->memcache->get($key) !== false;
}
/**
* @see Cache::_delete()
*/
protected function _delete($key)
{
if (!$this->is_connected) {
return false;
}
return $this->memcache->delete($key);
}
/**
* @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->memcache->flush();
}
/**
* 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 {
// Get keys (this code comes from Doctrine 2 project)
$pattern = str_replace('\\*', '.*', preg_quote($key));
$servers = $this->getMemcachedServers();
if (is_array($servers) && count($servers) > 0 && method_exists('Memcache', 'getStats')) {
$all_slabs = $this->memcache->getStats('slabs');
}
if (isset($all_slabs) && is_array($all_slabs)) {
foreach ($all_slabs as $server => $slabs) {
if (is_array($slabs)) {
foreach (array_keys($slabs) as $i => $slab_id) {
// $slab_id is not an int but a string, using the key instead ?
if (is_int($i)) {
$dump = $this->memcache->getStats('cachedump', (int) $i);
if ($dump) {
foreach ($dump as $entries) {
if ($entries) {
foreach ($entries as $key => $data) {
if (preg_match('#^' . $pattern . '$#', $key)) {
$this->_delete($key);
}
}
}
}
}
}
}
}
}
}
}
return true;
}
/**
* Close connection to memcache server.
*
* @return bool
*/
protected function close()
{
if (!$this->is_connected) {
return false;
}
return $this->memcache->close();
}
/**
* Add a memcache server.
*
* @param string $ip
* @param int $port
* @param int $weight
*/
public static function addServer($ip, $port, $weight)
{
return Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'memcached_servers (ip, port, weight) VALUES(\'' . pSQL($ip) . '\', ' . (int) $port . ', ' . (int) $weight . ')', false);
}
/**
* Get list of memcached servers.
*
* @return array
*/
public static function getMemcachedServers()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'memcached_servers', true, false);
}
/**
* Delete a memcache server.
*
* @param int $id_server
*/
public static function deleteServer($id_server)
{
return Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'memcached_servers WHERE id_memcached_server=' . (int) $id_server);
}
}

277
web/classes/cache/CacheMemcached.php vendored Normal file
View File

@@ -0,0 +1,277 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* This class require PECL Memcached extension.
*/
class CacheMemcachedCore extends Cache
{
/**
* @var Memcached
*/
protected $memcached;
/**
* @var bool Connection status
*/
protected $is_connected = false;
public function __construct()
{
$this->connect();
if ($this->isConnected()) {
$this->memcached->setOption(Memcached::OPT_PREFIX_KEY, _DB_PREFIX_);
if ($this->memcached->getOption(Memcached::HAVE_IGBINARY)) {
$this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
}
}
}
public function isConnected()
{
return $this->is_connected;
}
public function __destruct()
{
$this->close();
}
/**
* Connect to memcached server.
*/
public function connect()
{
if (class_exists('Memcached') && extension_loaded('memcached')) {
$this->memcached = new Memcached();
} else {
return;
}
$servers = self::getMemcachedServers();
if (!$servers) {
return;
}
foreach ($servers as $server) {
$this->memcached->addServer($server['ip'], $server['port'], (int) $server['weight']);
}
$this->is_connected = in_array('255.255.255', $this->memcached->getVersion(), true) === false;
}
/**
* {@inheritdoc}
*/
protected function _set($key, $value, $ttl = 0)
{
if (!$this->isConnected()) {
return false;
}
$result = $this->memcached->set($key, $value, $ttl);
if ($result === false) {
if ($this->memcached->getResultCode() === Memcached::RES_E2BIG) {
$this->setAdjustTableCacheSize(true);
}
}
return $result;
}
/**
* {@inheritdoc}
*/
protected function _get($key)
{
if (!$this->isConnected()) {
return false;
}
return $this->memcached->get($key);
}
/**
* {@inheritdoc}
*/
protected function _exists($key)
{
if (!$this->isConnected()) {
return false;
}
return $this->memcached->get($key) !== false;
}
/**
* {@inheritdoc}
*/
protected function _delete($key)
{
if (!$this->isConnected()) {
return false;
}
return $this->memcached->delete($key);
}
/**
* {@inheritdoc}
*/
protected function _deleteMulti(array $keyArray)
{
if (!$this->isConnected()) {
return false;
}
return $this->memcached->deleteMulti($keyArray);
}
/**
* @see Cache::_writeKeys()
*/
protected function _writeKeys()
{
if (!$this->isConnected()) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function flush()
{
if (!$this->isConnected()) {
return false;
}
return $this->memcached->flush();
}
/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = 0)
{
return $this->_set($key, $value, $ttl);
}
/**
* {@inheritdoc}
*/
public function get($key)
{
return $this->_get($key);
}
/**
* {@inheritdoc}
*/
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->memcached->getAllKeys();
foreach ($keys as $key => $data) {
if (preg_match('#^' . $pattern . '$#', $key)) {
$this->_delete($key);
}
}
}
return true;
}
/**
* Close connection to memcache server.
*
* @return bool
*/
protected function close()
{
if (!$this->isConnected()) {
return false;
}
return $this->memcached->quit();
}
/**
* Add a memcached server.
*
* @param string $ip
* @param int $port
* @param int $weight
*
* @return bool
*/
public static function addServer($ip, $port, $weight)
{
return Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'memcached_servers (ip, port, weight) VALUES(\'' . pSQL($ip) . '\', ' . (int) $port . ', ' . (int) $weight . ')', false);
}
/**
* Get list of memcached servers.
*
* @return array
*/
public static function getMemcachedServers()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'memcached_servers', true, false);
}
/**
* Delete a memcached server.
*
* @param int $id_server
*
* @return bool
*/
public static function deleteServer($id_server)
{
return Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'memcached_servers WHERE id_memcached_server=' . (int) $id_server);
}
}

97
web/classes/cache/CacheXcache.php vendored Normal file
View File

@@ -0,0 +1,97 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* This class require Xcache extension.
*
* @since 1.5.0
*/
class CacheXcacheCore extends Cache
{
public function __construct()
{
$this->keys = xcache_get(self::KEYS_NAME);
if (!is_array($this->keys)) {
$this->keys = array();
}
}
/**
* @see Cache::_set()
*/
protected function _set($key, $value, $ttl = 0)
{
$result = xcache_set($key, $value, $ttl);
if ($result === false) {
$this->setAdjustTableCacheSize(true);
}
return $result;
}
/**
* @see Cache::_get()
*/
protected function _get($key)
{
return xcache_isset($key) ? xcache_get($key) : false;
}
/**
* @see Cache::_exists()
*/
protected function _exists($key)
{
return xcache_isset($key);
}
/**
* @see Cache::_delete()
*/
protected function _delete($key)
{
return xcache_unset($key);
}
/**
* @see Cache::_writeKeys()
*/
protected function _writeKeys()
{
xcache_set(self::KEYS_NAME, $this->keys);
}
/**
* @see Cache::flush()
*/
public function flush()
{
$this->delete('*');
return true;
}
}

34
web/classes/cache/index.php vendored Normal file
View File

@@ -0,0 +1,34 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../../');
exit;