Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Glob brace fallback, clear component cache on new comment and order, improved cache namespace structure for better invalidation.

givanz1737f6d66a9c
diff --git a/app/controller/base.php b/app/controller/base.php
index 5dc7d6a..4bafef5 100755
--- a/app/controller/base.php
+++ b/app/controller/base.php
@@ -31,6 +31,7 @@ use function Vvveb\filter;
use function Vvveb\setLanguage;
use function Vvveb\siteSettings;
use Vvveb\System\Core\View;
+use Vvveb\System\PageCache;
use Vvveb\System\User\Admin;
use Vvveb\System\User\User;
@@ -208,6 +209,7 @@ class Base {
}
FrontController::closeConnections();
+ PageCache::getInstance()->cleanUp();
die(header("Location: $url"));
}
diff --git a/app/controller/checkout/checkout.php b/app/controller/checkout/checkout.php
index 58c08af..af35539 100755
--- a/app/controller/checkout/checkout.php
+++ b/app/controller/checkout/checkout.php
@@ -32,6 +32,7 @@ use function Vvveb\siteSettings;
use Vvveb\Sql\CountrySQL;
use Vvveb\Sql\RegionSQL;
use Vvveb\Sql\User_AddressSQL;
+use Vvveb\System\CacheManager;
use Vvveb\System\Cart\Cart;
use Vvveb\System\Cart\Order;
use Vvveb\System\Core\View;
@@ -277,6 +278,9 @@ class Checkout extends Base {
$this->view->errors[] = $error;
}
+ // clear notifications cache
+ CacheManager :: clearObjectCache('component', 'notifications');
+
return $this->redirect('checkout/confirm/index');
} else {
$this->view->errors[] = __('Error creating checkout!');
diff --git a/app/controller/content/comment-trait.php b/app/controller/content/comment-trait.php
index 302c866..8e107f3 100644
--- a/app/controller/content/comment-trait.php
+++ b/app/controller/content/comment-trait.php
@@ -25,6 +25,7 @@ namespace Vvveb\Controller\Content;
use function Vvveb\__;
use function Vvveb\model;
use function Vvveb\sanitizeHTML;
+use Vvveb\System\CacheManager;
use Vvveb\System\Event;
trait CommentTrait {
@@ -69,6 +70,9 @@ trait CommentTrait {
$comments[$comment['slug']][$comment["{$commentType}_id"]] = $comment;
$this->session->set($commentType, $comments);
+ // clear notifications cache
+ CacheManager :: clearObjectCache('component', 'notifications');
+
$this->view->success[] = ucfirst($commentName) . __(' was posted!');
} else {
$this->view->errors[] = sprintf(__('Error adding %s!'), $text);
diff --git a/system/cache-manager.php b/system/cache-manager.php
index 0a8b671..d4f09d1 100755
--- a/system/cache-manager.php
+++ b/system/cache-manager.php
@@ -22,13 +22,15 @@
namespace Vvveb\System;
+use function Vvveb\globBrace;
+
class CacheManager {
private static function clearFolder($folder, $root = DIR_PUBLIC) {
$name = $root . $folder . DS;
- $name .= '{*,*/*,*/*/*}';
+ $glob = ['*', '*/*', '*/*/*'];
- $files = glob($name, GLOB_BRACE);
+ $files = globBrace($name,$glob);
if ($files) {
foreach ($files as $file) {
@@ -96,10 +98,10 @@ class CacheManager {
return true;
}
- public static function clearObjectCache($namespace = '') {
+ public static function clearObjectCache($namespace = '', $key = '') {
$cacheDriver = Cache::getInstance();
- return $cacheDriver->delete($namespace);
+ return $cacheDriver->delete($namespace, $key);
}
public static function clearPageCache($namespace = '') {
diff --git a/system/cache.php b/system/cache.php
index a619ac0..9b99208 100755
--- a/system/cache.php
+++ b/system/cache.php
@@ -31,7 +31,7 @@ class Cache {
static $inst = null;
if ($inst === null) {
- $driver = \Vvveb\config(APP . '.cache.driver', 'file');
+ $driver = \Vvveb\config('app.cache.driver', 'file');
$inst = new self($driver);
}
@@ -88,7 +88,7 @@ class Cache {
return $this->driver->setMulti($namespace, $items, $expire, $serverKey);
}
- public function delete($namespace, $key = false) {
+ public function delete($namespace, $key = '') {
return $this->driver->delete($namespace, $key);
}
diff --git a/system/cache/file.php b/system/cache/file.php
index f0f1206..2e82d6e 100755
--- a/system/cache/file.php
+++ b/system/cache/file.php
@@ -34,7 +34,7 @@ class File {
private $cacheDir = DIR_CACHE;
- private $cachePrefix = 'cache.';
+ private $cachePrefix = ''; //'cache.';
public function __construct($options) {
$this->options += $options;
@@ -46,6 +46,7 @@ class File {
protected function validateNamespace($namespace) {
if ($namespace) {
+ $namespace = str_replace(['\\', '/'] , '.', $namespace);
$namespace = sanitizeFileName($namespace);
}
@@ -53,9 +54,10 @@ class File {
}
public function get($namespace, $key) {
- $namespace = $this->validateNamespace($namespace);
+ $namespace = $this->validateNamespace($namespace);
+ $key = $this->validateNamespace($key);
- $files = glob($this->cacheDir . $namespace . 'cache.' . basename($key) . '.*');
+ $files = glob($this->cacheDir . $namespace . '.' . $this->cachePrefix . basename($key) . '.*');
if ($files) {
$data = file_get_contents($files[0]);
@@ -67,7 +69,8 @@ class File {
}
public function set($namespace, $key, $value, $expire = null) {
- $namespace = $this->validateNamespace($namespace);
+ $namespace = $this->validateNamespace($namespace);
+ $key = $this->validateNamespace($key);
if (! $expire) {
$expire = $this->expire;
@@ -75,7 +78,7 @@ class File {
$expire = time() + $expire;
$this->delete($namespace, $key);
- $file = $this->cacheDir . $namespace . 'cache.' . basename($key) . '.' . $expire;
+ $file = $this->cacheDir . $namespace . '.' . $this->cachePrefix . basename($key) . '.' . $expire;
$handle = fopen($file, 'w');
flock($handle, LOCK_EX);
@@ -95,11 +98,11 @@ class File {
return $result;
}
- public function setMulti($namespace, $items, $expire = 0, $serverKey = false) {
+ public function setMulti($namespace, $items, $expire = null, $serverKey = false) {
$namespace = $this->validateNamespace($namespace);
foreach ($items as $key => $value) {
- $this->set($namespace, $key, $value);
+ $this->set($namespace, $key, $value, $expire);
}
}
@@ -110,10 +113,14 @@ class File {
public function delete($namespace, $key = false) {
$namespace = $this->validateNamespace($namespace);
- $name = $this->cacheDir . $namespace;
+ $name = $this->cacheDir;
+
+ if ($namespace) {
+ $name .= $namespace . '.';
+ }
if ($key) {
- $name .= 'cache.' . basename($key) . '.*';
+ $name .= $this->cachePrefix . basename($key) . '.*';
} else {
$name .= '*';
}
diff --git a/system/component/component-base.php b/system/component/component-base.php
index dbfd57e..db764ee 100755
--- a/system/component/component-base.php
+++ b/system/component/component-base.php
@@ -114,8 +114,8 @@ class ComponentBase {
return $this->cacheKey;
}
- $className = strtolower(str_replace('Vvveb\\Component\\', '',get_class($this)));
- $this->cacheKey = md5($className . serialize($this->options));
+ $className = strtolower(str_replace(['Vvveb\Plugins\\', 'Vvveb\Component\\', '\Component\\'], '',get_class($this)));
+ $this->cacheKey = $className . '.' . md5(serialize($this->options));
return $this->cacheKey;
}
diff --git a/system/component/component.php b/system/component/component.php
index e6d63af..b036377 100644
--- a/system/component/component.php
+++ b/system/component/component.php
@@ -53,6 +53,8 @@ class Component {
private $view = false;
+ private $cache = true;
+
static function getInstance($view = false, $regenerate = false, $content = false) {
if (self :: $instance === NULL) {
if (! $view) {
@@ -75,7 +77,7 @@ class Component {
$this->view = $view;
- $this->componentsFile = $view->serviceTemplate() . '.components';
+ $this->componentsFile = $view->serviceTemplate() . '.component';
$this->content = $content;
if ((! file_exists($this->componentsFile)) || $regenerate) {
@@ -108,7 +110,7 @@ class Component {
$cache = [];
$objs = [];
- $namespace = 'components';
+ $namespace = 'component';
$notFound404 = false;
@@ -138,8 +140,8 @@ class Component {
$obj = new $class($options);
$cacheKey = $obj->cacheKey();
- if ($cacheKey) {
- $cacheExpireKey = 'expire_' . $cacheKey;
+ if ($cacheKey && $this->cache) {
+ $cacheExpireKey = $cacheKey . '_expire';
$cache[] = $cacheKey;
$cache[] = $cacheExpireKey;
$obj->component = $component;
@@ -162,106 +164,112 @@ class Component {
}
}
- //get cached data
- $cacheDriver = Cache::getInstance();
- //$cacheDriver = new Memcached();
- $null = [];
- $data = $cacheDriver->getMulti($namespace, $cache, SITE_ID);
-
- foreach ($objs as $cacheKey => $instances) {
- foreach ($instances as $index => $instance) {
- $component = $instance->component;
- $component = str_replace('-', '_', $component);
+ if ($this->cache) {
+ //get cached data
+ $cacheDriver = Cache::getInstance();
+ //$cacheDriver = new Memcached();
+ $null = [];
+ $data = $cacheDriver->getMulti($namespace, $cache, SITE_ID) ?? [];
+
+ foreach ($objs as $cacheKey => $instances) {
+ foreach ($instances as $index => $instance) {
+ if (! isset($data[$cacheKey]) || ! is_array($data[$cacheKey])) {
+ $data[$cacheKey] = [];
+ }
- $data[$cacheKey]['_instance'] = $instance;
- $comp = &$view->_component[$component];
- $comp[$index] = $data[$cacheKey];
+ $component = $instance->component;
+ $component = str_replace('-', '_', $component);
+ $data[$cacheKey]['_instance'] = $instance;
+ $comp = &$view->_component[$component];
+ $comp[$index] = $data[$cacheKey];
- if (isset($comp[$index]['404']) && $comp[$index]['404'] == true) {
- $notFound404 = true;
+ if (isset($comp[$index]['404']) && $comp[$index]['404'] == true) {
+ $notFound404 = true;
+ }
}
- }
- //cache hit, remove from sql regeneration queue
- $cacheExpireKey = 'expire_' . $cacheKey;
+ //cache hit, remove from sql regeneration queue
+ $cacheExpireKey = $cacheKey . '_expire';
- //if no lock set (! -1) and cache is expiring then set lock and set for regeneration
- if (! isset($data[$cacheExpireKey]) || ! isset($data[$cacheKey]) ||
- ($data[$cacheExpireKey] && ($data[$cacheExpireKey] > 0) &&
- ($data[$cacheExpireKey] + COMPONENT_CACHE_EXPIRE_DELAY) < $_SERVER['REQUEST_TIME'])) {
- $cacheDriver->set($cacheExpireKey, COMPONENT_CACHE_FLAG_LOCK, COMPONENT_CACHE_LOCK_EXPIRE); //set lock
- $data[$cacheExpireKey] = COMPONENT_CACHE_FLAG_REGENERATE; //set regeneration flag
- //error_log($data[$cacheExpireKey] . ' regeneration' . $cacheExpireKey);
- }
+ //if no lock set (! -1) and cache is expiring then set lock and set for regeneration
+ if (! isset($data[$cacheExpireKey]) || ! isset($data[$cacheKey]) ||
+ ($data[$cacheExpireKey] && ($data[$cacheExpireKey] > 0) &&
+ ($data[$cacheExpireKey] + COMPONENT_CACHE_EXPIRE_DELAY) < $_SERVER['REQUEST_TIME'])) {
+ $cacheDriver->set($namespace, $cacheExpireKey, COMPONENT_CACHE_FLAG_LOCK, COMPONENT_CACHE_LOCK_EXPIRE); //set lock
+ $data[$cacheExpireKey] = COMPONENT_CACHE_FLAG_REGENERATE; //set regeneration flag
+ }
- if ($data[$cacheExpireKey] > 0) {
- unset($objs[$cacheKey], $cache[$cacheKey], $cache[$cacheExpireKey], $data[$cacheKey], $data[$cacheExpireKey]);
+ if ($data[$cacheExpireKey] > 0) {
+ unset($objs[$cacheKey], $cache[$cacheKey], $cache[$cacheExpireKey], $data[$cacheKey], $data[$cacheExpireKey]);
+ }
}
- }
- $wait = true;
- $retry = 0;
- //run sql queries for uncached components
- $saveCache = [];
+ $wait = true;
+ $retry = 0;
+ //run sql queries for uncached components
+ $saveCache = [];
- while ($wait && $retry < COMPONENT_CACHE_MAX_WAIT_RETRY) {
- $wait = false;
- $retry++;
+ while ($wait && $retry < COMPONENT_CACHE_MAX_WAIT_RETRY) {
+ $wait = false;
+ $retry++;
- foreach ($objs as $key => $objects) {
- $cacheExpireKey = 'expire_' . $key;
- //error_log($cacheExpireKey . ' --- waiTTTTT ' . $data[$cacheExpireKey]);
- //check lock
- if (! isset($data[$cacheExpireKey]) || ! isset($data[$key]) || $data[$cacheExpireKey] == COMPONENT_CACHE_FLAG_REGENERATE) {
- //lock set by this script, regenerate content
- $id = key($objects);
+ foreach ($objs as $key => $objects) {
+ $cacheExpireKey = $key . '_expire';
- $results = $objects[$id]->results();
+ //check lock
+ if ((! isset($data[$cacheExpireKey]) || ! ($data[$cacheExpireKey])) ||
+ ! isset($data[$key]) ||
+ $data[$cacheExpireKey] == COMPONENT_CACHE_FLAG_REGENERATE) {
+ //lock set by this script, regenerate content
+ $id = key($objects);
- if ($results !== null) {
- $saveCache[$key] = $results;
- $saveCache[$cacheExpireKey] = $_SERVER['REQUEST_TIME'] + $objects[$id]->cacheExpire;
- }
+ $results = $objects[$id]->results();
- if (isset($results['404']) && $results['404'] == true) {
- $notFound404 = true;
- }
+ if ($results !== null) {
+ $saveCache[$key] = $results;
+ $saveCache[$cacheExpireKey] = $_SERVER['REQUEST_TIME'] + $objects[$id]->cacheExpire;
+ }
- $data[$cacheExpireKey] = 0;
+ if (isset($results['404']) && $results['404'] == true) {
+ $notFound404 = true;
+ }
- foreach ($objects as $index => $instance) {
- $results['_instance'] = $instance;
- $component = $instance->component;
- $component = str_replace('-', '_', $component);
- $comp = &$view->_component[$component];
- $comp[$index] = $results;
- }
- } else {
- if ($data[$cacheExpireKey] == COMPONENT_CACHE_FLAG_LOCK) {
- //error_log("wait for $cacheExpireKey");
- //item is locked, some other script is generating content
- $wait = true;
+ $data[$cacheExpireKey] = 0;
+
+ foreach ($objects as $index => $instance) {
+ $results['_instance'] = $instance;
+ $component = $instance->component;
+ $component = str_replace('-', '_', $component);
+ $comp = &$view->_component[$component];
+ $comp[$index] = $results;
+ }
+ } else {
+ if ($data[$cacheExpireKey] == COMPONENT_CACHE_FLAG_LOCK) {
+ //error_log("wait for $cacheExpireKey");
+ //item is locked, some other script is generating content
+ $wait = true;
+ }
}
}
+
+ if ($wait) {
+ error_log('wait cache ' . $_SERVER['REQUEST_URI'] . print_r($cache,1));
+ //get
+ @sleep(COMPONENT_CACHE_WAIT);
+ $data = $cacheDriver->getMulti($namespace, $cache);
+ }
}
- if ($wait) {
- error_log('wait cache ' . $_SERVER['REQUEST_URI'] . print_r($cache,1));
- //get
- @sleep(COMPONENT_CACHE_WAIT);
- $data = $cacheDriver->getMulti($namespace, $cache);
+ if ($retry >= COMPONENT_CACHE_MAX_WAIT_RETRY) {
+ error_log('error:CACHE max retry reached for ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
}
- if ($retry >= COMPONENT_CACHE_MAX_WAIT_RETRY) {
- error_log('error:CACHE max retry reached for ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
+ if (! empty($saveCache)) {
+ $cacheDriver->setMulti($namespace, $saveCache, $_SERVER['REQUEST_TIME'] + COMPONENT_CACHE_EXPIRE, SITE_ID);
}
}
- if (! empty($saveCache)) {
- $cacheDriver->setMulti($namespace, $saveCache, $_SERVER['REQUEST_TIME'] + COMPONENT_CACHE_EXPIRE, SITE_ID);
- }
-
//call request for each component
foreach ($this->components as $component => $instances) {
diff --git a/system/functions.php b/system/functions.php
index 5316044..6fe1232 100755
--- a/system/functions.php
+++ b/system/functions.php
@@ -1147,7 +1147,7 @@ function getLanguage() {
function siteSettings($site_id = SITE_ID) {
$cache = System\Cache::getInstance();
- $site = $cache->cache(APP,'site.' . $site_id,function () use ($site_id) {
+ $site = $cache->cache(APP,'site.' . $site_id, function () use ($site_id) {
$siteSql = new Sql\SiteSQL();
$site = $siteSql->get(['site_id' => $site_id]);
@@ -1178,10 +1178,12 @@ function siteSettings($site_id = SITE_ID) {
function checkPhpSyntax($source) {
$tokens = false;
+ if (function_exists('token_get_all')) {
try {
$tokens = token_get_all($source, TOKEN_PARSE);
} catch (\ParseError $e) {
}
+ }
return $tokens ? true : false;
}
@@ -1351,8 +1353,9 @@ function download($url) {
}
function getUrl($url, $cache = true, $expire = 0) {
- $cacheDriver = System\Cache :: getInstance();
- $cacheKey = md5($url);
+ $cacheDriver = System\Cache :: getInstance();
+ $cacheKey = md5($url);
+ $result = false;
if ($cache && ($result = $cacheDriver->get('url', $cacheKey))) {
return $result;
@@ -1378,25 +1381,29 @@ function getUrl($url, $cache = true, $expire = 0) {
return $result;
} else {
//try with curl
- $ch = curl_init($url);
+ if (function_exists('curl_init')) {
+ $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ $result = curl_exec($ch);
+ curl_close($ch);
- if ($result) {
- if ($cache) {
- $cacheDriver->set('url', $cacheKey, $result);
- }
+ if ($result) {
+ if ($cache) {
+ $cacheDriver->set('url', $cacheKey, $result);
+ }
- return $result;
- } else {
- throw new \Exception('Curl error: ' . curl_errno($ch) . ' - ' . curl_error($ch));
+ return $result;
+ } else {
+ throw new \Exception('Curl error: ' . curl_errno($ch) . ' - ' . curl_error($ch));
- return [];
+ return [];
+ }
}
}
}
+
+ return $result;
}
function unzip($file) {
@@ -1460,3 +1467,19 @@ function commentStatusBadgeClass($status = 0) {
return $classes[$status] ?? 'bg-secondary-subtle text-dark';
}
+
+function globBrace($path, $glob, $filename = '') {
+ //$glob = ['*','*/*','*/*/*'];
+ $files = [];
+
+ if (false && defined('GLOB_BRACE')) {
+ $path .= '{' . implode(',', $glob) . '}' . $filename;
+ $files = glob($path, GLOB_BRACE);
+ } else {
+ foreach ($glob as $pattern) {
+ $files = array_merge($files, glob($path . DS . $pattern . $filename));
+ }
+ }
+
+ return $files;
+}
diff --git a/system/page-cache.php b/system/page-cache.php
index 01e94a8..1cd10e6 100755
--- a/system/page-cache.php
+++ b/system/page-cache.php
@@ -22,6 +22,7 @@
namespace Vvveb\System;
+use function Vvveb\globBrace;
use Vvveb\System\Core\FrontController;
class PageCache {
@@ -31,7 +32,7 @@ class PageCache {
const CACHE_DIR = PAGE_CACHE_DIR;
- const MAX_LOCK_SECONDS = 60;
+ const MAX_LOCK_SECONDS = 10;
private $fileName;
@@ -94,7 +95,11 @@ class PageCache {
function startGenerating() {
$dir = dirname($this->fileName);
- if (! file_exists($dir)) {
+ if (! is_dir($dir)) {
+ //if page with the same name as folder remove
+ if (file_exists($dir)) {
+ unlink($dir);
+ }
mkdir($dir, (0755 & ~umask()), true);
}
@@ -153,11 +158,12 @@ class PageCache {
}
function cleanUp() {
- //remove lock
- $file = $this->fileName . self :: LOCK_EXT;
-
- if (file_exists($file)) {
- return unlink($file);
+ //remove lock and stale cache
+ foreach ([$this->fileName . self :: LOCK_EXT,
+ $this->fileName . self :: STALE_EXT, ] as $file) {
+ if (file_exists($file)) {
+ return unlink($file);
+ }
}
return false;
@@ -178,6 +184,10 @@ class PageCache {
$this->fileName && $data &&
http_response_code() == 200) {
//create directory structure
+ if (is_dir($this->fileName)) {
+ $this->fileName .= 'index.html';
+ }
+
$dir = dirname($this->fileName);
if (! file_exists($dir)) {
@@ -197,10 +207,11 @@ class PageCache {
}
function purge($path = '/') {
- $name = $this->cacheFolder . $path;
- $name .= '{,*/*/,*/}*';
+ $folder = $this->cacheFolder . $path;
+ $glob = ['', '*/*/', '*/'];
- $files = glob($name, GLOB_BRACE);
+ //$files = glob($name, GLOB_BRACE);
+ $files = globBrace($folder, $glob, '*');
if ($files) {
foreach ($files as $file) {
@@ -217,11 +228,11 @@ class PageCache {
return true;
}
- static function enable() {
- setcookie('nocache', '', time() - 3600, '/');
+ static function enable($type = false) {
+ setcookie((in_array($type, ['user', 'cart']) ? $type : 'nocache'), '', time() - 3600, '/');
}
- static function disable() {
- setcookie('nocache', '1', 0, '/');
+ static function disable($type = false) {
+ setcookie((in_array($type, ['user', 'cart']) ? $type : 'nocache'), '1', 0, '/');
}
}