first commit
This commit is contained in:
94
system/library/cache/apcu.php
vendored
Normal file
94
system/library/cache/apcu.php
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cache;
|
||||
/**
|
||||
* Class APCU
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class APCU {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $expire;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $active;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $expire
|
||||
*/
|
||||
public function __construct(int $expire = 3600) {
|
||||
$this->expire = $expire;
|
||||
$this->active = function_exists('apcu_cache_info') && ini_get('apc.enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function get(string $key): array|string|null {
|
||||
return $this->active ? apcu_fetch(CACHE_PREFIX . $key) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param string $key
|
||||
* @param array|string|null $key
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, array|string|null $value, int $expire = 0): void {
|
||||
if (!$expire) {
|
||||
$expire = $this->expire;
|
||||
}
|
||||
|
||||
if ($this->active) {
|
||||
apcu_store(CACHE_PREFIX . $key, $value, $expire);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $key): void {
|
||||
if ($this->active) {
|
||||
$cache_info = apcu_cache_info();
|
||||
|
||||
$cache_list = $cache_info['cache_list'];
|
||||
|
||||
foreach ($cache_list as $entry) {
|
||||
if (strpos($entry['info'], CACHE_PREFIX . $key) === 0) {
|
||||
apcu_delete($entry['info']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all cache
|
||||
*
|
||||
* @param null
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function flush(): bool {
|
||||
$status = false;
|
||||
|
||||
if (function_exists('apcu_clear_cache')) {
|
||||
$status = apcu_clear_cache();
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
95
system/library/cache/file.php
vendored
Normal file
95
system/library/cache/file.php
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cache;
|
||||
/**
|
||||
* Class File
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class File {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $expire;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $expire
|
||||
*/
|
||||
public function __construct(int $expire = 3600) {
|
||||
$this->expire = $expire;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function get(string $key): array|string|null {
|
||||
$files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');
|
||||
|
||||
if ($files) {
|
||||
return json_decode(file_get_contents($files[0]), true);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param string $key
|
||||
* @param array|string|null $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, array|string|null $value, int $expire = 0): void {
|
||||
$this->delete($key);
|
||||
|
||||
if (!$expire) {
|
||||
$expire = $this->expire;
|
||||
}
|
||||
|
||||
file_put_contents(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . (time() + $expire), json_encode($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $key): void {
|
||||
$files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');
|
||||
|
||||
if ($files) {
|
||||
foreach ($files as $file) {
|
||||
if (!@unlink($file)) {
|
||||
clearstatcache(false, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
public function __destruct() {
|
||||
$files = glob(DIR_CACHE . 'cache.*');
|
||||
|
||||
if ($files && rand(1, 100) == 1) {
|
||||
foreach ($files as $file) {
|
||||
$time = substr(strrchr($file, '.'), 1);
|
||||
|
||||
if ($time < time()) {
|
||||
if (!@unlink($file)) {
|
||||
clearstatcache(false, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
69
system/library/cache/mem.php
vendored
Normal file
69
system/library/cache/mem.php
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cache;
|
||||
/**
|
||||
* Class Mem
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Mem {
|
||||
/**
|
||||
* @var object|\Memcache
|
||||
*/
|
||||
private object $memcache;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $expire;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const CACHEDUMP_LIMIT = 9999;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $expire
|
||||
*/
|
||||
public function __construct(int $expire = 3600) {
|
||||
$this->expire = $expire;
|
||||
|
||||
$this->memcache = new \Memcache();
|
||||
$this->memcache->pconnect(CACHE_HOSTNAME, CACHE_PORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function get(string $key): array|string|null {
|
||||
return $this->memcache->get(CACHE_PREFIX . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param string $key
|
||||
* @param array|string|null $value
|
||||
* @param int $expire
|
||||
*/
|
||||
public function set(string $key, array|string|null $value, int $expire = 0) {
|
||||
if (!$expire) {
|
||||
$expire = $this->expire;
|
||||
}
|
||||
|
||||
$this->memcache->set(CACHE_PREFIX . $key, $value, MEMCACHE_COMPRESSED, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param string $key
|
||||
*/
|
||||
public function delete(string $key) {
|
||||
$this->memcache->delete(CACHE_PREFIX . $key);
|
||||
}
|
||||
}
|
69
system/library/cache/memcached.php
vendored
Normal file
69
system/library/cache/memcached.php
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cache;
|
||||
/**
|
||||
* Class Memcached
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Memcached {
|
||||
/**
|
||||
* @var object|\Memcached
|
||||
*/
|
||||
private object $memcached;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $expire;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const CACHEDUMP_LIMIT = 9999;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $expire
|
||||
*/
|
||||
public function __construct(int $expire = 3600) {
|
||||
$this->expire = $expire;
|
||||
|
||||
$this->memcached = new \Memcached();
|
||||
$this->memcached->addServer(CACHE_HOSTNAME, CACHE_PORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function get(string $key): array|string|null {
|
||||
return $this->memcached->get(CACHE_PREFIX . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param string $key
|
||||
* @param array|string|null $value
|
||||
* @param int $expire
|
||||
*/
|
||||
public function set(string $key, array|string|null $value, int $expire = 0) {
|
||||
if (!$expire) {
|
||||
$expire = $this->expire;
|
||||
}
|
||||
|
||||
$this->memcached->set(CACHE_PREFIX . $key, $value, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param string $key
|
||||
*/
|
||||
public function delete(string $key) {
|
||||
$this->memcached->delete(CACHE_PREFIX . $key);
|
||||
}
|
||||
}
|
70
system/library/cache/redis.php
vendored
Normal file
70
system/library/cache/redis.php
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\Cache;
|
||||
/**
|
||||
* Class Redis
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class Redis {
|
||||
/**
|
||||
* @var object|\Redis
|
||||
*/
|
||||
private object $redis;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $expire;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $expire
|
||||
*/
|
||||
public function __construct(int $expire = 3600) {
|
||||
$this->expire = $expire;
|
||||
|
||||
$this->redis = new \Redis();
|
||||
$this->redis->pconnect(CACHE_HOSTNAME, CACHE_PORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function get(string $key): array|string|null {
|
||||
$data = $this->redis->get(CACHE_PREFIX . $key);
|
||||
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set
|
||||
*
|
||||
* @param string $key
|
||||
* @param array|string|null $value
|
||||
* @param int $expire
|
||||
*/
|
||||
public function set(string $key, array|string|null $value, int $expire = 0) {
|
||||
if (!$expire) {
|
||||
$expire = $this->expire;
|
||||
}
|
||||
|
||||
$status = $this->redis->set(CACHE_PREFIX . $key, json_encode($value));
|
||||
|
||||
if ($status) {
|
||||
$this->redis->expire(CACHE_PREFIX . $key, $expire);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param string $key
|
||||
*/
|
||||
public function delete(string $key): void {
|
||||
$this->redis->del(CACHE_PREFIX . $key);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user