6 * The Simplicity Cache module is a core module providing a cache abstraction layer to Simplicity
9 * @author John Le Drew <jp@antz29.com>
10 * @copyright Copyright (c) 2009, John Le Drew
11 * @license http://www.opensource.org/licenses/mit-license.php MIT License
12 * @version 2.0.0-alpha
14 * @smp_children smp_CacheBackend
17 class smp_CacheModule
extends smp_Module
25 * @var smp_CacheBackend
32 public function init()
34 $this->createExtensionPoint('smp_CacheBackend');
35 $this->registerPluginPrefix('smp_');
37 $this->_ns
= md5(smp_Enviroment
::getInstance()->getServer('SCRIPT_FILENAME').__FILE__
);
39 $this->setDefault('backend.module','auto');
40 $this->setDefault('backend.options',array());
46 public function exec($args=array())
54 * Load the selected backend, or automatically load the backend module based on their loadPriority.
57 private function loadBackend()
59 $backend = $this->getConfig('backend.module');
60 $options = $this->getConfig('backend.options');
62 if ($backend == 'auto')
64 $backends = $this->getModules();
66 foreach ($backends as $module => $class)
68 if ($pri = call_user_func(array($class,'loadPriority')))
70 $beds[$module] = $pri;
74 arsort($beds,SORT_NUMERIC
);
76 $backend = key($beds);
79 if (!$class = $this->getModuleClass($backend)) throw new Exception("Unknown cache backend {$backend}");
81 $this->_backend
= new $class($options);
87 * Retrieve the given key $name from the cache.
92 public function get($name)
94 $name = "{$this->_ns}.$name";
95 return $this->_backend
->get($name);
101 * Set the key $name to the given $value, optionally providing a timeout $ttl in seconds (defaults to 0; no timeout)
103 * @param $name string
104 * @param $value mixed
105 * @param $ttl integer
107 public function set($name,$value,$ttl=0)
110 $name = "{$this->_ns}.$name";
111 return $this->_backend
->set($name,$value,$ttl);
117 * Set the given key only if it does not exist.
120 * @param $name string
121 * @param $value mixed
122 * @param $ttl integer
124 public function add($name,$value,$ttl=0)
127 $name = "{$this->_ns}.$name";
128 return $this->_backend
->add($name,$value,$ttl);
134 * Delete the given key $name.
136 * @param $name string
138 public function del($name)
141 $name = "{$this->_ns}.$name";
142 return $this->_backend
->del($name);
148 * Returns true or false if the given key $name is in the cache.
150 * @param $name string
153 public function has($name)
155 $name = "{$this->_ns}.$name";
156 return $this->_backend
->has($name);
159 public function clear()
161 return $this->_backend
->clear();