[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/cache/ -> memcache.class.php (source)

   1  <?php
   2  
   3      $__memcache_hits = 0;
   4      $__memcache_misses = 0;
   5      $__memcache_queries = 0;
   6  
   7      /**
   8       * \ingroup Cache
   9       *
  10       * Support for caching via memcached
  11       */
  12      class MemCache
  13      {
  14          var $cache;
  15          var $lifeTime;
  16  
  17          var $_disabledCacheCategories = array();
  18  
  19          function MemCache( $cacheProperties )
  20          {
  21              lt_include( PLOG_CLASS_PATH . "class/cache/Memcached_Client/memcached-client.php" );
  22              
  23              $this->cache = new memcached( $cacheProperties );
  24              $this->lifeTime = $cacheProperties['life_time'];
  25          }
  26  
  27  		function setLifeTime( $lifeTime )
  28          {
  29              $this->lifeTime = $lifeTime;
  30          }
  31  
  32          function setData( $id, $group, $data )
  33          {
  34              $key = $this->getKey( $id, $group );
  35              return $this->cache->set( $key, $data, $this->lifeTime );
  36          }
  37          
  38          /** 
  39           * Works in the same way as Cache::setData does, but instead of setting single values,
  40           * it assumes that the value we're setting for the given key is part of an array of values. This
  41           * method is useful for data which we know is not unique.
  42           */
  43  		function setMultipleData( $id, $group, $data )
  44          {
  45              $currentData = $this->getData( $id, $group );
  46              if( !$currentData ) $currentData = Array();
  47  
  48              /**
  49              * :TODO:
  50              * It's clear that we're only going to cache DbObjects using this method
  51              * but what happens if we don't? Should we force developers to provide a method
  52              * to uniquely identify their own objects? We definitely need a unique id here so that
  53              * the array doesn't grow forever...
  54              */
  55              $currentData[$data->getId()] = $data;
  56  
  57              return $this->setData( $id, "$group", $currentData );
  58          }
  59  
  60          function getData( $id, $group )
  61          {
  62              global $__memcache_hits;            
  63              global $__memcache_queries;
  64              global $__memcache_misses;        
  65          
  66              $__memcache_queries++;
  67  
  68              $key = $this->getKey( $id, $group );
  69              $data = $this->cache->get( $key );
  70  
  71              if ($data) {
  72                  $__memcache_hits++;
  73              }
  74              else {
  75                  $__memcache_misses++;                        
  76              }
  77  
  78              return $data;
  79          }
  80  
  81          function removeData( $id, $group )
  82          {
  83              $key = $this->getKey( $id, $group );
  84              return $this->cache->delete( $key, $group );
  85          }
  86  
  87          function clearCacheByGroup( $group )
  88          {
  89              return true;
  90          }
  91  
  92          function clearCache()
  93          {
  94              return $this->cache->flush_all();
  95          }
  96  
  97          /**
  98           * returns the total count of cache hits, miss and total queries over the lifetime of the
  99           * script so far.
 100           *
 101           * @return An array with 3 keys: "hits", "total" and "miss"
 102           */
 103  		function getCacheStats()
 104          {
 105              global $__memcache_hits;
 106              global $__memcache_misses;
 107              global $__memcache_queries;
 108          
 109              return( Array( "total" => $__memcache_queries,
 110                             "hits"  => $__memcache_hits,
 111                             "miss"  => $__memcache_misses )); 
 112          }
 113          
 114  		function getKey( $id, $group )
 115          {
 116              return $group.':'.$id;    
 117          }
 118      }
 119  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics