[ Index ]

PHP Cross Reference of PHK Manager

title

Body

[close]

/PHK/ -> Cache.php (source)

   1  <?php
   2  //=============================================================================
   3  //
   4  // Copyright Francois Laupretre <phk@tekwire.net>
   5  //
   6  //   Licensed under the Apache License, Version 2.0 (the "License");
   7  //   you may not use this file except in compliance with the License.
   8  //   You may obtain a copy of the License at
   9  //
  10  //       http://www.apache.org/licenses/LICENSE-2.0
  11  //
  12  //   Unless required by applicable law or agreed to in writing, software
  13  //   distributed under the License is distributed on an "AS IS" BASIS,
  14  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15  //   See the License for the specific language governing permissions and
  16  //   limitations under the License.
  17  //
  18  //=============================================================================
  19  /**
  20  * @copyright Francois Laupretre <phk@tekwire.net>
  21  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  22  * @category PHK
  23  * @package PHK
  24  *///==========================================================================
  25  
  26  namespace PHK {
  27  
  28  if (!class_exists('PHK\Cache',false))
  29  {
  30  
  31  //=============================================================================
  32  /**
  33  * The PHK cache gateway
  34  *
  35  * The cache key is based on the mount point because it uniquely defines the
  36  * PHK file on the current host.
  37  *
  38  * API status: Public
  39  * Included in the PHK PHP runtime: Yes
  40  * Implemented in the extension: Yes
  41  *///==========================================================================
  42  
  43  class Cache    // Static only
  44  {
  45  const TTL=3600;    // Arbitrary TTL of one hour
  46  
  47  //-- Global static properties
  48  
  49  private static $caches=array("apc","xcache","eaccelerator");
  50  
  51  private static $cacheName;    // Info
  52  
  53  /** @var Object|false|null    The cache system we are using for PHK instances
  54  *
  55  * False means 'no cache'.
  56  * null until set by setCacheObject().
  57  */
  58  
  59  private static $cache=null;
  60  
  61  private static $cache_maxsize=524288;    // 512 Kb
  62  
  63  //-----
  64  
  65  public static function cacheID($prefix,$key)
  66  {
  67  return 'phk.'.$prefix.'.'.$key;
  68  }
  69  
  70  //---------------------------------
  71  // If the cache's init() method returns an exception, don't use it.
  72  
  73  private static function setCacheObject()
  74  {
  75  if (is_null(self::$cache))
  76      {
  77      self::$cache=false;
  78      self::$cacheName='none';
  79      foreach(self::$caches as $c)
  80          {
  81          if (!extension_loaded($c)) continue;
  82          
  83          $class='Cache_'.$c;
  84          $obj=new $class;
  85          try { $status=$obj->init(); }
  86          catch (\Exception $e) { $status=false; }
  87          if ($status)
  88              {
  89              self::$cache=$obj;
  90              self::$cacheName=$c;
  91              break;
  92              }
  93          unset($obj);
  94          }
  95      \PHK\Tools\Util::trace("Cache system used : ".self::$cacheName);//TRACE
  96      }
  97  }
  98  
  99  //---------------------------------
 100  
 101  public static function setCacheMaxSize($size)
 102  {
 103  $this->cache_maxsize=$size;
 104  }
 105  
 106  //---------------------------------
 107  
 108  public static function cacheName()
 109  {
 110  if (is_null(self::$cache)) self::setCacheObject();
 111  
 112  return self::$cacheName;
 113  }
 114  
 115  //---------------------------------
 116  
 117  public static function cachePresent()
 118  {
 119  if (is_null(self::$cache)) self::setCacheObject();
 120  
 121  return (self::$cache!==false);
 122  }
 123  //---------------------------------
 124  /**
 125  * Gets an element from cache
 126  *
 127  * Fast path
 128  *
 129  * @param string $id        Cache key
 130  *
 131  * @return string|null The data. Null if not found
 132  */
 133  
 134  public static function get($id)
 135  {
 136  if (is_null(self::$cache)) self::setCacheObject();
 137  
 138  if (self::$cache===false) return null;
 139  
 140  $result=self::$cache->get($id);
 141  if ($result===false) $result=null;
 142  
 143  return $result;
 144  }
 145  
 146  //---------------------------------
 147  /**
 148  * Writes an element to cache
 149  *
 150  * @param string $id        Cache key
 151  * @param string $data    Data to cache
 152  *
 153  * @return void
 154  */
 155  
 156  public static function set($id,$data)
 157  {
 158  if (is_null(self::$cache)) self::setCacheObject();
 159  
 160  if (is_object(self::$cache))
 161      {
 162      if (is_string($data) && (strlen($data) > self::$cache_maxsize)) return;
 163  
 164      \PHK\Tools\Util::trace("Writing cache: id=$id");//TRACE
 165      self::$cache->set($id,$data);
 166      }
 167  }
 168  
 169  //---------------------------------
 170  } // End of class \PHK\Cache
 171  //=============================================================================
 172  
 173  abstract class CacheBase
 174  {
 175  // Returns true if this system can be used. \Exception if unavailable
 176  
 177  abstract public function init();
 178  
 179  // Return data or null
 180  
 181  abstract public function get($id);
 182  
 183  // Return void
 184  
 185  abstract public function set($id,$data);
 186  }
 187  
 188  //=============================================================================
 189  
 190  class Cache_apc extends CacheBase
 191  {
 192  
 193  public function init()
 194  {
 195  // Valid only in a web environment or if CLI is explicitely enabled
 196  
 197  return \PHK\Tools\Util::envIsWeb() || ini_get('apc.enable_cli');
 198  }
 199  
 200  //------
 201  
 202  public function get($id)
 203  {
 204  return apc_fetch($id);
 205  }
 206  
 207  //------
 208  
 209  public function set($id,$data)
 210  {
 211  apc_store($id,$data,\PHK\Cache::TTL);
 212  }
 213  
 214  //---------------------------------
 215  } // End of class \PHK\Cache_apc
 216  //=============================================================================
 217  
 218  class Cache_xcache extends CacheBase
 219  {
 220  
 221  public function init()
 222  {
 223  return \PHK\Tools\Util::envIsWeb(); // Valid only in a web environment
 224  }
 225  
 226  //------
 227  
 228  public function get($id)
 229  {
 230  return xcache_get($id);
 231  }
 232  
 233  //------
 234  
 235  public function set($id,$data)
 236  {
 237  xcache_set($id,$data,\PHK\Cache::TTL);
 238  }
 239  
 240  //---------------------------------
 241  } // End of class \PHK\Cache_xcache
 242  //=============================================================================
 243  
 244  class Cache_eaccelerator extends CacheBase
 245  {
 246  
 247  public function init()
 248  {
 249  // eaccelerator must be compiled with shared memory functions 
 250  // (configured with --with-eaccelerator-shared-memory)
 251  
 252  if (!function_exists('eaccelerator_get')) return false;
 253  
 254  return \PHK\Tools\Util::envIsWeb(); // Valid only in a web environment
 255  }
 256  
 257  //------
 258  
 259  public function get($id)
 260  {
 261  return eaccelerator_get($id);
 262  }
 263  
 264  //------
 265  
 266  public function set($id,$data)
 267  {
 268  eaccelerator_put($id,$data,\PHK\Cache::TTL);
 269  }
 270  
 271  //---
 272  } // End of class
 273  //===========================================================================
 274  } // End of class_exists
 275  //===========================================================================
 276  } // End of namespace
 277  //===========================================================================
 278  ?>


Generated: Thu Jun 4 18:33:15 2015 Cross-referenced by PHPXref 0.7.1