diff options
author | Jesús <heckyel@hyperbola.info> | 2019-08-18 21:14:58 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-08-18 21:14:58 -0500 |
commit | 2eed7b082f83630301e51f57ca8394de228a8605 (patch) | |
tree | 1d19962d22d30f99317d9276e4bae7744fc93fc2 /public/system/library/cache | |
download | librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.lz librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.xz librecart-2eed7b082f83630301e51f57ca8394de228a8605.zip |
first commit
Diffstat (limited to 'public/system/library/cache')
-rw-r--r-- | public/system/library/cache/apc.php | 33 | ||||
-rw-r--r-- | public/system/library/cache/file.php | 73 | ||||
-rw-r--r-- | public/system/library/cache/mem.php | 27 | ||||
-rw-r--r-- | public/system/library/cache/memcached.php | 27 | ||||
-rw-r--r-- | public/system/library/cache/redis.php | 30 |
5 files changed, 190 insertions, 0 deletions
diff --git a/public/system/library/cache/apc.php b/public/system/library/cache/apc.php new file mode 100644 index 0000000..5acd778 --- /dev/null +++ b/public/system/library/cache/apc.php @@ -0,0 +1,33 @@ +<?php +namespace Cache; +class APC { + private $expire; + private $active = false; + + public function __construct($expire) { + $this->expire = $expire; + $this->active = function_exists('apc_cache_info') && ini_get('apc.enabled'); + } + + public function get($key) { + return $this->active ? apc_fetch(CACHE_PREFIX . $key) : false; + } + + public function set($key, $value) { + return $this->active ? apc_store(CACHE_PREFIX . $key, $value, $this->expire) : false; + } + + public function delete($key) { + if (!$this->active) { + return false; + } + + $cache_info = apc_cache_info('user'); + $cache_list = $cache_info['cache_list']; + foreach ($cache_list as $entry) { + if (strpos($entry['info'], CACHE_PREFIX . $key) === 0) { + apcu_delete($entry['info']); + } + } + } +} diff --git a/public/system/library/cache/file.php b/public/system/library/cache/file.php new file mode 100644 index 0000000..0330771 --- /dev/null +++ b/public/system/library/cache/file.php @@ -0,0 +1,73 @@ +<?php +namespace Cache; +class File { + private $expire; + + public function __construct($expire = 3600) { + $this->expire = $expire; + + $files = glob(DIR_CACHE . 'cache.*'); + + if ($files) { + foreach ($files as $file) { + $time = substr(strrchr($file, '.'), 1); + + if ($time < time()) { + if (file_exists($file)) { + unlink($file); + } + } + } + } + } + + public function get($key) { + $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*'); + + if ($files) { + $handle = fopen($files[0], 'r'); + + flock($handle, LOCK_SH); + + $data = fread($handle, filesize($files[0])); + + flock($handle, LOCK_UN); + + fclose($handle); + + return json_decode($data, true); + } + + return false; + } + + public function set($key, $value) { + $this->delete($key); + + $file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . (time() + $this->expire); + + $handle = fopen($file, 'w'); + + flock($handle, LOCK_EX); + + fwrite($handle, json_encode($value)); + + fflush($handle); + + flock($handle, LOCK_UN); + + fclose($handle); + } + + public function delete($key) { + $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*'); + + if ($files) { + foreach ($files as $file) { + if (file_exists($file)) { + unlink($file); + } + } + } + } +}
\ No newline at end of file diff --git a/public/system/library/cache/mem.php b/public/system/library/cache/mem.php new file mode 100644 index 0000000..4e556e5 --- /dev/null +++ b/public/system/library/cache/mem.php @@ -0,0 +1,27 @@ +<?php +namespace Cache; +class Mem { + private $expire; + private $memcache; + + const CACHEDUMP_LIMIT = 9999; + + public function __construct($expire) { + $this->expire = $expire; + + $this->memcache = new \Memcache(); + $this->memcache->pconnect(CACHE_HOSTNAME, CACHE_PORT); + } + + public function get($key) { + return $this->memcache->get(CACHE_PREFIX . $key); + } + + public function set($key, $value) { + return $this->memcache->set(CACHE_PREFIX . $key, $value, MEMCACHE_COMPRESSED, $this->expire); + } + + public function delete($key) { + $this->memcache->delete(CACHE_PREFIX . $key); + } +} diff --git a/public/system/library/cache/memcached.php b/public/system/library/cache/memcached.php new file mode 100644 index 0000000..d6ae2f4 --- /dev/null +++ b/public/system/library/cache/memcached.php @@ -0,0 +1,27 @@ +<?php +namespace Cache; +class Memcached { + private $expire; + private $memcached; + + const CACHEDUMP_LIMIT = 9999; + + public function __construct($expire) { + $this->expire = $expire; + $this->memcached = new \Memcached(); + + $this->memcached->addServer(CACHE_HOSTNAME, CACHE_PORT); + } + + public function get($key) { + return $this->memcached->get(CACHE_PREFIX . $key); + } + + public function set($key, $value) { + return $this->memcached->set(CACHE_PREFIX . $key, $value, $this->expire); + } + + public function delete($key) { + $this->memcached->delete(CACHE_PREFIX . $key); + } +} diff --git a/public/system/library/cache/redis.php b/public/system/library/cache/redis.php new file mode 100644 index 0000000..315f27b --- /dev/null +++ b/public/system/library/cache/redis.php @@ -0,0 +1,30 @@ +<?php +namespace Cache; +class Redis { + private $expire; + private $cache; + + public function __construct($expire) { + $this->expire = $expire; + + $this->cache = new \Redis(); + $this->cache->pconnect(CACHE_HOSTNAME, CACHE_PORT); + } + + public function get($key) { + $data = $this->cache->get(CACHE_PREFIX . $key); + return json_decode($data, true); + } + + public function set($key,$value) { + $status = $this->cache->set(CACHE_PREFIX . $key, json_encode($value)); + if($status){ + $this->cache->setTimeout(CACHE_PREFIX . $key, $this->expire); + } + return $status; + } + + public function delete($key) { + $this->cache->delete(CACHE_PREFIX . $key); + } +}
\ No newline at end of file |