Page MenuHomeDevCentral

D3865.id.diff
No OneTemporary

D3865.id.diff

diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -17,10 +17,12 @@
}
],
"require": {
+ "keruald/cache": "^0.1.1",
"keruald/database": "0.6.1",
"keruald/omnitools": "dev-develop/1.x@dev",
"keruald/yaml": "0.1.1",
"netresearch/jsonmapper": "^v5.0.0",
+ "psr/simple-cache": "^1.0 || ^2.0 || ^3.0",
"smarty/smarty": "^5.6.0",
"vlucas/phpdotenv": "^v5.6.2",
"ext-mysqli": "*"
diff --git a/workspaces/composer.json b/workspaces/composer.json
--- a/workspaces/composer.json
+++ b/workspaces/composer.json
@@ -3,9 +3,11 @@
"description": "Core interfaces for Obsidian Workspaces",
"type": "project",
"require": {
+ "keruald/cache": "^0.1.1",
"keruald/database": "0.5.2",
"keruald/omnitools": "0.16.0",
"keruald/yaml": "0.1.1",
+ "psr/simple-cache": "^1.0 || ^2.0 || ^3.0",
"smarty/smarty": "^5.6.0"
},
"require-dev": {
diff --git a/workspaces/src/Engines/Framework/Application.php b/workspaces/src/Engines/Framework/Application.php
--- a/workspaces/src/Engines/Framework/Application.php
+++ b/workspaces/src/Engines/Framework/Application.php
@@ -2,10 +2,12 @@
namespace Waystone\Workspaces\Engines\Framework;
-use Keruald\Database\Database;
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
use Waystone\Workspaces\Engines\Users\UserRepository;
+use Keruald\Cache\CacheFactory;
+use Keruald\Database\Database;
+
class Application {
public static function init () : void {
@@ -28,6 +30,8 @@
$context->url = get_current_url_fragments();
$context->initializeTemplateEngine($context->config['Theme']);
+ $context->cache = CacheFactory::load($context->config["cache"]);
+
return $context;
}
diff --git a/workspaces/src/Engines/Framework/Context.php b/workspaces/src/Engines/Framework/Context.php
--- a/workspaces/src/Engines/Framework/Context.php
+++ b/workspaces/src/Engines/Framework/Context.php
@@ -18,9 +18,9 @@
namespace Waystone\Workspaces\Engines\Framework;
use Keruald\Database\DatabaseEngine;
+use Psr\SimpleCache\CacheInterface;
use Smarty\Smarty;
use Waystone\Workspaces\Engines\Users\User;
-use Waystone\Workspaces\Engines\Users\UserRepository;
use Waystone\Workspaces\Engines\Workspaces\WorkSpace;
/**
@@ -67,6 +67,8 @@
*/
public Smarty $templateEngine;
+ public CacheInterface $cache;
+
///
/// Helper methods
///
diff --git a/workspaces/src/Engines/Workspaces/Workspace.php b/workspaces/src/Engines/Workspaces/Workspace.php
--- a/workspaces/src/Engines/Workspaces/Workspace.php
+++ b/workspaces/src/Engines/Workspaces/Workspace.php
@@ -24,7 +24,7 @@
use Keruald\OmniTools\Collections\Vector;
-use Cache;
+use Psr\SimpleCache\InvalidArgumentException as InvalidArgumentCacheException;
use Exception;
use LogicException;
@@ -235,28 +235,22 @@
* @param int $user_id The user to get his workspaces
*
* @return Workspace[] A list of workspaces
+ * @throws InvalidArgumentCacheException
*/
public static function get_user_workspaces ($user_id) {
- //Gets the workspaces list from cache, as this complex request could take 100ms
- //and is called on every page.
- $cache = Cache::load();
- $cachedWorkspaces = $cache->get("workspaces-$user_id");
+ global $context;
+ $cache = $context->cache;
- if ($cachedWorkspaces !== null && $cachedWorkspaces !== false) {
- return unserialize($cachedWorkspaces);
- }
-
- $workspaces = self::loadWorkspacesForUser($user_id);
- $cache->set("workspaces-$user_id", serialize($workspaces));
-
- return $workspaces;
+ return $cache->get("workspaces-$user_id") ?? self::loadForUser($user_id);
}
/**
* @return self[]
*/
- private static function loadWorkspacesForUser (int $user_id) : array {
- global $db;
+ private static function loadForUser (int $user_id) : array {
+ global $context;
+ $db = $context->db;
+ $cache = $context->cache;
$clause = User::get_permissions_clause_from_user_id($user_id, $db);
$sql = "SELECT DISTINCT w.*
@@ -271,9 +265,13 @@
"Can't get user workspaces", '', __LINE__, __FILE__, $sql);
}
- return Vector::from($result)
+ $workspaces = Vector::from($result)
->map(fn($row) => self::fromRow($row))
->toArray();
+
+ $cache->set("workspaces-$user_id", $workspaces);
+
+ return $workspaces;
}
/**
diff --git a/workspaces/src/includes/autoload.php b/workspaces/src/includes/autoload.php
--- a/workspaces/src/includes/autoload.php
+++ b/workspaces/src/includes/autoload.php
@@ -48,10 +48,6 @@
/// Keruald and Obsidian Workspaces libraries
///
- if ($name == 'Cache') { require $dir . '/includes/cache/cache.php'; return true; }
- if ($name == 'CacheMemcached') { require $dir . '/includes/cache/memcached.php'; return true; }
- if ($name == 'CacheVoid') { require $dir . '/includes/cache/void.php'; return true; }
-
if ($name == 'Disclaimer') { require $dir . '/includes/objects/Disclaimer.php'; return true; }
if ($name == 'UserGroup') { require $dir . '/includes/objects/usergroup.php'; return true; }
diff --git a/workspaces/src/includes/cache/cache.php b/workspaces/src/includes/cache/cache.php
deleted file mode 100644
--- a/workspaces/src/includes/cache/cache.php
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-/**
- * _, __, _, _ __, _ _, _, _
- * / \ |_) (_ | | \ | /_\ |\ |
- * \ / |_) , ) | |_/ | | | | \|
- * ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- *
- * Cache loader class.
- *
- * This file provides a calling class, which read the configuration, ensures
- * the cache class for the cache engine given in config exists and initializes
- * it.
- *
- * You'll find a sample of implementation in the CacheMemcached.
- * @see CacheMemcached
- *
- * If no caching mechanism, a "blackhole" void cache will be used.
- * @see CacheVoid.
- *
- * The class to call is determined from the following preference:
- * <code>
- * $Config['cache']['engine'] = 'memcached'; //will use CacheMemcached class.
- * </code>
- *
- * @package ObsidianWorkspaces
- * @subpackage Cache
- * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
- * @license http://www.opensource.org/licenses/bsd-license.php BSD
- * @filesource
- *
- */
-
-use Waystone\Workspaces\Engines\Errors\ErrorHandling;
-
-/**
- * Cache caller
- */
-class Cache {
- /**
- * Gets the cache instance, initializing it if needed
- *
- * The correct cache instance to initialize will be determined from the
- * $Config['cache']['engine'] preference.
- *
- * The class cache to use will be Cache + (preference engine, capitalized)
- *
- * This method will create an instance of the specified object,
- * calling the load static method from this object class.
- *
- * Example:
- * <code>
- * $Config['cache']['engine'] = 'quux';
- * $cache = Cache::load(); //Cache:load() will call CacheQuux:load();
- * </code>
- *
- * @return Cache the cache instance
- */
- static function load () {
- global $Config;
- if (
- !array_key_exists('cache', $Config) ||
- !array_key_exists('engine', $Config['cache'])
- ) {
- //cache is not configured or engine is not specified
- $engine = 'void';
- } else {
- //engine is specified in the configuration
- $engine = $Config['cache']['engine'];
- }
-
- $engine_file = 'includes/cache/' . $engine . '.php';
- $engine_class = 'Cache' . ucfirst($engine);
-
- if (!file_exists($engine_file)) {
- ErrorHandling::messageAndDie(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />$engine_file not found.", 'Cache');
- }
-
- require_once($engine_file);
- if (!class_exists($engine_class)) {
- ErrorHandling::messageAndDie(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />$engine_class class not found.", 'Cache');
- }
-
- return call_user_func(array($engine_class, 'load'));
- }
-}
diff --git a/workspaces/src/includes/cache/memcached.php b/workspaces/src/includes/cache/memcached.php
deleted file mode 100644
--- a/workspaces/src/includes/cache/memcached.php
+++ /dev/null
@@ -1,106 +0,0 @@
-<?php
-
-/**
- * _, __, _, _ __, _ _, _, _
- * / \ |_) (_ | | \ | /_\ |\ |
- * \ / |_) , ) | |_/ | | | | \|
- * ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- *
- * Memcached cache.
- *
- * @package ObsidianWorkspaces
- * @subpackage Cache
- * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
- * @license http://www.opensource.org/licenses/bsd-license.php BSD
- * @filesource
- *
- */
-
-use Waystone\Workspaces\Engines\Errors\ErrorHandling;
-
-/**
- * Memcached cache
- *
- * !!! This class uses the Memcached extension AND NOT the Memcache ext !!!!
- *
- * References:
- * @link http://www.php.net/manual/en/book/memcached.php
- * @link http://memcached.org
- *
- * This class implements a singleton pattern.
- */
-class CacheMemcached {
-
- /**
- * The current cache instance
- *
- * @var CacheMemcached
- */
- static $instance = null;
-
- /**
- * The Memcached object
- *
- * @var Memcached
- */
- private $memcached = null;
-
- /**
- * Gets the cache instance, initializing it if needed
- *
- * @return Cache the cache instance, or null if nothing is cached
- */
- static function load () {
- //Checks extension is okay
- if (!extension_loaded('memcached')) {
- if (extension_loaded('memcache')) {
- ErrorHandling::messageAndDie(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />PHP extension memcached not loaded.<br /><strong>!!! This class uses the Memcached extension AND NOT the Memcache extension (this one is loaded) !!!</strong>", 'Cache');
- } else {
- ErrorHandling::messageAndDie(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />PHP extension memcached not loaded.", 'Cache');
- }
- }
-
- //Creates the Memcached object if needed
- if (self::$instance === null) {
- global $Config;
-
- self::$instance = new CacheMemcached();
- self::$instance->memcached = new Memcached();
- self::$instance->memcached->addServer(
- $Config['cache']['server'],
- $Config['cache']['port']
- );
- }
-
- return self::$instance;
- }
-
- /**
- * Gets the specified key's data
- *
- * @param string $key the key to fetch
- * @return mixed the data at the specified key
- */
- function get ($key) {
- return $this->memcached->get($key);
- }
-
- /**
- * Sets the specified data at the specified key
- *
- * @param string $key the key where to store the specified data
- * @param mixed $value the data to store
- */
- function set ($key, $value) {
- return $this->memcached->set($key, $value);
- }
-
- /**
- * Deletes the specified key's data
- *
- * @param string $key the key to delete
- */
- function delete ($key) {
- return $this->memcached->delete($key);
- }
-}
diff --git a/workspaces/src/includes/cache/void.php b/workspaces/src/includes/cache/void.php
deleted file mode 100644
--- a/workspaces/src/includes/cache/void.php
+++ /dev/null
@@ -1,73 +0,0 @@
-<?php
-
-/**
- * _, __, _, _ __, _ _, _, _
- * / \ |_) (_ | | \ | /_\ |\ |
- * \ / |_) , ) | |_/ | | | | \|
- * ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- *
- * "Blackhole" void cache.
- *
- * @package ObsidianWorkspaces
- * @subpackage Cache
- * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
- * @license http://www.opensource.org/licenses/bsd-license.php BSD
- * @filesource
- *
- */
-
-/**
- * "blackhole" void cache
- *
- * This class doesn't cache information, it's a void wrapper
- * get will always return null
- * set and delete do nothing
- *
- * It will be used by default if no cache is specified.
- *
- * This class implements a singleton pattern.
- */
-class CacheVoid {
- /**
- * @var CacheVoid the current cache instance
- */
- static $instance = null;
-
- /**
- * Gets the cache instance, initializing it if needed
- *
- * @return Cache the cache instance, or null if nothing is cached
- */
- static function load () {
- if (self::$instance === null) {
- self::$instance = new CacheVoid();
- }
-
- return self::$instance;
- }
-
- /**
- * Gets the specified key's data
- *
- * @param string $key the key to fetch
- * @return mixed the data at the specified key
- */
- function get ($key) {
- return null;
- }
-
- /**
- * Sets the specified data at the specified key
- *
- * @param string $key the key where to store the specified data
- * @param mixed $value the data to store
- */
- function set ($key, $value) { }
-
- /**
- * Deletes the specified key's data
- *
- * @param string $key the key to delete
- */
- function delete ($key) { }
-}
diff --git a/workspaces/src/includes/config.php b/workspaces/src/includes/config.php
--- a/workspaces/src/includes/config.php
+++ b/workspaces/src/includes/config.php
@@ -19,6 +19,7 @@
*
*/
+use Keruald\Cache\Engines\CacheVoid;
use Keruald\Database\Engines\MySQLiEngine;
////////////////////////////////////////////////////////////////////////////////
@@ -258,15 +259,15 @@
* data from heavy database queries, or frequently accessed stuff.
*
* To use memcached:
- * - $Config['cache']['engine'] = 'memcached';
+ * - $Config['cache']['engine'] = CacheMemcached::class;
* - $Config['cache']['server'] = 'localhost';
* - $Config['cache']['port'] = 11211;
*
* To disable cache:
- * - $Config['cache']['engine'] = 'void';
+ * - $Config['cache']['engine'] = CacheVoid::class;
* (or omit the cache key)
*/
-$Config['cache']['engine'] = 'void';
+$Config['cache']['engine'] = CacheVoid::class;
////////////////////////////////////////////////////////////////////////////////
/// ///

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 16, 09:01 (15 m, 54 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3173217
Default Alt Text
D3865.id.diff (14 KB)

Event Timeline