Permalink
Browse files

MDL-36768 cache: Implemented abstract cache store base class

  • Loading branch information...
1 parent f42c34a commit bd188851f2b5c4e525ad4e7a10f18e5df9c64039 Sam Hemelryk committed Nov 22, 2012
View
@@ -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
@@ -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
@@ -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
@@ -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 @@ public static function is_supported_mode($mode) {
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 @@ protected function ensure_path_exists() {
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.
*
@@ -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
@@ -173,33 +173,6 @@ public static function get_supported_features(array $configuration = array()) {
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 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
$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.
*/
@@ -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
@@ -199,33 +199,6 @@ public static function get_supported_features(array $configuration = array()) {
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 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
$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.
*/
@@ -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
@@ -141,14 +141,6 @@ public static function are_requirements_met() {
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 @@ public static function is_supported_mode($mode) {
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 @@ public function supports_multiple_identifiers() {
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.
*
@@ -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
@@ -113,33 +113,6 @@ public static function is_supported_mode($mode) {
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 @@ 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.
Oops, something went wrong.

0 comments on commit bd18885

Please sign in to comment.