first commit
This commit is contained in:
Vendored
+33
@@ -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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+79
@@ -0,0 +1,79 @@
|
||||
<?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) {
|
||||
$filename = basename($file);
|
||||
|
||||
$time = substr(strrchr($file, '.'), 1);
|
||||
|
||||
if ($time < time()) {
|
||||
$this->delete(substr($filename, 6, strrpos($filename, '.') - 6));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
$size = filesize($files[0]);
|
||||
|
||||
if ($size > 0) {
|
||||
$data = fread($handle, $size);
|
||||
} else {
|
||||
$data = '';
|
||||
}
|
||||
|
||||
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.' . basename($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(string $key) {
|
||||
$files = glob(DIR_CACHE . 'cache.' . basename($key) . '.*');
|
||||
|
||||
if ($files) {
|
||||
foreach ($files as $file) {
|
||||
if (!@unlink($file)) {
|
||||
clearstatcache(false, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
<?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->expire(CACHE_PREFIX . $key, $this->expire);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function delete($key) {
|
||||
$this->cache->del(CACHE_PREFIX . $key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user