new pisilinux web sites
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user