diff --git a/src/Pluf/Cache/Memcached.php b/src/Pluf/Cache/Memcached.php new file mode 100644 index 0000000..91adcfd --- /dev/null +++ b/src/Pluf/Cache/Memcached.php @@ -0,0 +1,93 @@ +memcache = memcache_connect(Pluf::f('cache_memcached_server', 'localhost'), + Pluf::f('cache_memcached_port', 11211)); + if (false == $this->memcache) { + $this->memcache = null; + } + $this->keyprefix = Pluf::f('cache_memcached_keyprefix', ''); + } + + /** + * Set a value in the cache. + * + * @param string Key to store the information + * @param mixed Value to store + * @param int Timeout in seconds (null) + * @return bool Success + */ + public function set($key, $value, $timeout=null) + { + if ($this->memcache) { + if ($timeout == null) $timeout = Pluf::f('cache_timeout', 300); + $this->memcache->set($this->keyprefix.$key, serialize($value), + Pluf::f('cache_memcached_compress', 0), + $timeout); + } + } + + /** + * Get value from the cache. + * + * @param string Key to get the information + * @param mixed Default value to return if cache miss (null) + * @param mixed Stored value or default + */ + public function get($key, $default=null) + { + if ($this->memcache) { + $val = $this->memcache->get($this->keyprefix.$key); + if (false === $val) { + return $default; + } else { + return unserialize($val); + } + } else { + return $default; + } + } +}