Memcached wrapper for PHP

<?php
/**
 * Wrapper for storing and retrieving memcached data.
 *
 * Start memcached by running the following command:
 * memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211
 * 
 */
class Cache_Main {
 
	/** @var int */
	const MEMCACHE_PORT = 11211;
 
	/** @var string */
	const MEMCACHE_HOST = '127.0.0.1';
 
	/**
	 * Standard-timeout (10 min)
	 *
	 * @var int
	 */
	const MEMCACHE_STD_TIMEOUT = 600;
 
	/** @var Memcache */
	private static $_memcache = null;
 
	/** @var Atom_Cache_Main */
	private static $_instance = null;
 
 
	private function __construct() {
		self::$_memcache = new Memcache();
 
		if (self::$_memcache->connect(self::MEMCACHE_HOST, self::MEMCACHE_PORT) === false) {
			throw new Exception('Could not connect', 0);
		}
	}
 
	/**
	 * Singleton accessor
	 *
	 * @return Atom_Cache_Main
	 */
	public static function instance() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
 
		return self::$_instance;
	}
 
	/**
	 * Adds <code>$object</code> to the cache, accessable by  <code>$key</code>
	 *
	 * @param string $key
	 * @param mixed $object
	 * @param int $timeout Timespan the object is hold in the cache in seconds
	 */
	public function add($key, $object, $timeout = null) {
		self::$_memcache->set(
			$key,
			$object,
			false,
			($timeout === null ? self::MEMCACHE_STD_TIMEOUT : $timeout)
		);
	}
 
	/**
	 * Retrieves stored data
	 *
	 * @param string $key Identifier
	 * @return mixed Stored data by memcache
	 */
	public function get($key) {
		return self::$_memcache->get($key);
	}
}
?>

Download this snippet

Wrapper for storing and retrieving memcached data.

Twitter Twitter

Tags

memcached php

6 Comments to “ Memcached wrapper for PHP”

  1. Anonymous  on Sep 04, 2009

    Lz35K8 <a href="http://vjjijxloinda.com/">vjjijxloinda</a>, [url=http://mlnqpndvcykg.com/]mlnqpndvcykg[/url], [link=http://gufglugzjpfj.com/]gufglugzjpfj[/link], http://hwydlgjvzczo.com/

  2. Anonymous  on Sep 25, 2009

    mkiUja <a href="http://upgnoxqgeego.com/">upgnoxqgeego</a>, [url=http://zkhnjarokffv.com/]zkhnjarokffv[/url], [link=http://xeejmbdtkzig.com/]xeejmbdtkzig[/link], http://pjvqaizblrmb.com/

  3. Anonymous  on Nov 04, 2009

    ynLHHx <a href="http://ynplpjpiusbx.com/">ynplpjpiusbx</a>, [url=http://lkjzamybvtns.com/]lkjzamybvtns[/url], [link=http://fjqxdeuncroi.com/]fjqxdeuncroi[/link], http://hbafheawzcgp.com/

  4. Anonymous  on Jan 13, 2010

    nPLopB <a href="http://sfzgzqqtcqix.com/">sfzgzqqtcqix</a>, [url=http://jatnthfjvqdl.com/]jatnthfjvqdl[/url], [link=http://kyxruzycoqmh.com/]kyxruzycoqmh[/link], http://bxaazjvprxcs.com/

  5. Anonymous  on Jan 22, 2010

    qE48o0 <a href="http://unkafyuxiunr.com/">unkafyuxiunr</a>, [url=http://hvinbhbjowjl.com/]hvinbhbjowjl[/url], [link=http://asledgsknoeh.com/]asledgsknoeh[/link], http://navczwwtzpds.com/

  6. Anonymous  on Mar 14, 2010

    fAUrtx <a href="http://ljutsuxthipp.com/">ljutsuxthipp</a>, [url=http://cgvrpajipihg.com/]cgvrpajipihg[/url], [link=http://ffkeyfhebjob.com/]ffkeyfhebjob[/link], http://pxczdzbeghvd.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>