new pisilinux web sites
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php namespace ZN\Cache;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
/**
|
||||
* Default Configuration
|
||||
*
|
||||
* Enabled when the configuration file can not be accessed.
|
||||
*/
|
||||
class CacheDefaultConfiguration
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Cache
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Includes configurations for the Cache library.
|
||||
|
|
||||
| drivers : apc, memcache, wincache, file, redis
|
||||
| driverSettings: Configurations by driver
|
||||
|
|
||||
*/
|
||||
|
||||
public $driver = 'file';
|
||||
public $driverSettings =
|
||||
[
|
||||
'memcache' =>
|
||||
[
|
||||
'host' => '127.0.0.1',
|
||||
'port' => '11211',
|
||||
'weight' => '1',
|
||||
],
|
||||
'redis' =>
|
||||
[
|
||||
'password' => NULL,
|
||||
'socketType' => 'tcp',
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 6379,
|
||||
'timeout' => 0
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php namespace ZN\Cache;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Config;
|
||||
|
||||
abstract class DriverMappingAbstract
|
||||
{
|
||||
/**
|
||||
* Keeps cache config
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Magic Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = Config::default('ZN\Cache\CacheDefaultConfiguration')
|
||||
::get('Storage', 'cache');
|
||||
}
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function select($key, $compressed);
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $time
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function insert($key, $var, $time, $compressed);
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function delete($key);
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function increment($key, $increment);
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function decrement($key, $decrement);
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function clean();
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function info($type);
|
||||
|
||||
/**
|
||||
* Close
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php namespace ZN\Cache\Drivers;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Support;
|
||||
use ZN\Cache\DriverMappingAbstract;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class ApcDriver extends DriverMappingAbstract
|
||||
{
|
||||
/**
|
||||
* Keeps apc type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $apc = 'apcu';
|
||||
|
||||
/**
|
||||
* Magic constructor
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
Support::extension('Apcu', 'Apcu');
|
||||
}
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function select($key, $compressed = NULL)
|
||||
{
|
||||
return $this->apc('fetch', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $time
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert($key, $var, $time, $compressed)
|
||||
{
|
||||
return $this->apc('store', $key, $var, $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return $this->apc('delete', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function increment($key, $increment)
|
||||
{
|
||||
return $this->apc('inc', $key, $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function decrement($key, $decrement)
|
||||
{
|
||||
return $this->apc('dec', $key, $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->apc('clear_cache');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info($type = NULL)
|
||||
{
|
||||
return $this->apc('cache_info', $type) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Apc type
|
||||
*
|
||||
* @param string $type
|
||||
* @param mixed ...$args
|
||||
*/
|
||||
protected function apc(string $type, ...$args)
|
||||
{
|
||||
$method = $this->apc . '_' . $type;
|
||||
|
||||
return $method(...$args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
<?php namespace ZN\Cache\Drivers;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Support;
|
||||
use ZN\Singleton;
|
||||
use ZN\Filesystem;
|
||||
use ZN\Cache\DriverMappingAbstract;
|
||||
|
||||
class FileDriver extends DriverMappingAbstract
|
||||
{
|
||||
/**
|
||||
* Keeps path
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $path = STORAGE_DIR . 'Cache/';
|
||||
|
||||
/**
|
||||
* Keeps compression class
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $compress;
|
||||
|
||||
|
||||
/**
|
||||
* Magic constructor
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->createCacheDirectoryIfNotExists();
|
||||
|
||||
Support::writable($this->path);
|
||||
|
||||
$this->compress = Singleton::class('ZN\Compression\Force');
|
||||
}
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function select($key, $compressed)
|
||||
{
|
||||
$data = $this->selectCacheKey($key);
|
||||
|
||||
if( ! empty($data['data']) )
|
||||
{
|
||||
if( $compressed !== false )
|
||||
{
|
||||
$data['data'] = $this->compress->driver($compressed)->undo($data['data']);
|
||||
}
|
||||
|
||||
return $data['data'];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $time
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert($key, $var, $time, $compressed = false)
|
||||
{
|
||||
if( $compressed !== false )
|
||||
{
|
||||
$var = $this->compress->driver($compressed)->do($var);
|
||||
}
|
||||
|
||||
$datas =
|
||||
[
|
||||
'time' => time(),
|
||||
'ttl' => $time,
|
||||
'data' => $var
|
||||
];
|
||||
|
||||
if( file_put_contents($pathKey = $this->path . $key, serialize($datas)) )
|
||||
{
|
||||
chmod($pathKey, 0640);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
if( is_file($path = $this->path . $key) )
|
||||
{
|
||||
return unlink($path);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function increment($key, $increment)
|
||||
{
|
||||
$data = $this->selectCacheKey($key);
|
||||
|
||||
if( $data === false )
|
||||
{
|
||||
$data = ['data' => 0, 'ttl' => 60];
|
||||
}
|
||||
elseif( ! is_numeric($data['data']) )
|
||||
{
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$newValue = $data['data'] + $increment;
|
||||
|
||||
return ( $this->insert($key, $newValue, $data['ttl']) )
|
||||
? $newValue
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function decrement($key, $decrement)
|
||||
{
|
||||
$data = $this->selectCacheKey($key);
|
||||
|
||||
if( $data === false )
|
||||
{
|
||||
$data = ['data' => 0, 'ttl' => 60];
|
||||
}
|
||||
elseif( ! is_numeric($data['data']) )
|
||||
{
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$newValue = $data['data'] - $decrement;
|
||||
|
||||
return $this->insert($key, $newValue, $data['ttl'])
|
||||
? $newValue
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
foreach( Filesystem\Folder::files($this->path, NULL, true) as $file )
|
||||
{
|
||||
if( is_file($file) )
|
||||
{
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info($type = NULL)
|
||||
{
|
||||
$info = Filesystem\Info::fileInfo($this->path);
|
||||
|
||||
if( $type === NULL )
|
||||
{
|
||||
return $info;
|
||||
}
|
||||
elseif( ! empty($info[$type]) )
|
||||
{
|
||||
return $info[$type];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected create cache directory if not exists
|
||||
*/
|
||||
protected function createCacheDirectoryIfNotExists()
|
||||
{
|
||||
if( ! is_dir($this->path) )
|
||||
{
|
||||
mkdir($this->path, 0755); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* protected select key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function selectCacheKey($key)
|
||||
{
|
||||
if( ! file_exists($pathKey = $this->path . $key) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = unserialize(file_get_contents($pathKey));
|
||||
|
||||
if( $data['ttl'] > 0 && time() > $data['time'] + $data['ttl'] )
|
||||
{
|
||||
unlink($pathKey); // @codeCoverageIgnore
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php namespace ZN\Cache\Drivers;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use Memcached;
|
||||
use ZN\Support;
|
||||
use ZN\ErrorHandling\Errors;
|
||||
use ZN\Cache\DriverMappingAbstract;
|
||||
use ZN\Cache\Exception\ConnectionRefusedException;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class MemcacheDriver extends DriverMappingAbstract
|
||||
{
|
||||
/**
|
||||
* Keeps memcahce class
|
||||
*
|
||||
* @param object
|
||||
*/
|
||||
protected $memcache;
|
||||
|
||||
/**
|
||||
* Magic constructor
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(?array $settings = NULL)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
Support::library('Memcached', 'Memcached');
|
||||
|
||||
$this->memcache = new Memcached;
|
||||
|
||||
$config = $this->config['driverSettings'];
|
||||
|
||||
$config = ! empty($settings)
|
||||
? $settings
|
||||
: $config['memcache'];
|
||||
|
||||
if( ! $this->memcache->addServer($config['host'], $config['port'], $config['weight']) )
|
||||
{
|
||||
throw new ConnectionRefusedException(NULL, 'Memcached connection error!'); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function select($key, $compressed = NULL)
|
||||
{
|
||||
return $this->memcache->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $time
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert($key, $var, $time, $compressed)
|
||||
{
|
||||
return $this->memcache->set($key, $var, $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return $this->memcache->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function increment($key, $increment)
|
||||
{
|
||||
return $this->memcache->increment($key, $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function decrement($key, $decrement)
|
||||
{
|
||||
return $this->memcache->decrement($key, $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->memcache->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info($type = NULL)
|
||||
{
|
||||
return $this->memcache->getStats() ?: [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php namespace ZN\Cache\Drivers;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use Redis;
|
||||
use RedisException;
|
||||
use ZN\Support;
|
||||
use ZN\ErrorHandling\Errors;
|
||||
use ZN\Cache\Exception\ConnectionRefusedException;
|
||||
use ZN\Cache\Exception\AuthenticationFailedException;
|
||||
use ZN\Cache\DriverMappingAbstract;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class RedisDriver extends DriverMappingAbstract
|
||||
{
|
||||
/**
|
||||
* Keeps redis class
|
||||
*
|
||||
* @var Redis
|
||||
*/
|
||||
protected $redis;
|
||||
|
||||
/**
|
||||
* Magic constructor
|
||||
*
|
||||
* @param array $settings = NULL
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(?array $settings = NULL)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
Support::extension('redis');
|
||||
|
||||
$config = $settings ?: $this->config['driverSettings']['redis'];
|
||||
|
||||
$this->redis = new Redis();
|
||||
|
||||
try
|
||||
{
|
||||
$success = $this->redis->connect($config['host'], $config['port'], $config['timeout']);
|
||||
|
||||
if ( empty($success) )
|
||||
{
|
||||
throw new ConnectionRefusedException(NULL, 'Connection');
|
||||
}
|
||||
}
|
||||
catch( RedisException $e )
|
||||
{
|
||||
throw new ConnectionRefusedException(NULL, $e->getMessage());
|
||||
}
|
||||
|
||||
if ( $config['password'] && ! $this->redis->auth($config['password']) )
|
||||
{
|
||||
throw new AuthenticationFailedException; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function select($key, $compressed = NULL)
|
||||
{
|
||||
return $this->redis->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $time
|
||||
* @param mixed $compressed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert($key, $data, $time, $compressed)
|
||||
{
|
||||
return $this->redis->set($key, $data, $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return $this->redis->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function increment($key, $increment)
|
||||
{
|
||||
return $this->redis->incr($key, $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function decrement($key, $decrement)
|
||||
{
|
||||
return $this->redis->decr($key, $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->redis->flushDB();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info($type = NULL)
|
||||
{
|
||||
return $this->redis->info();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
$this->redis->close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace ZN\Cache\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class AuthenticationFailedException extends Exception
|
||||
{
|
||||
const lang =
|
||||
[
|
||||
'en' => 'Authentication failed!',
|
||||
'tr' => 'Geçersiz giriş!'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace ZN\Cache\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class ConnectionRefusedException extends Exception
|
||||
{
|
||||
const lang =
|
||||
[
|
||||
'en' => 'Connection refused! Error:`%`',
|
||||
'tr' => 'Bağlantı sağlanamadı! Hata:`%`'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace ZN\Cache\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class InvalidTimeException extends Exception
|
||||
{
|
||||
const lang =
|
||||
[
|
||||
'en' => '[%] information is invalid time format!',
|
||||
'tr' => '[%] bilgisi geçersiz zaman formatıdır!'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php namespace ZN\Cache\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class UnsupportedDriverException extends Exception
|
||||
{
|
||||
/**
|
||||
* Exception language settings
|
||||
*
|
||||
* @param string en
|
||||
* @param string tr
|
||||
*/
|
||||
const lang =
|
||||
[
|
||||
'en' => '`%` must be loaded to use the driver!!',
|
||||
'tr' => '`%` sürücüsünü kullanmak için yüklenmesi gerekmektedir!'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
<?php namespace ZN\Cache;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Buffering;
|
||||
use ZN\Inclusion;
|
||||
use ZN\Ability\Driver;
|
||||
use ZN\Helpers\Converter;
|
||||
|
||||
class Processor implements ProcessorInterface
|
||||
{
|
||||
use Driver;
|
||||
|
||||
/**
|
||||
* Driver settings
|
||||
*
|
||||
* @param array options
|
||||
* @param string namespace
|
||||
*/
|
||||
const driver =
|
||||
[
|
||||
'options' => ['file', 'apc', 'memcache', 'redis'],
|
||||
'namespace' => 'ZN\Cache\Drivers',
|
||||
'config' => 'Storage:cache',
|
||||
'default' => 'ZN\Cache\CacheDefaultConfiguration'
|
||||
];
|
||||
|
||||
protected $codeCount = 0;
|
||||
protected $refresh = false;
|
||||
protected $key = NULL;
|
||||
|
||||
/**
|
||||
* Refresh cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function refresh()
|
||||
{
|
||||
$this->refresh = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data
|
||||
*
|
||||
* @param array $data = NULL
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function data(?array $data = NULL)
|
||||
{
|
||||
Inclusion\Properties::data($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set key
|
||||
*
|
||||
* @param string $key = NULL
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function key(?string $key = NULL) : Processor
|
||||
{
|
||||
$this->key = $key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache code
|
||||
*
|
||||
* @param callable $function
|
||||
* @param mixed $time = 60
|
||||
* @param string $compressed = 'gz'
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function code(callable $function, $time = 60, string $compress = 'gz') : string
|
||||
{
|
||||
$this->codeCount++;
|
||||
|
||||
if( $this->key === NULL )
|
||||
{
|
||||
$name = 'code-' . $this->codeCount . '-' . CURRENT_CONTROLLER . '-' . CURRENT_CFUNCTION;
|
||||
}
|
||||
else
|
||||
{
|
||||
$name = $this->key;
|
||||
|
||||
$this->key = NULL;
|
||||
}
|
||||
|
||||
$this->refreshCacheData($name);
|
||||
|
||||
if( ! $select = $this->select($name, $compress) )
|
||||
{
|
||||
$output = Buffering\Callback::do($function);
|
||||
|
||||
$this->insert($name, $output, $time, $compress);
|
||||
|
||||
return $output;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $select;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache view
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $time = 60
|
||||
* @param string $compress = 'gz'
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function view(string $file, $time = 60, string $compress = 'gz') : string
|
||||
{
|
||||
return $this->file($file, $time, $compress, 'view');
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache file
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $time = 60
|
||||
* @param string $compress = 'gz'
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function file(string $file, $time = 60, string $compress = 'gz', $type = 'something') : string
|
||||
{
|
||||
$name = Converter::slug($file);
|
||||
|
||||
$this->refreshCacheData($name);
|
||||
|
||||
if( ! $select = $this->select($name, $compress) )
|
||||
{
|
||||
Inclusion\Properties::usable();
|
||||
|
||||
if( $type === 'shomething' )
|
||||
{
|
||||
$output = Inclusion\Something::use($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
$output = Inclusion\View::use($file);
|
||||
}
|
||||
|
||||
$this->insert($name, $output, $time, $compress);
|
||||
|
||||
return $output;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $select; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed = false
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function select(string $key, $compressed = false)
|
||||
{
|
||||
return $this->driver->select($key, $compressed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param mixed $time = 60
|
||||
* @param mixed $compressed = false
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert(string $key, $var, $time = 60, $compressed = false) : bool
|
||||
{
|
||||
if( ! preg_match('/(?<count>[0-9]+)\s*(?<type>second|minute|hour|day|week|month|year)*s*/', $time, $match) )
|
||||
{
|
||||
throw new Exception\InvalidTimeException(NULL, $time);
|
||||
}
|
||||
|
||||
$time = Converter::time($match['count'], $match['type'] ?? 'second', 'second');
|
||||
|
||||
return $this->driver->insert($key, $var, $time, $compressed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $key) : bool
|
||||
{
|
||||
return $this->driver->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function increment(string $key, int $increment = 1) : int
|
||||
{
|
||||
return $this->driver->increment($key, $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function decrement(string $key, int $decrement = 1) : int
|
||||
{
|
||||
return $this->driver->decrement($key, $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clean() : bool
|
||||
{
|
||||
return $this->driver->clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info($type = NULL) : array
|
||||
{
|
||||
return $this->driver->info($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor magic method
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->driver->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* protected refresh
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function refreshCacheData($data)
|
||||
{
|
||||
if( $this->refresh === true )
|
||||
{
|
||||
$this->delete($data);
|
||||
$this->refresh = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php namespace ZN\Cache;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* Refresh cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function refresh();
|
||||
|
||||
/**
|
||||
* Set data
|
||||
*
|
||||
* @param array $data = NULL
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function data(?array $data = NULL);
|
||||
|
||||
/**
|
||||
* Set key
|
||||
*
|
||||
* @param string $key = NULL
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
public function key(?string $key = NULL) : Processor;
|
||||
|
||||
/**
|
||||
* Cache code
|
||||
*
|
||||
* @param callable $function
|
||||
* @param mixed $time = 60
|
||||
* @param string $compressed = 'gz'
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function code(callable $function, $time = 60, string $compress = 'gz') : string;
|
||||
|
||||
/**
|
||||
* Cache view
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $time = 60
|
||||
* @param string $compress = 'gz'
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function view(string $file, $time = 60, string $compress = 'gz') : string;
|
||||
|
||||
/**
|
||||
* Cache file
|
||||
*
|
||||
* @param string $file
|
||||
* @param mixed $time = 60
|
||||
* @param string $compress = 'gz'
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function file(string $file, $time = 60, string $compress = 'gz', $type = 'something') : string;
|
||||
|
||||
/**
|
||||
* Select key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $compressed = false
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function select(string $key, $compressed = false);
|
||||
|
||||
/**
|
||||
* Insert key
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param mixed $time = 60
|
||||
* @param mixed $compressed = false
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert(string $key, $var, $time = 60, $compressed = false) : bool;
|
||||
|
||||
/**
|
||||
* Delete key
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $key) : bool;
|
||||
|
||||
/**
|
||||
* Increment key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function increment(string $key, int $increment = 1) : int;
|
||||
|
||||
/**
|
||||
* Decrement key
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement = 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function decrement(string $key, int $decrement = 1) : int;
|
||||
|
||||
/**
|
||||
* Clean all cache
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function clean() : bool;
|
||||
|
||||
/**
|
||||
* Get info
|
||||
*
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info($info) : array;
|
||||
}
|
||||
Reference in New Issue
Block a user