From bd188851f2b5c4e525ad4e7a10f18e5df9c64039 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Fri, 23 Nov 2012 10:32:51 +1300 Subject: [PATCH] MDL-36768 cache: Implemented abstract cache store base class --- cache/README.md | 1 + cache/classes/store.php | 83 ++++++++++++++++++++++++++++++++++ cache/lib.php | 1 + cache/stores/file/lib.php | 38 +--------------- cache/stores/memcache/lib.php | 38 +--------------- cache/stores/memcached/lib.php | 38 +--------------- cache/stores/mongodb/lib.php | 26 +---------- cache/stores/session/lib.php | 31 +------------ cache/stores/static/lib.php | 29 +----------- 9 files changed, 92 insertions(+), 193 deletions(-) create mode 100644 cache/classes/store.php diff --git a/cache/README.md b/cache/README.md index 408e014a9f6..fc8309d130e 100644 --- a/cache/README.md +++ b/cache/README.md @@ -89,6 +89,7 @@ The following points highlight things you should know about stores. ** Data guarantee - Data is guaranteed to exist in the cache once it is set there. It is never cleaned up to free space or because it has not been recently used. ** Multiple identifiers - Rather than a single string key, the parts that make up the key are passed as an array. ** Native TTL support - When required, the store supports native ttl and doesn't require the cache API to manage ttl of things given to the store. +* There are two reserved store names, base and dummy. These are both used internally. ### Definition _Definitions were not a part of the previous proposal._ diff --git a/cache/classes/store.php b/cache/classes/store.php new file mode 100644 index 00000000000..fd4d0963584 --- /dev/null +++ b/cache/classes/store.php @@ -0,0 +1,83 @@ +. + +/** + * Cache store - base class + * + * This file is part of Moodle's cache API, affectionately called MUC. + * It contains the components that are required in order to use caching. + * + * @package core + * @category cache + * @copyright 2012 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Abstract cache store base class. + * + * This class implements the cache_store interface that all caches store plugins are required in implement. + * It then implements basic methods that likely won't need to be overridden by plugins. + * It will also be used to implement any API changes that come about in the future. + * + * While it is not required that you extend this class it is highly recommended. + * + * @since 2.4 + * @package core + * @category cache + * @copyright 2012 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class cache_store_base implements cache_store { + + /** + * Returns true if the user can add an instance of the store plugin. + * + * @return bool + */ + public static function can_add_instance() { + return true; + } + + /** + * Returns true if the store instance guarantees data. + * + * @return bool + */ + public function supports_data_guarantee() { + return $this::get_supported_features() & self::SUPPORTS_DATA_GUARANTEE; + } + + /** + * Returns true if the store instance supports multiple identifiers. + * + * @return bool + */ + public function supports_multiple_identifiers() { + return $this::get_supported_features() & self::SUPPORTS_MULTIPLE_IDENTIFIERS; + } + + /** + * Returns true if the store instance supports native ttl. + * + * @return bool + */ + public function supports_native_ttl() { + return $this::supports_data_guarantee() & self::SUPPORTS_NATIVE_TTL; + } +} \ No newline at end of file diff --git a/cache/lib.php b/cache/lib.php index b5e23ade97f..001cd431439 100644 --- a/cache/lib.php +++ b/cache/lib.php @@ -36,6 +36,7 @@ require_once($CFG->dirroot.'/cache/classes/config.php'); require_once($CFG->dirroot.'/cache/classes/helper.php'); require_once($CFG->dirroot.'/cache/classes/factory.php'); require_once($CFG->dirroot.'/cache/classes/loaders.php'); +require_once($CFG->dirroot.'/cache/classes/store.php'); require_once($CFG->dirroot.'/cache/classes/definition.php'); /** diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index 19fdfb6b83d..aad565b1810 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -37,7 +37,7 @@ * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class cachestore_file implements cache_store, cache_is_key_aware { +class cachestore_file extends cache_store_base implements cache_is_key_aware { /** * The name of the store. @@ -204,33 +204,6 @@ class cachestore_file implements cache_store, cache_is_key_aware { return ($mode === self::MODE_APPLICATION || $mode === self::MODE_SESSION); } - /** - * Returns true if the store instance supports multiple identifiers. - * - * @return bool - */ - public function supports_multiple_identifiers() { - return false; - } - - /** - * Returns true if the store instance guarantees data. - * - * @return bool - */ - public function supports_data_guarantee() { - return true; - } - - /** - * Returns true if the store instance supports native ttl. - * - * @return bool - */ - public function supports_native_ttl() { - return true; - } - /** * Initialises the cache. * @@ -593,15 +566,6 @@ class cachestore_file implements cache_store, cache_is_key_aware { return true; } - /** - * Returns true if the user can add an instance of the store plugin. - * - * @return bool - */ - public static function can_add_instance() { - return true; - } - /** * Performs any necessary clean up when the store instance is being deleted. * diff --git a/cache/stores/memcache/lib.php b/cache/stores/memcache/lib.php index d62fac7bd3e..2c373c0af22 100644 --- a/cache/stores/memcache/lib.php +++ b/cache/stores/memcache/lib.php @@ -37,7 +37,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class cachestore_memcache implements cache_store { +class cachestore_memcache extends cache_store_base { /** * The name of the store @@ -173,33 +173,6 @@ class cachestore_memcache implements cache_store { return self::SUPPORTS_NATIVE_TTL; } - /** - * Returns true if the store instance supports multiple identifiers. - * - * @return bool - */ - public function supports_multiple_identifiers() { - return false; - } - - /** - * Returns true if the store instance guarantees data. - * - * @return bool - */ - public function supports_data_guarantee() { - return false; - } - - /** - * Returns true if the store instance supports native ttl. - * - * @return bool - */ - public function supports_native_ttl() { - return true; - } - /** * Returns the supported modes as a combined int. * @@ -343,15 +316,6 @@ class cachestore_memcache implements cache_store { $editform->set_data($data); } - /** - * Returns true if the user can add an instance of the store plugin. - * - * @return bool - */ - public static function can_add_instance() { - return true; - } - /** * Performs any necessary clean up when the store instance is being deleted. */ diff --git a/cache/stores/memcached/lib.php b/cache/stores/memcached/lib.php index 419e640c8c7..2da37b03c61 100644 --- a/cache/stores/memcached/lib.php +++ b/cache/stores/memcached/lib.php @@ -43,7 +43,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class cachestore_memcached implements cache_store { +class cachestore_memcached extends cache_store_base { /** * The name of the store @@ -199,33 +199,6 @@ class cachestore_memcached implements cache_store { return self::SUPPORTS_NATIVE_TTL; } - /** - * Returns true if the store instance supports multiple identifiers. - * - * @return bool - */ - public function supports_multiple_identifiers() { - return false; - } - - /** - * Returns true if the store instance guarantees data. - * - * @return bool - */ - public function supports_data_guarantee() { - return false; - } - - /** - * Returns true if the store instance supports native ttl. - * - * @return bool - */ - public function supports_native_ttl() { - return true; - } - /** * Returns the supported modes as a combined int. * @@ -426,15 +399,6 @@ class cachestore_memcached implements cache_store { $editform->set_data($data); } - /** - * Returns true if the user can add an instance of the store plugin. - * - * @return bool - */ - public static function can_add_instance() { - return true; - } - /** * Performs any necessary clean up when the store instance is being deleted. */ diff --git a/cache/stores/mongodb/lib.php b/cache/stores/mongodb/lib.php index 1931bdbfc36..c2f2eb820e1 100644 --- a/cache/stores/mongodb/lib.php +++ b/cache/stores/mongodb/lib.php @@ -37,7 +37,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class cachestore_mongodb implements cache_store { +class cachestore_mongodb extends cache_store_base { /** * The name of the store @@ -141,14 +141,6 @@ class cachestore_mongodb implements cache_store { return class_exists('Mongo'); } - /** - * Returns true if the user can add an instance of this store. - * @return bool - */ - public static function can_add_instance() { - return true; - } - /** * Returns the supported features. * @param array $configuration @@ -218,14 +210,6 @@ class cachestore_mongodb implements cache_store { return ($mode == self::MODE_APPLICATION || $mode == self::MODE_SESSION); } - /** - * Returns true if this store guarantees its data is there once set. - * @return bool - */ - public function supports_data_guarantee() { - return true; - } - /** * Returns true if this store is making use of multiple identifiers. * @return bool @@ -234,14 +218,6 @@ class cachestore_mongodb implements cache_store { return $this->extendedmode; } - /** - * Returns true if this store supports native TTL. - * @return bool - */ - public function supports_native_ttl() { - return false; - } - /** * Retrieves an item from the cache store given its key. * diff --git a/cache/stores/session/lib.php b/cache/stores/session/lib.php index ba82c17c46b..e8e25ea62dc 100644 --- a/cache/stores/session/lib.php +++ b/cache/stores/session/lib.php @@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class cachestore_session extends session_data_store implements cache_store, cache_is_key_aware { +class cachestore_session extends session_data_store implements cache_is_key_aware { /** * The name of the store @@ -113,33 +113,6 @@ class cachestore_session extends session_data_store implements cache_store, cach return ($mode === self::MODE_SESSION); } - /** - * Returns true if the store instance guarantees data. - * - * @return bool - */ - public function supports_data_guarantee() { - return true; - } - - /** - * Returns true if the store instance supports multiple identifiers. - * - * @return bool - */ - public function supports_multiple_identifiers() { - return false; - } - - /** - * Returns true if the store instance supports native ttl. - * - * @return bool - */ - public function supports_native_ttl() { - return true; - } - /** * Initialises the cache. * @@ -378,7 +351,7 @@ class cachestore_session extends session_data_store implements cache_store, cach * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -abstract class session_data_store { +abstract class session_data_store extends cache_store_base { /** * Used for the actual storage. diff --git a/cache/stores/static/lib.php b/cache/stores/static/lib.php index 7a7bd5a3fc9..4d62a318193 100644 --- a/cache/stores/static/lib.php +++ b/cache/stores/static/lib.php @@ -113,33 +113,6 @@ class cachestore_static extends static_data_store implements cache_store, cache_ return ($mode === self::MODE_REQUEST); } - /** - * Returns true if the store instance guarantees data. - * - * @return bool - */ - public function supports_data_guarantee() { - return true; - } - - /** - * Returns true if the store instance supports multiple identifiers. - * - * @return bool - */ - public function supports_multiple_identifiers() { - return false; - } - - /** - * Returns true if the store instance supports native ttl. - * - * @return bool - */ - public function supports_native_ttl() { - return true; - } - /** * Initialises the cache. * @@ -378,7 +351,7 @@ class cachestore_static extends static_data_store implements cache_store, cache_ * @copyright 2012 Sam Hemelryk * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -abstract class static_data_store { +abstract class static_data_store extends cache_store_base { /** * An array for storage. -- 2.17.1