From c6e6ae59ec2878a9ed819b6d2c3e25eee3f5e71a Mon Sep 17 00:00:00 2001 From: Matteo Gheza Date: Wed, 23 Jun 2021 23:20:51 +0200 Subject: [PATCH] Better caching --- .gitignore | 2 + server/cache/index.php | 4 + server/composer.json | 3 +- server/composer.lock | 219 ++++++++++++++++++++++++++++++++++++++++- server/core.php | 43 ++++---- 5 files changed, 249 insertions(+), 22 deletions(-) create mode 100644 server/cache/index.php diff --git a/.gitignore b/.gitignore index 5e4108a..8dc7740 100644 --- a/.gitignore +++ b/.gitignore @@ -508,6 +508,8 @@ custom-*.* *.log *.txt +/server/cache/* +!/server/cache/index.php !/server/robots.txt config.json diff --git a/server/cache/index.php b/server/cache/index.php new file mode 100644 index 0000000..bd36de4 --- /dev/null +++ b/server/cache/index.php @@ -0,0 +1,4 @@ +rickroll(); \ No newline at end of file diff --git a/server/composer.json b/server/composer.json index c8da46e..853ea84 100644 --- a/server/composer.json +++ b/server/composer.json @@ -28,7 +28,8 @@ "ministryofweb/php-osm-tiles": "2.0.0", "jenstornell/tiny-html-minifier": "dev-master", "delight-im/db": "1.3.1", - "webonyx/graphql-php": "14.9.0" + "webonyx/graphql-php": "14.9.0", + "phpfastcache/phpfastcache": "^8.0" }, "license": "GPL-3.0-or-later", "authors": [ diff --git a/server/composer.lock b/server/composer.lock index 4e1497b..b042dc2 100644 --- a/server/composer.lock +++ b/server/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d580f574ccf54bd281d6ec715246e61f", + "content-hash": "84ab5004b5cdeff7c65bb6c7480311d8", "packages": [ { "name": "azuyalabs/yasumi", @@ -1438,6 +1438,172 @@ }, "time": "2020-07-07T09:29:14+00:00" }, + { + "name": "phpfastcache/phpfastcache", + "version": "8.0.5", + "source": { + "type": "git", + "url": "https://github.com/PHPSocialNetwork/phpfastcache.git", + "reference": "6d66cc0604bcbd7681ef1b3fdd1ba22901a16b1c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPSocialNetwork/phpfastcache/zipball/6d66cc0604bcbd7681ef1b3fdd1ba22901a16b1c", + "reference": "6d66cc0604bcbd7681ef1b3fdd1ba22901a16b1c", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "php": ">=7.3", + "psr/cache": "~1.0.0", + "psr/simple-cache": "~1.0.0" + }, + "conflict": { + "basho/riak": "*", + "doctrine/couchdb": "*" + }, + "require-dev": { + "league/climate": "^3.5" + }, + "suggest": { + "ext-apc": "*", + "ext-couchbase": "*", + "ext-intl": "*", + "ext-leveldb": "*", + "ext-memcache": "*", + "ext-memcached": "*", + "ext-redis": "*", + "ext-sqlite": "*", + "ext-wincache": "*", + "ext-xcache": "*", + "mongodb/mongodb": "^1.1", + "phpfastcache/couchdb": "~1.0.0", + "phpfastcache/phpssdb": "~1.0.0", + "phpfastcache/riak-client": "~1.4.4", + "predis/predis": "~1.1.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Phpfastcache\\": "lib/Phpfastcache/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Georges.L", + "email": "contact@geolim4.com", + "homepage": "https://github.com/Geolim4", + "role": "Actual Project Manager/Developer" + }, + { + "name": "Khoa Bui", + "email": "khoaofgod@gmail.com", + "homepage": "https://www.phpfastcache.com", + "role": "Former Project Developer/Original Creator" + } + ], + "description": "PHP Abstract Cache Class - Reduce your database call using cache system. PhpFastCache handles a lot of drivers such as Apc(u), Cassandra, CouchBase, Couchdb, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.", + "homepage": "https://www.phpfastcache.com", + "keywords": [ + "LevelDb", + "abstract", + "apc", + "apcu", + "cache", + "cache class", + "caching", + "cassandra", + "cookie", + "couchbase", + "couchdb", + "files cache", + "memcache", + "memcached", + "mongodb", + "mysql cache", + "pdo cache", + "php cache", + "predis", + "redis", + "ssdb", + "wincache", + "xcache", + "zend", + "zend data cache", + "zend disk cache", + "zend memory cache", + "zend server" + ], + "support": { + "issues": "https://github.com/PHPSocialNetwork/phpfastcache/issues", + "source": "https://github.com/PHPSocialNetwork/phpfastcache/tree/8.0.5" + }, + "funding": [ + { + "url": "https://github.com/geolim4", + "type": "github" + }, + { + "url": "https://www.patreon.com/geolim4", + "type": "patreon" + } + ], + "time": "2021-04-05T22:24:16+00:00" + }, + { + "name": "psr/cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8", + "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "support": { + "source": "https://github.com/php-fig/cache/tree/master" + }, + "time": "2016-08-06T20:24:11+00:00" + }, { "name": "psr/container", "version": "1.1.1", @@ -1696,6 +1862,57 @@ }, "time": "2021-05-03T11:20:27+00:00" }, + { + "name": "psr/simple-cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "support": { + "source": "https://github.com/php-fig/simple-cache/tree/master" + }, + "time": "2017-10-23T01:57:42+00:00" + }, { "name": "ralouphie/getallheaders", "version": "3.0.3", diff --git a/server/core.php b/server/core.php index 185590b..4a5bb29 100644 --- a/server/core.php +++ b/server/core.php @@ -3,13 +3,15 @@ require_once 'vendor/autoload.php'; use DebugBar\StandardDebugBar; use MinistryOfWeb\OsmTiles\Converter; use MinistryOfWeb\OsmTiles\LatLng; +use Phpfastcache\CacheManager; +use Phpfastcache\Config\ConfigurationOption; -if(!file_exists("config.php") && !file_exists("../../config.php")) { +if(!file_exists(realpath(dirname(__FILE__).'/config.php'))) { header('Location: install/install.php'); exit(); } -require_once 'config.php'; +require_once realpath(dirname(__FILE__).'/config.php'); if(SENTRY_ENABLED){ \Sentry\init([ @@ -35,6 +37,7 @@ class tools public $profiler_enabled; public $profiler_last_name = ""; public $script_nonce = null; + public $cache; public function generateNonce($bytes_lenght = 16, $base64_encode = false){ $nonce = bin2hex(random_bytes($bytes_lenght)); @@ -59,6 +62,10 @@ class tools $_SESSION["script_nonce"] = $this->script_nonce; } } + CacheManager::setDefaultConfig(new ConfigurationOption([ + 'path' => realpath(dirname(__FILE__).'/cache') + ])); + $this->cache = CacheManager::getInstance('files'); } public function validate_form($data, $expected_value=null, $data_source=null) @@ -246,7 +253,7 @@ class tools } } - public function cachePreviewMap($filename, $lat, $lng, $zoom=16){ + public function savePreviewMap($filename, $lat, $lng, $zoom=16){ $url = $this->convertMapAddressToUrl($lat, $lng, $zoom); $options = ['http' => [ 'user_agent' => 'AllertaVVF dev version (cached map previews generator)' @@ -284,7 +291,7 @@ class tools $lat = explode(";", $place)[0]; $lng = explode(";", $place)[1]; $mapImageID = \Delight\Auth\Auth::createUuid(); - $this->cachePreviewMap($mapImageID, $lat, $lng); + $this->savePreviewMap($mapImageID, $lat, $lng); $place = $place . "#" . $mapImageID; } } @@ -314,25 +321,21 @@ class tools class options { protected $db; - public $load_from_file = true; + protected $tools; + public $bypassCache = false; public $options = []; - public $options_cache_file = null; + public $optionsCache; - public function __construct($db){ + public function __construct($db, $tools){ $this->db = $db; - $file_infos = pathinfo(array_reverse(debug_backtrace())[0]['file']); - if(strpos($file_infos['dirname'], 'resources') !== false) { - $this->options_cache_file = "../../options.txt"; - } else { - $this->options_cache_file = "options.txt"; - } - if($this->load_from_file) { - if(file_exists($this->options_cache_file)/* && time()-@filemtime($this->options_cache_file) < 604800*/) { - $this->options = json_decode(file_get_contents($this->options_cache_file), true); - } else { - $this->options = $db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1"); - file_put_contents($this->options_cache_file, json_encode($this->options)); + $this->tools = $tools; + if(!$this->bypassCache){ + $this->optionsCache = $this->tools->cache->getItem("options"); + if (is_null($this->optionsCache->get())) { + $this->optionsCache->set($db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1"))->expiresAfter(60); + $this->tools->cache->save($this->optionsCache); } + $this->options = $this->optionsCache->get(); } else { $this->options = $db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1"); } @@ -911,8 +914,8 @@ function init_class($enableDebugger=true, $headers=true) { global $tools, $options, $db, $user, $crud, $translations, $debugbar; init_db(); - $options = new options($db); $tools = new tools($db, $enableDebugger); + $options = new options($db, $tools); $user = new user($db, $tools); $crud = new crud($tools, $db, $user); $translations = new translations(get_option("force_language"));