Page MenuHomeDevCentral

No OneTemporary

diff --git a/includes/cache/cache.php b/includes/cache/cache.php
new file mode 100644
index 0000000..fbc36d9
--- /dev/null
+++ b/includes/cache/cache.php
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * Cache calling class.
+ *
+ * Zed. The immensity of stars. The HyperShip. The people.
+ *
+ * (c) 2010, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
+ * This file provides a calling class, which read the configuration, ensures
+ * the cache class for the cache engine given in config exists and initializes
+ * it.
+ *
+ * You'll find a sample of implementation in the CacheMemcached.
+ * @see CacheMemcached
+ *
+ * If no caching mechanism, a "blackhole" void cache will be used.
+ * @see CacheVoid.
+ *
+ * The class to call is determined from the following preference:
+ * <code>
+ * $Config['cache']['engine'] = 'memcached'; //will use CacheMemcached class.
+ * </code>
+ *
+ * 0.1 2010-07-06 22:45 Initial version [DcK]
+ *
+ * @package Zed
+ * @subpackage Cache
+ * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @copyright 2010 Sébastien Santoro aka Dereckson
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @version 0.1
+ * @link http://scherzo.dereckson.be/doc/zed
+ * @link http://zed.dereckson.be/
+ * @filesource
+ */
+
+/**
+ * Cache caller
+ */
+class Cache {
+ /**
+ * Gets the cache instance, initializing it if needed
+ *
+ * The correct cache instance to initialize will be determined from the
+ * $Config['cache']['engine'] preference.
+ *
+ * The class cache to use will be Cache + (preference engine, capitalized)
+ *
+ * This method will creates an instance of the specified object,
+ * calling the load static method from this object class.
+ *
+ * Example:
+ * <code>
+ * $Config['cache']['engine'] = 'quux';
+ * $cache = Cache::load(); //Cache:load() will call CacheQuux:load();
+ * </code>
+ *
+ * @return Cache the cache instance, or null if nothing is cached
+ */
+ static function load () {
+ global $Config;
+ if (
+ !array_key_exists('cache', $Config) ||
+ !array_key_exists('engine', $Config['cache'])
+ ) {
+ //cache is not configured or engine is not specified
+ $engine = 'void';
+ } else {
+ //engine is specified in the configuration
+ $engine = $Config['cache']['engine'];
+ }
+
+ $engine_file = 'includes/cache/' . $engine . '.php';
+ $engine_class = 'Cache' . ucfirst($engine);
+
+ if (!file_exists($engine_file)) {
+ message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />$engine_file not found.", 'Cache');
+ }
+
+ require_once($engine_file);
+ if (!class_exists($engine_class)) {
+ message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />$engine_class class not found.", 'Cache');
+ }
+
+ return call_user_func(array($engine_class, 'load'));
+ }
+}
+
+?>
diff --git a/includes/cache/memcached.php b/includes/cache/memcached.php
new file mode 100644
index 0000000..6193a16
--- /dev/null
+++ b/includes/cache/memcached.php
@@ -0,0 +1,112 @@
+<?php
+
+/**
+ * Memcached cache.
+ *
+ * Zed. The immensity of stars. The HyperShip. The people.
+ *
+ * (c) 2010, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
+ * Cache class: memcached
+ *
+ * 0.1 2010-07-06 22:55 Initial version [DcK]
+ *
+ * @package Zed
+ * @subpackage Cache
+ * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @copyright 2010 Sébastien Santoro aka Dereckson
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @version 0.1
+ * @link http://scherzo.dereckson.be/doc/zed
+ * @link http://zed.dereckson.be/
+ * @filesource
+ */
+
+/**
+ * Memcached cache
+ *
+ * !!! This class uses the Memcached extension AND NOT the Memcache ext !!!!
+ *
+ * References:
+ * @link http://www.php.net/manual/en/book/memcached.php
+ * @link http://memcached.org
+ *
+ * This class implements a singleton pattern.
+ */
+class CacheMemcached {
+
+ /**
+ * The current cache instance
+ *
+ * @var CacheMemcached
+ */
+ static $instance = null;
+
+ /**
+ * The Memcached object
+ *
+ * @var Memcached
+ */
+ private $memcached = null;
+
+ /**
+ * Gets the cache instance, initializing it if needed
+ *
+ * @return Cache the cache instance, or null if nothing is cached
+ */
+ static function load () {
+ //Checks extension is okay
+ if (!extension_loaded('memcached')) {
+ if (extension_loaded('memcache')) {
+ message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />PHP extension memcached not loaded.<br /><strong>!!! This class uses the Memcached extension AND NOT the Memcache extension (this one is loaded) !!!</strong>", 'Cache');
+ } else {
+ message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />PHP extension memcached not loaded.", 'Cache');
+ }
+ }
+
+ //Creates the Memcached object if needed
+ if (self::$instance === null) {
+ global $Config;
+
+ self::$instance = new CacheMemcached();
+ self::$instance->memcached = new Memcached();
+ self::$instance->memcached->addServer(
+ $Config['cache']['server'],
+ $Config['cache']['port']
+ );
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Gets the specified key's data
+ *
+ * @param string $key the key to fetch
+ * @return mixed the data at the specified key
+ */
+ function get ($key) {
+ return $this->memcached->get($key);
+ }
+
+ /**
+ * Sets the specified data at the specified key
+ *
+ * @param string $key the key where to store the specified data
+ * @param mixed $value the data to store
+ */
+ function set ($key, $value) {
+ return $this->memcached->set($key, $value);
+ }
+
+ /**
+ * Deletes the specified key's data
+ *
+ * @param string $key the key to delete
+ */
+ function delete ($key) {
+ return $this->memcached->delete($key);
+ }
+}
+?>
\ No newline at end of file
diff --git a/includes/cache/void.php b/includes/cache/void.php
new file mode 100644
index 0000000..2d203fd
--- /dev/null
+++ b/includes/cache/void.php
@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * "Blackhole" void cache.
+ *
+ * Zed. The immensity of stars. The HyperShip. The people.
+ *
+ * (c) 2010, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
+ * Cache class: void
+ *
+ * 0.1 2010-07-06 22:55 Initial version [DcK]
+ *
+ * @package Zed
+ * @subpackage Cache
+ * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @copyright 2010 Sébastien Santoro aka Dereckson
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @version 0.1
+ * @link http://scherzo.dereckson.be/doc/zed
+ * @link http://zed.dereckson.be/
+ * @filesource
+ */
+
+/**
+ * "blackhole" void cache
+ *
+ * This class doesn't cache information, it'a void wrapper
+ * get will always return null
+ * set and delete do nothing
+ *
+ * It will be used by default if no cache is specified.
+ *
+ * This class implements a singleton pattern.
+ */
+class CacheVoid {
+ /**
+ * @var CacheVoid the current cache instance
+ */
+ static $instance = null;
+
+ /**
+ * Gets the cache instance, initializing it if needed
+ *
+ * @return Cache the cache instance, or null if nothing is cached
+ */
+ static function load () {
+ if (self::$instance === null) {
+ self::$instance = new CacheVoid();
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Gets the specified key's data
+ *
+ * @param string $key the key to fetch
+ * @return mixed the data at the specified key
+ */
+ function get ($key) {
+ return null;
+ }
+
+ /**
+ * Sets the specified data at the specified key
+ *
+ * @param string $key the key where to store the specified data
+ * @param mixed $value the data to store
+ */
+ function set ($key, $value) { }
+
+ /**
+ * Deletes the specified key's data
+ *
+ * @param string $key the key to delete
+ */
+ function delete ($key) { }
+}
+?>
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 17:29 (10 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260644
Default Alt Text
(8 KB)

Event Timeline