[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkCache/ -> AkMemcache.php (source)

   1  <?php
   2  require_once(AK_VENDOR_DIR.DS.'phpmemcached'.DS.'class_MemCachedClient.php');
   3  require_once (AK_LIB_DIR.DS.'AkObject.php');
   4  
   5  class AkMemcache extends AkObject
   6  {
   7      
   8      
   9      /**
  10       * @var MemCachedClient
  11       */
  12      var $_memcache;
  13      /**
  14       * caching the integer namespace values
  15       *
  16       * @var array
  17       */
  18      var $_namespaces = array();
  19      
  20      /**
  21       * max storable size for 1 key,
  22       * above this size, the class will autosplit
  23       * the data into chunks
  24       *
  25       * @var int
  26       */
  27      var $_max_size = 1000000;
  28      
  29      var $_servers = array();
  30      var $_lifeTime = 0;
  31      
  32      function init($options = array())
  33      {
  34          $default_options = array('servers'=>array('localhost:11211'),'lifeTime'=>0);
  35          $options = array_merge($default_options, $options);
  36          $this->_lifeTime = $options['lifeTime'];
  37          if (empty($options['servers'])) {
  38              trigger_error('Need to provide at least 1 server',E_USER_ERROR);
  39              return false;
  40          }
  41          $this->_memcache = new MemCachedClient(is_array($options['servers'])?$options['servers']:array($options['servers']));
  42          $ping = $this->_memcache->get('ping');
  43          if (!$ping) {
  44              if ($this->_memcache->errno==ERR_NO_SOCKET) {
  45                  trigger_error("Could not connect to MemCache daemon", E_USER_WARNING);
  46                  return false;
  47              }
  48              $this->_memcache->set('ping',1);
  49          }
  50          return true;
  51      }
  52      
  53  
  54      function _getNamespaceId($group)
  55      {
  56          $ident = $group;
  57          return $ident;
  58      }
  59      
  60      function _clearNamespace($group)
  61      {
  62          $group = 'group_'.md5($group);
  63          $ident = $this->_getNamespaceId($group);
  64          unset($this->_namespaces[$group]);
  65          return $this->_memcache->incr($ident,1);
  66      }
  67      
  68      function _getNamespace($group)
  69      {
  70          $groupName = $group;
  71          $group = 'group_'.md5($groupName);
  72          if (!isset($this->_namespaces[$group])) {
  73              $ident = $this->_getNamespaceId($group);
  74              $namespaceVersion = $this->_memcache->get($ident);
  75              if (!$namespaceVersion) {
  76                  if ($this->_memcache->errno==ERR_NO_SOCKET) {
  77                      trigger_error("Could not connect to MemCache daemon", E_USER_ERROR);
  78                  }
  79                  $namespaceVersion = 1;
  80                  $this->_memcache->set($ident,$namespaceVersion);
  81                  
  82              }
  83              $this->_namespaces[$group] = $groupName.'_'.$namespaceVersion;
  84          }
  85          return $this->_namespaces[$group];
  86      }
  87      
  88      function _generateCacheKey($id,$group)
  89      {
  90          $namespace = $this->_getNamespace($group);
  91          $key = $namespace.'_'.$id;
  92          $key = 'key_'.md5($key);
  93          return $key;
  94      }
  95      
  96      function get($id, $group = 'default')
  97      {
  98          $key = $this->_generateCacheKey($id, $group);
  99          $return = $this->_memcache->get($key);
 100          
 101          if ($return === false) {
 102              return false;
 103          }
 104          
 105          @list($type,$data) = @split('@#!',$return,2);
 106          if (isset($data)) {
 107              settype($data,$type);
 108          } else {
 109              if (is_string($return) && substr($return,0,15) == '@____join____@:') {
 110                  @list($start,$parts) = @split(':',$return,2);
 111                  $return = '';
 112                  for($i=0;$i<(int)$parts;$i++) {
 113                      $return.=$this->_memcache->get($key.'_'.$i);
 114                  }
 115              }
 116              $data = &$return;
 117          }
 118          return $data;
 119      }
 120      
 121      function save($data, $id = null, $group = null)
 122      {   
 123          if (is_numeric($data) || is_bool($data)) {
 124              $type=gettype($data);
 125              $data = $type.'@#!'.$data;
 126          } else if (is_string($data) && ($strlen=strlen($data))> $this->_max_size) {
 127              $parts = round($strlen / $this->_max_size);
 128              $key = $this->_generateCacheKey($id, $group);
 129              $keys = array();
 130              for ($i=0;$i<$parts;$i++) {
 131                  $nkey = $key.'_'.$i;
 132                  $this->_memcache->set($nkey,substr($data,$i*$this->_max_size,$this->_max_size),$this->_lifeTime);
 133              }
 134              
 135              $return = $this->_memcache->set($key,'@____join____@:'. $parts);
 136              return $return !== false ? true:false;
 137          }
 138          $key = $this->_generateCacheKey($id, $group);
 139          $return = $this->_memcache->set($key,$data, $this->_lifeTime);
 140          return $return !== false ? true:false;
 141      }
 142      
 143      function remove($id, $group = 'default')
 144      {
 145          $key = $this->_generateCacheKey($id, $group);
 146          $return = $this->_memcache->delete($key);
 147          return $return;
 148      }
 149      
 150      function clean($group = false, $mode = 'ingroup')
 151      {
 152          switch ($mode) {
 153              case 'ingroup':
 154                  return $this->_clearNamespace($group);
 155              case 'notingroup':
 156                  return false;
 157              case 'old':
 158                  return true;
 159              default:
 160                  return true;
 161          }
 162          
 163      }
 164  }


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6