<?php
/**
 * Singleton Class for representing memcache
 */
class MemcacheHandler {
  
  /**
   * The singleton Instance
   * @var type 
   */
  private static $instance;
  
  /**
   * Memcache instance
   * @var type 
   */
  private $memcache;
  
  /**
   * Defailt duration
   * @var type 
   */
  private $duration = 10;
  
  /**
   * Default constructor, private because of the singleton
   */
  private function __construct() {
    $this->memcache = new Memcache;
    $this->memcache->connect('localhost', 11211) or die ("[MEMCACHE] Could not connect");
  }
  
  /**
   * The instance of the class, thsi class administers a default Memcache
   * @return type
   */
  public static function getInstance() {
    if (self::$instance == null)
      self::$instance = new MemcacheHandler();
    
    return self::$instance;
  }
  
  /**
   * Gets the value of the memcached key
   * @param string $key
   * @return mixed
   */
  public function get($key) {
    return $this->memcache->get($key);
  }
  
  /**
   * Sets the value of the memcached key
   * @param string $key
   * @param mixer $value
   */
  public function set($key, $value) {
    $this->memcache->set($key, $value, false, $this->duration) or die ("[MEMCACHE] Failed to save data at the server:<br/>[KEY=$key]<br/>[VALUE=$value]");
  }
}
?>
