From e61e940d0e17433fba6871abe6c359e4962358da Mon Sep 17 00:00:00 2001 From: KaraKunT Date: Thu, 17 Sep 2020 04:29:19 +0300 Subject: [PATCH 1/2] Added Php Memcached --- src/CacheMem.php | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Pdox.php | 2 +- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/CacheMem.php diff --git a/src/CacheMem.php b/src/CacheMem.php new file mode 100644 index 0000000..f2c1965 --- /dev/null +++ b/src/CacheMem.php @@ -0,0 +1,89 @@ + + * @url + * @license The MIT License (MIT) - + */ + +namespace Buki; + +use Memcached; + +class CacheMem +{ + protected $cacheDir = null; + protected $cache = null; + protected $finish = null; + + protected $memcached = null; + + /** + * Cache constructor. + * + * @param null $dir + * @param int $time + */ + function __construct($dir = null, $time = 0) + { + $this->cacheDir = $dir; + $this->cache = $time; + $this->finish = time() + $time; + + $this->memcached = new Memcached; + $this->memcached->addServer('localhost', 11211); + } + + /** + * @param $sql + * @param bool $array + * + * @return bool|void + */ + public function getCache($sql, $array = false) + { + + if (is_null($this->cache)) { + return false; + } + + if (($cache = $this->memcached->get($this->keyName($sql)))) { + if($this->memcached->getResultCode() == Memcached::RES_SUCCESS){ + $cache = json_decode($cache, $array); + return ($array ? $cache['data'] : $cache->data); + } + } + + return false; + } + + /** + * @param $sql + * @param $result + * + * @return bool|void + */ + public function setCache($sql, $result) + { + if (is_null($this->cache)) { + return false; + } + + $this->memcached->set($this->keyName($sql), json_encode(['data' => $result, 'finish' => $this->finish]), $this->finish); + + return; + } + + /** + * @param $name + * + * @return string + */ + protected function keyName($name) + { + return md5($this->cacheDir.':'.$name); + } +} diff --git a/src/Pdox.php b/src/Pdox.php index 762461c..5faf877 100644 --- a/src/Pdox.php +++ b/src/Pdox.php @@ -1160,7 +1160,7 @@ public function escape($data) */ public function cache($time) { - $this->cache = new Cache($this->cacheDir, $time); + $this->cache = new CacheMem($this->cacheDir, $time); return $this; } From 6a1478e6082a6fe7183c724db436f5667e2a890d Mon Sep 17 00:00:00 2001 From: KaraKunT Date: Thu, 17 Sep 2020 22:05:58 +0300 Subject: [PATCH 2/2] Memcached config settings and clearCache function added. --- DOCS.md | 35 ++++++++++++++++++++++--- src/CacheMem.php | 68 +++++++++++++++++++++++++++++++++++------------- src/Pdox.php | 61 +++++++++++++++++++++++++++++++++---------- 3 files changed, 129 insertions(+), 35 deletions(-) diff --git a/DOCS.md b/DOCS.md index ed0bae2..27d063b 100644 --- a/DOCS.md +++ b/DOCS.md @@ -82,9 +82,32 @@ $config = [ # default value: null 'prefix' => '', - # Cache Directory of the Sql Result (optional) - # default value: __DIR__ . '/cache/' - 'cachedir' => __DIR__ . '/cache/sql/' + # Cache Type: File + 'cache' => [ + 'type' => 'file', + + # Cache Directory of the Sql Result (optional) + # default value: __DIR__ . '/cache/' + 'dir' => __DIR__ . '/cache/sql/' + ], + + # Cache Type: Memcached + 'cache' => [ + 'type' => 'memcached', + + # Memcached server address + # default value: localhost + 'host' => 'localhost', + + # Memcached server address port + # default value: 11211 + 'port' => 11211, + + # The key under which to store the value. + # available characters: = 0-9 a-z A-Z _ - . + # default value: masterkey + 'masterkey' => 'database.name_key' + ], ]; $db = new \Buki\Pdox($config); @@ -549,6 +572,12 @@ $db->error(); # Usage: ...->cache($time)->... $db->table('pages')->where('slug', 'example-page')->cache(60)->get(); # cache time: 60 seconds + +# Memcached clear +print_r($db->clearCache()); + +# Cleaning using the masterkey +print_r($db->clearCache('database.name_key')); ``` ### queryCount diff --git a/src/CacheMem.php b/src/CacheMem.php index f2c1965..4da2ec3 100644 --- a/src/CacheMem.php +++ b/src/CacheMem.php @@ -1,11 +1,12 @@ - * @url + * @author Şükrü Kansız + * @web + * @url * @license The MIT License (MIT) - */ @@ -15,26 +16,31 @@ class CacheMem { - protected $cacheDir = null; + protected $masterKey = null; protected $cache = null; protected $finish = null; protected $memcached = null; /** - * Cache constructor. + * CacheMem constructor. * - * @param null $dir + * @param null $config + */ + function __construct($config = []) + { + $this->masterKey = $config['masterkey']; + $this->memcached = new Memcached(); + $this->memcached->addServer($config['host'], $config['port']); + } + + /** * @param int $time */ - function __construct($dir = null, $time = 0) + public function setCacheTime($time = 0) { - $this->cacheDir = $dir; $this->cache = $time; $this->finish = time() + $time; - - $this->memcached = new Memcached; - $this->memcached->addServer('localhost', 11211); } /** @@ -45,17 +51,17 @@ function __construct($dir = null, $time = 0) */ public function getCache($sql, $array = false) { - + if (is_null($this->cache)) { return false; } if (($cache = $this->memcached->get($this->keyName($sql)))) { - if($this->memcached->getResultCode() == Memcached::RES_SUCCESS){ + if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) { $cache = json_decode($cache, $array); return ($array ? $cache['data'] : $cache->data); } - } + } return false; } @@ -71,12 +77,28 @@ public function setCache($sql, $result) if (is_null($this->cache)) { return false; } - $this->memcached->set($this->keyName($sql), json_encode(['data' => $result, 'finish' => $this->finish]), $this->finish); - return; } + /** + * @param $masterKey + * + * @return array + */ + public function clearCache($masterKey) + { + $data = null; + $keys = $this->memcached->getAllKeys(); + foreach ($keys as $item) { + if (preg_match('/' . $masterKey . '.PDOx.*/', $item)) { + $this->memcached->delete($item); + $data[] = $item; + } + } + return is_array($data) ? $data : null; + } + /** * @param $name * @@ -84,6 +106,16 @@ public function setCache($sql, $result) */ protected function keyName($name) { - return md5($this->cacheDir.':'.$name); + return $this->masterKey . '.PDOx.' . md5($this->masterKey . ':' . $name); + } + + /** + * @return void + */ + public function __destruct() + { + if (!is_null($this->memcached)) { + $this->memcached->quit(); + } } } diff --git a/src/Pdox.php b/src/Pdox.php index 5faf877..8f79522 100644 --- a/src/Pdox.php +++ b/src/Pdox.php @@ -56,12 +56,17 @@ class Pdox implements PdoxInterface /** * @var Cache|null */ - protected $cache = null; + protected $cacheType = null; + + /** + * @var array Cache Config + */ + protected $cacheConfig = []; /** - * @var string|null Cache Directory + * @var Cache|null */ - protected $cacheDir = null; + protected $cache = null; /** * @var int Total query count @@ -91,11 +96,20 @@ public function __construct(array $config) $config['collation'] = isset($config['collation']) ? $config['collation'] : 'utf8_general_ci'; $config['port'] = isset($config['port']) ? $config['port'] - : strstr($config['host'], ':') ? explode(':', $config['host'])[1] : ''; + : (strstr($config['host'], ':') ? explode(':', $config['host'])[1] : ''); + $this->prefix = isset($config['prefix']) ? $config['prefix'] : ''; - $this->cacheDir = isset($config['cachedir']) ? $config['cachedir'] : __DIR__ . '/cache/'; $this->debug = isset($config['debug']) ? $config['debug'] : true; + if ((isset($config['cache']) && (is_array($config['cache'])))) { + $this->cacheConfig = $config['cache']; + $this->cacheType = isset($this->cacheConfig['type']) ? $this->cacheConfig['type'] : ''; + if ($this->cacheType === 'memcached') { + $this->cacheConfig['host'] = isset($this->cacheConfig['host']) ? $this->cacheConfig['host'] : 'localhost'; + $this->cacheConfig['port'] = isset($this->cacheConfig['port']) ? $this->cacheConfig['port'] : 11211; + $this->cacheConfig['masterkey'] = isset($this->cacheConfig['masterkey']) ? $this->cacheConfig['masterkey'] : 'masterkey'; + } + } $dsn = ''; if (in_array($config['driver'], ['', 'mysql', 'pgsql'])) { $dsn = $config['driver'] . ':host=' . str_replace(':' . $config['port'], '', $config['host']) . ';' @@ -1117,14 +1131,14 @@ public function query($query, $all = true, $type = null, $argument = null) if (!is_null($this->cache) && $type !== PDO::FETCH_CLASS) { $this->cache->setCache($this->query, $this->result); } - $this->cache = null; + //$this->cache = null; } else { - $this->cache = null; + //$this->cache = null; $this->error = $this->pdo->errorInfo()[2]; $this->error(); } } elseif ((!$cache && !$str) || ($cache && !$str)) { - $this->cache = null; + //$this->cache = null; $this->result = $this->pdo->exec($this->query); if ($this->result === false) { @@ -1132,7 +1146,7 @@ public function query($query, $all = true, $type = null, $argument = null) $this->error(); } } else { - $this->cache = null; + //$this->cache = null; $this->result = $cache; $this->numRows = is_array($this->result) ? count($this->result) : ($this->result === '' ? 0 : 1); } @@ -1148,9 +1162,7 @@ public function query($query, $all = true, $type = null, $argument = null) */ public function escape($data) { - return $data === null ? 'NULL' : ( - is_int($data) || is_float($data) ? $data : $this->pdo->quote($data) - ); + return $data === null ? 'NULL' : (is_int($data) || is_float($data) ? $data : $this->pdo->quote($data)); } /** @@ -1160,11 +1172,32 @@ public function escape($data) */ public function cache($time) { - $this->cache = new CacheMem($this->cacheDir, $time); - + if ($this->cacheType === 'file') { + $this->cache = new Cache($this->cacheConfig['dir'], $time); + } else { + if (is_null($this->cache)) { + $this->cache = new CacheMem($this->cacheConfig); + } + $this->cache->setCacheTime($time); + } return $this; } + /** + * @param $masterKey + * + * @return string + */ + public function clearCache($masterKey = null) + { + if ($this->cacheType === 'memcached') { + if (is_null($this->cache)) { + $this->cache = new CacheMem($this->cacheConfig); + } + return $this->cache->clearCache(isset($masterKey) ? $masterKey : $this->cacheConfig['masterkey']); + } + } + /** * @return int */