Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
121 changes: 121 additions & 0 deletions src/CacheMem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* PDOx - Useful Query Builder & PDO Class
*
* @class CacheMem
* @author Şükrü Kansız
* @web <http://burakdemirtas.org>
* @url <https://github.com/izniburak/PDOx>
* @license The MIT License (MIT) - <http://opensource.org/licenses/MIT>
*/

namespace Buki;

use Memcached;

class CacheMem
{
protected $masterKey = null;
protected $cache = null;
protected $finish = null;

protected $memcached = null;

/**
* CacheMem constructor.
*
* @param null $config
*/
function __construct($config = [])
{
$this->masterKey = $config['masterkey'];
$this->memcached = new Memcached();
$this->memcached->addServer($config['host'], $config['port']);
}

/**
* @param int $time
*/
public function setCacheTime($time = 0)
{
$this->cache = $time;
$this->finish = time() + $time;
}

/**
* @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 $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
*
* @return string
*/
protected function keyName($name)
{
return $this->masterKey . '.PDOx.' . md5($this->masterKey . ':' . $name);
}

/**
* @return void
*/
public function __destruct()
{
if (!is_null($this->memcached)) {
$this->memcached->quit();
}
}
}
61 changes: 47 additions & 14 deletions src/Pdox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']) . ';'
Expand Down Expand Up @@ -1117,22 +1131,22 @@ 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) {
$this->error = $this->pdo->errorInfo()[2];
$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);
}
Expand All @@ -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));
}

/**
Expand All @@ -1160,11 +1172,32 @@ public function escape($data)
*/
public function cache($time)
{
$this->cache = new Cache($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
*/
Expand Down