Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-36768 cache: Implemented abstract cache store base class

  • Loading branch information...
commit bd188851f2b5c4e525ad4e7a10f18e5df9c64039 1 parent f42c34a
@samhemelryk samhemelryk authored
View
1  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._
View
83 cache/classes/store.php
@@ -0,0 +1,83 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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;
+ }
+}
View
1  cache/lib.php
@@ -36,6 +36,7 @@
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');
/**
View
38 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.
@@ -205,33 +205,6 @@ public static function is_supported_mode($mode) {
}
/**
- * 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.
*
* Once this has been done the cache is all set to be used.
@@ -594,15 +567,6 @@ protected function ensure_path_exists() {
}
/**
- * 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.
*
* 1. Purges the cache directory.
View
38 cache/stores/memcache/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_memcache implements cache_store {
+class cachestore_memcache extends cache_store_base {
/**
* The name of the store
@@ -174,33 +174,6 @@ public static function get_supported_features(array $configuration = array()) {
}
/**
- * 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.
*
* @param array $configuration
@@ -344,15 +317,6 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
}
/**
- * 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.
*/
public function cleanup() {
View
38 cache/stores/memcached/lib.php
@@ -43,7 +43,7 @@
* @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
@@ -200,33 +200,6 @@ public static function get_supported_features(array $configuration = array()) {
}
/**
- * 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.
*
* @param array $configuration
@@ -427,15 +400,6 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
}
/**
- * 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.
*/
public function cleanup() {
View
26 cache/stores/mongodb/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_mongodb implements cache_store {
+class cachestore_mongodb extends cache_store_base {
/**
* The name of the store
@@ -142,14 +142,6 @@ public static function are_requirements_met() {
}
/**
- * 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
* @return int
@@ -219,14 +211,6 @@ public static function is_supported_mode($mode) {
}
/**
- * 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
*/
@@ -235,14 +219,6 @@ public function supports_multiple_identifiers() {
}
/**
- * 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.
*
* @param string $key The key to retrieve
View
31 cache/stores/session/lib.php
@@ -34,7 +34,7 @@
* @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
@@ -114,33 +114,6 @@ public static function is_supported_mode($mode) {
}
/**
- * 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.
*
* Once this has been done the cache is all set to be used.
@@ -378,7 +351,7 @@ public function my_name() {
* @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.
View
29 cache/stores/static/lib.php
@@ -114,33 +114,6 @@ public static function is_supported_mode($mode) {
}
/**
- * 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.
*
* Once this has been done the cache is all set to be used.
@@ -378,7 +351,7 @@ public function my_name() {
* @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.
Please sign in to comment.
Something went wrong with that request. Please try again.