error handler improvements
[simplicity.git] / source / lib / simplicity / core / cache / cache.php
blob72ad751dd1bd8d414ed02b4f1bedf41e960719b0
1 <?php
3 /**
4 * Cache Module
6 * The Simplicity Cache module is a core module providing a cache abstraction layer to Simplicity
7 * and other modules.
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
15 * @smp_core
17 class smp_CacheModule extends smp_Module
20 private $_ns;
22 /**
23 * smp_CacheBackend
25 * @var smp_CacheBackend
27 private $_backend;
29 /**
30 * @inherited
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());
43 /**
44 * @inherited
46 public function exec($args=array())
48 $this->loadBackend();
51 /**
52 * loadBackend
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();
65 $beds = array();
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);
84 /**
85 * get
87 * Retrieve the given key $name from the cache.
89 * @param $name string
90 * @return mixed
92 public function get($name)
94 $name = "{$this->_ns}.$name";
95 return $this->_backend->get($name);
98 /**
99 * set
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)
109 if (!$name) return;
110 $name = "{$this->_ns}.$name";
111 return $this->_backend->set($name,$value,$ttl);
115 * add
117 * Set the given key only if it does not exist.
119 * @see set
120 * @param $name string
121 * @param $value mixed
122 * @param $ttl integer
124 public function add($name,$value,$ttl=0)
126 if (!$name) return;
127 $name = "{$this->_ns}.$name";
128 return $this->_backend->add($name,$value,$ttl);
132 * del
134 * Delete the given key $name.
136 * @param $name string
138 public function del($name)
140 if (!$name) return;
141 $name = "{$this->_ns}.$name";
142 return $this->_backend->del($name);
146 * has
148 * Returns true or false if the given key $name is in the cache.
150 * @param $name string
151 * @return boolean
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();