123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * \ElggCache The elgg cache superclass.
- * This defines the interface for a cache (wherever that cache is stored).
- *
- * @package Elgg.Core
- * @subpackage Cache
- */
- abstract class ElggCache implements \ArrayAccess {
- /**
- * Variables for the cache object.
- *
- * @var array
- */
- private $variables;
- /**
- * Set the constructor.
- */
- public function __construct() {
- $this->variables = array();
- }
- // @codingStandardsIgnoreStart
- /**
- * Set a cache variable.
- *
- * @param string $variable Name
- * @param string $value Value
- *
- * @return void
- *
- * @deprecated 1.8 Use \ElggCache:setVariable()
- */
- public function set_variable($variable, $value) {
- elgg_deprecated_notice('\ElggCache::set_variable() is deprecated by \ElggCache::setVariable()', 1.8);
- $this->setVariable($variable, $value);
- }
- // @codingStandardsIgnoreEnd
- /**
- * Set a cache variable.
- *
- * @param string $variable Name
- * @param string $value Value
- *
- * @return void
- */
- public function setVariable($variable, $value) {
- if (!is_array($this->variables)) {
- $this->variables = array();
- }
- $this->variables[$variable] = $value;
- }
- // @codingStandardsIgnoreStart
- /**
- * Get variables for this cache.
- *
- * @param string $variable Name
- *
- * @return mixed The value or null;
- *
- * @deprecated 1.8 Use \ElggCache::getVariable()
- */
- public function get_variable($variable) {
- elgg_deprecated_notice('\ElggCache::get_variable() is deprecated by \ElggCache::getVariable()', 1.8);
- return $this->getVariable($variable);
- }
- // @codingStandardsIgnoreEnd
- /**
- * Get variables for this cache.
- *
- * @param string $variable Name
- *
- * @return mixed The variable or null;
- */
- public function getVariable($variable) {
- if (isset($this->variables[$variable])) {
- return $this->variables[$variable];
- }
- return null;
- }
- /**
- * Class member get overloading, returning key using $this->load defaults.
- *
- * @param string $key Name
- *
- * @return mixed
- */
- public function __get($key) {
- return $this->load($key);
- }
- /**
- * Class member set overloading, setting a key using $this->save defaults.
- *
- * @param string $key Name
- * @param mixed $value Value
- *
- * @return void
- */
- public function __set($key, $value) {
- $this->save($key, $value);
- }
- /**
- * Supporting isset, using $this->load() with default values.
- *
- * @param string $key The name of the attribute or metadata.
- *
- * @return bool
- */
- public function __isset($key) {
- return (bool)$this->load($key);
- }
- /**
- * Supporting unsetting of magic attributes.
- *
- * @param string $key The name of the attribute or metadata.
- *
- * @return bool
- */
- public function __unset($key) {
- return $this->delete($key);
- }
- /**
- * Save data in a cache.
- *
- * @param string $key Name
- * @param string $data Value
- *
- * @return bool
- */
- abstract public function save($key, $data);
- /**
- * Load data from the cache using a given key.
- *
- * @todo $offset is a horrible variable name because it creates confusion
- * with the \ArrayAccess methods
- *
- * @param string $key Name
- * @param int $offset Offset
- * @param int $limit Limit
- *
- * @return mixed The stored data or false.
- */
- abstract public function load($key, $offset = 0, $limit = null);
- /**
- * Invalidate a key
- *
- * @param string $key Name
- *
- * @return bool
- */
- abstract public function delete($key);
- /**
- * Clear out all the contents of the cache.
- *
- * @return bool
- */
- abstract public function clear();
- /**
- * Add a key only if it doesn't already exist.
- * Implemented simply here, if you extend this class and your caching engine
- * provides a better way then override this accordingly.
- *
- * @param string $key Name
- * @param string $data Value
- *
- * @return bool
- */
- public function add($key, $data) {
- if (!isset($this[$key])) {
- return $this->save($key, $data);
- }
- return false;
- }
- // ARRAY ACCESS INTERFACE //////////////////////////////////////////////////////////
- /**
- * Assigns a value for the specified key
- *
- * @see \ArrayAccess::offsetSet()
- *
- * @param mixed $key The key (offset) to assign the value to.
- * @param mixed $value The value to set.
- *
- * @return void
- */
- public function offsetSet($key, $value) {
- $this->save($key, $value);
- }
- /**
- * Get the value for specified key
- *
- * @see \ArrayAccess::offsetGet()
- *
- * @param mixed $key The key (offset) to retrieve.
- *
- * @return mixed
- */
- public function offsetGet($key) {
- return $this->load($key);
- }
- /**
- * Unsets a key.
- *
- * @see \ArrayAccess::offsetUnset()
- *
- * @param mixed $key The key (offset) to unset.
- *
- * @return void
- */
- public function offsetUnset($key) {
- if (isset($this->$key)) {
- unset($this->$key);
- }
- }
- /**
- * Does key exist
- *
- * @see \ArrayAccess::offsetExists()
- *
- * @param mixed $key A key (offset) to check for.
- *
- * @return bool
- */
- public function offsetExists($key) {
- return isset($this->$key);
- }
- }
|