Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Use null to check if value is not cached, set crc32 of root directory for cache prefix for shared hosting for apcu/redis/memcached, added automatic purge for file cache

givanz0ca05a07a6ba
diff --git a/system/cache.php b/system/cache.php
index 9b99208..8ea59a3 100755
--- a/system/cache.php
+++ b/system/cache.php
@@ -65,9 +65,10 @@ class Cache {
// cache the results of the callback retrive if exists or save if expired
public function cache($namespace, $key, $callback, $expire = 0) {
- if ($value = $this->driver->get($namespace, $key)) {
+ if (($value = $this->driver->get($namespace, $key)) !== null) {
return $value;
}
+
$value = $callback();
$expire = $expire ?? $this->expire;
diff --git a/system/cache/apcu.php b/system/cache/apcu.php
index b0a5ee3..27f8a04 100755
--- a/system/cache/apcu.php
+++ b/system/cache/apcu.php
@@ -29,22 +29,29 @@ class APCu {
private $active;
- private $cachePrefix = 'cache.';
+ private $cachePrefix = '';
public function __construct($options) {
$this->options += $options;
- $this->cachePrefix = md5(DIR_ROOT); //unique instance for shared hosting
+ $this->cachePrefix = crc32(DIR_ROOT); //unique instance for shared hosting
$this->expire = $this->options['expire'] ?? $this->expire;
$this->active = function_exists('apcu_cache_info') && ini_get('apc.enabled');
}
private function key($namespace, $key = '') {
- return str_replace(['\\', '/'], '.', $this->cachePrefix . $namespace . $key);
+ return str_replace(['\\', '/'], '.', $this->cachePrefix . $namespace . '.' . $key);
}
public function get($namespace, $key) {
- return $this->active ? apcu_fetch($this->key($namespace, $key)) : null;
+ $success = false;
+ $result = $this->active ? apcu_fetch($this->key($namespace, $key), $success) : null;
+
+ if ($success) {
+ return $result;
+ }
+
+ return null;
}
public function set($namespace, $key, $value, $expire = null) {
@@ -60,6 +67,7 @@ class APCu {
public function getMulti($namespace, $keys, $serverKey = false) {
$result = [];
$fullKeys = [];
+ $success = false;
foreach ($keys as &$key) {
// simulate with single call version
@@ -71,11 +79,15 @@ class APCu {
$key = $newKey;
}
- $result = $this->active ? apcu_fetch($keys) : null;
+ $result = $this->active ? apcu_fetch($keys, $success) : null;
if ($result) {
foreach ($fullKeys as $key => &$fullKey) {
- $fullKeys[$key] = $result[$fullKey] ?? null;
+ $fullKeys[$key] = null;
+
+ if (isset($result[$fullKey]) && $success) {
+ $fullKeys[$key] = $fullKeys[$key];
+ }
}
return $fullKeys;
@@ -94,6 +106,8 @@ class APCu {
if ($this->active) {
if ($namespace) {
if ($key) {
+ return apcu_delete(new \APCUIterator('/' . $this->key($namespace, $key) . '.*/'));
+
return apcu_delete($this->key($namespace, $key));
} else {
return apcu_delete(new \APCUIterator('/' . $this->key($namespace, $key) . '.*/'));
diff --git a/system/cache/file.php b/system/cache/file.php
index 70cda03..5d15d48 100755
--- a/system/cache/file.php
+++ b/system/cache/file.php
@@ -60,9 +60,11 @@ class File {
$files = glob($this->cacheDir . $namespace . '.' . $this->cachePrefix . basename($key) . '.*');
if ($files) {
- $data = file_get_contents($files[0]);
+ if (file_exists($files[0])) {
+ $data = file_get_contents($files[0]);
- return json_decode($data, true);
+ return json_decode($data, true);
+ }
}
return null;
@@ -142,4 +144,24 @@ class File {
}
}
}
+
+ public function purgeExpired() {
+ $files = glob($this->cacheDir);
+
+ if (rand(1, 1000) == 1 && $files = (glob($this->cacheDir))) {
+ foreach ($files as $file) {
+ $time = substr(strrchr($file, '.'), 1);
+
+ if ($time < time()) {
+ if (! @unlink($file)) {
+ clearstatcache(false, $file);
+ }
+ }
+ }
+ }
+ }
+
+ public function __destruct() {
+ $this->purgeExpired();
+ }
}
diff --git a/system/cache/memcached.php b/system/cache/memcached.php
index c0c22b0..dbb5e1d 100755
--- a/system/cache/memcached.php
+++ b/system/cache/memcached.php
@@ -27,6 +27,8 @@ class Memcached {
private $memcached;
+ private $cachePrefix = '';
+
public function stats($time = false) {
$stats = $this->memcached->getStats();
/*
@@ -43,8 +45,9 @@ class Memcached {
public function __construct($options) {
//$this->expire = $expire;
- $this->memcached = new \Memcached();
+ $this->cachePrefix = crc32(DIR_ROOT); //unique instance for shared hosting
+ $this->memcached = new \Memcached();
$this->memcached->addServers($options['servers']);
if (isset($options['options'])) {
@@ -56,12 +59,16 @@ class Memcached {
return $this->memcached;
}
+ private function key($namespace, $key = '') {
+ return $this->cachePrefix . ($namespace ? ".$namespace" : '') . $key;
+ }
+
public function get($namespace, $key) {
- return $this->memcached->get($namespace . $key);
+ return $this->memcached->get($this->key($namespace, $key));
}
public function set($namespace, $key, $value, $expire = 0) {
- return $this->memcached->set($namespace . $key, $value, $expire);
+ return $this->memcached->set($this->key($namespace, $key), $value, $expire);
}
public function getMulti($namespace, $keys, $serverKey = false) {
@@ -81,6 +88,6 @@ class Memcached {
}
public function delete($namespace, $key) {
- $this->memcached->delete($namespace . $key);
+ $this->memcached->delete($this->key($namespace, $key));
}
}
diff --git a/system/cache/redis.php b/system/cache/redis.php
index 7f57405..1e016b7 100755
--- a/system/cache/redis.php
+++ b/system/cache/redis.php
@@ -34,6 +34,7 @@ class Redis {
public function __construct($options) {
$this->options += $options;
+ $this->cachePrefix = crc32(DIR_ROOT); //unique instance for shared hosting
$this->expire = $this->options['expire'] ?? $this->expire;
$this->cachePrefix = $this->options['prefix'] ?? $this->cachePrefix;