Newer
Older
ubFramework / Portal / docroot / include / runtime / cache / Instance.php
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 1 KB Cleaning Up Making It A Sub Module
<?php
include_once dirname ( __FILE__ ) . '/Memory.php';

class Sphere_Cache_Instance {

  protected $instance;

  protected function __construct() {

    if (! $this->instance) {
      $this->instance = new Sphere_Cache_Instance_Memory ();
    }

  }

  protected function cacheKey($ns, $key) {

    if (is_array ( $key ))
      $key = implode ( '-', $key );
    return $ns . '-' . $key;

  }

  public function set($namespace, $key, $value) {

    $this->instance->set ( $this->cacheKey ( $namespace, $key ), $value );

  }

  public function get($namespace, $key) {

    return $this->instance->get ( $this->cacheKey ( $namespace, $key ) );

  }

  public function has($namespace, $key) {

    return $this->get ( $namespace, $key ) !== false;

  }

  public function flush() {

    $this->instance->flush ();

    $time = time () + 1; // one second future
    while ( time () < $time ) {
      // sleep
    }

  }

  public static function getInstance() {

    static $singleton = NULL;
    if ($singleton === NULL) {
      $singleton = new self ();
    }
    return $singleton;

  }

}
?>