[ 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/ -> cache.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup Cache
   5       *
   6       * The Cache group is made up of the Cache class, which provides the basic methods to store and
   7       * retrieve data based on keys, and the CacheManager class which provides a singleton to access to
   8       * the global.
   9       */
  10  
  11      $__cache_hits = 0;
  12      $__cache_misses = 0;
  13      $__cache_queries = 0;
  14  
  15      /** 
  16       * \ingroup Cache 
  17       *
  18       * <p>This class wraps around the Cache_Lite class and provides a set of basic methods for storing
  19       * and retrieving data from the cache. As of LifeType 1.1, this class is only used by the DAO layer as its
  20       * caching mechanism but it can only be used by any other class if necessary.</p>
  21       *
  22       * <p>You probably don't want to create objects of this class, but instead use the CacheManager class
  23       * that provides a singleton to access one global instance of this class</p>
  24       *
  25       * @see CacheManager
  26       * @see Cache_Lite
  27       */
  28      class Cache
  29      {
  30          var $cache;
  31          var $lifeTime;
  32  
  33          /** 
  34           * Constructor of the class. 
  35           *
  36           * @param cacheProperties An array with parameters as required by Cache_Lite        
  37           */
  38          function Cache( $cacheProperties )
  39          {
  40              lt_include( PLOG_CLASS_PATH . "class/cache/Cache_Lite/Lite.php" );
  41              
  42              $this->cache = new Cache_Lite( $cacheProperties );
  43              $this->lifeTime = $cacheProperties['lifeTime'];
  44          }
  45  
  46          /** 
  47           * Sets the lifetime of cached data
  48           *
  49           * @param lifeType
  50           */
  51  		function setLifeTime( $lifeTime )
  52          {
  53              $this->lifeTime = $lifeTime;
  54          }        
  55  
  56          /**
  57           * Saves data to the cache. Data is identified by an id and a group. Any data
  58           * can be saved to the cache but please check that you are using unique keys for
  59           * different data or else data will be overwritten. If you have data that you know
  60           * beforehand is not going to be unique, please use the setMultipleData method.
  61           *
  62           * @param id Unique identifier for the data.
  63           * @param group The cache group
  64           * @param data Data that is going to be cached
  65           * @return Returns true if successful or false otherwise
  66           */
  67          function setData( $id, $group, $data )
  68          {
  69              $this->cache->setLifeTime( $this->lifeTime );    
  70              return $this->cache->save( $data, $id, "$group" );
  71          }
  72          
  73          /** 
  74           * Works in the same way as Cache::setData does, but instead of setting single values,
  75           * it assumes that the value we're setting for the given key is part of an array of values. This
  76           * method is useful for data which we know is not unique, since it will store the data
  77           * identified by data in an array identified by $id, alongside with any other values
  78           * that are sharing the same key.        
  79           *
  80           * @param id Unique identifier for the data.
  81           * @param group The cache group
  82           * @param data Data that is going to be cached
  83           * @return Returns true if successful or false otherwise
  84           */
  85  		function setMultipleData( $id, $group, $data )
  86          {
  87              $currentData = $this->getData( $id, $group );
  88              if( !$currentData ) $currentData = Array();
  89                  
  90              /**
  91               * :TODO:
  92               * It's clear that we're only going to cache DbObjects using this method
  93               * but what happens if we don't? Should we force developers to provide a method
  94               * to uniquely identify their own objects? We definitely need a unique id here so that
  95               * the array doesn't grow forever...
  96               */
  97              $currentData[$data->getId()] = $data;
  98                  
  99              return $this->cache->save( $currentData, $id, "$group" );
 100          }
 101  
 102          /**
 103           * Retrieves previously stored data given its key.
 104           *
 105           * @param id Unique identifier under which the data was cached
 106           * @param group Cache group where data was stored
 107           * @return Returns the cached data if found or false otherwise
 108           */
 109          function getData( $id, $group )
 110          {
 111              global $__cache_hits;            
 112              global $__cache_queries;
 113              global $__cache_misses;        
 114              $__cache_queries++;
 115              
 116              $data = $this->cache->get( $id, $group );
 117  
 118              if ($data) {
 119                  $__cache_hits++;
 120              }
 121              else {
 122                  $__cache_misses++;                        
 123              }
 124  
 125              return $data;
 126          }
 127  
 128          /**
 129           * Removes cached data from the cache, given its key and cache group.
 130           *
 131           * @param id Unique identifier under which the data was cached
 132           * @param group Cache group where data was stored
 133           * @return Returns the cached data if found or false otherwise
 134           */
 135          function removeData( $id, $group )
 136          {
 137              return $this->cache->remove( $id, $group );
 138          }
 139  
 140          /**
 141           * Clears the data of a whole cache group.
 142           *
 143           * @param group The group identifer whose data we'd like to cleanup
 144           * @return
 145           */
 146          function clearCacheByGroup( $group )
 147          {
 148              return $this->cache->clean( $group );
 149          }
 150  
 151          /**
 152           * Clears the entire cache, use with care.
 153           *
 154           * @return Returns true if successful or false otherwise
 155           */
 156          function clearCache()
 157          {
 158              return $this->cache->clean();
 159          }
 160  
 161          /**
 162           * Sets the directory where Cache_Lite will store its data. By default this is set to
 163           * ./tmp.
 164           *
 165           * @param temp_folder Folder where cache data will be stored
 166           * @return Always true
 167           */
 168          function setCacheDir( $temp_folder )
 169          {
 170              $this->cache->cacheDir = $temp_folder;
 171          }
 172          
 173          /**
 174           * Returns the total count of cache hits, miss and total queries over the lifetime of the
 175           * script so far.
 176           *
 177           * @return An array with 3 keys: "hits", "total" and "miss"
 178           */
 179  		function getCacheStats()
 180          {
 181              global $__cache_hits;
 182              global $__cache_misses;
 183              global $__cache_queries;
 184          
 185              return( Array( "total" => $__cache_queries,
 186                             "hits"  => $__cache_hits,
 187                             "miss"  => $__cache_misses )); 
 188          }
 189      }
 190  ?>


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