[ Index ]
 

Code source de Phorum 5.1.25

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/include/ -> cache.php (source)

   1  <?php
   2  
   3  ////////////////////////////////////////////////////////////////////////////////
   4  //                                                                            //
   5  //   Copyright (C) 2006  Phorum Development Team                              //
   6  //   http://www.phorum.org                                                    //
   7  //                                                                            //
   8  //   This program is free software. You can redistribute it and/or modify     //
   9  //   it under the terms of either the current Phorum License (viewable at     //
  10  //   phorum.org) or the Phorum License that was distributed with this file    //
  11  //                                                                            //
  12  //   This program is distributed in the hope that it will be useful,          //
  13  //   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
  14  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
  15  //                                                                            //
  16  //   You should have received a copy of the Phorum License                    //
  17  //   along with this program.                                                 //
  18  ////////////////////////////////////////////////////////////////////////////////
  19  
  20  /*
  21   * Simple file-based caching-layer
  22   * Recommended are some more sophisticated solutions, like 
  23   * memcached-, mmcache/eaccelerator-layer
  24   */
  25  if(!defined("PHORUM")) return;
  26  
  27  /* Only load the caching mechanism if we have a cache directory configured. */
  28  if(!isset($PHORUM["cache"])) return;
  29   
  30   
  31  /* initializing our real cache-dir */
  32  $PHORUM['real_cache']=$PHORUM['cache']."/".md5(__FILE__);
  33  
  34  
  35  
  36  /*
  37   * This function returns the cached data for the given key 
  38   * or NULL if no data is cached for this key
  39   */
  40  function phorum_cache_get($type,$key) {
  41      
  42      $partpath=$GLOBALS['PHORUM']['real_cache']."/".$type;
  43  
  44      if(is_array($key)) {
  45          $ret=array();
  46          foreach($key as $realkey) {
  47              $path=$partpath."/".wordwrap(md5($realkey), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
  48              if(file_exists($path)){
  49                  $retval=unserialize(file_get_contents($path));
  50                  // the data is: array($ttl_time,$data)
  51                  if($retval[0] < time()) { // timeout
  52                      unlink($path);
  53                  } else {
  54                      $ret[$realkey]=$retval[1];    
  55                  }
  56                  unset($retval);
  57              }                
  58          }
  59      } else {
  60          $path=$partpath."/".wordwrap(md5($key), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
  61          if(!file_exists($path)){
  62              $ret=NULL;
  63          } else {
  64              $ret=unserialize(file_get_contents($path));
  65              // the data is: array($ttl_time,$data)
  66              if($ret[0] < time()) { // timeout
  67                  $ret=NULL;    
  68                  unlink($path);
  69              } else {
  70                  $ret=$ret[1];    
  71              }
  72          }
  73      }
  74      
  75      
  76      if(is_array($ret) && count($ret) == 0) {
  77          $ret=NULL;    
  78      }
  79      
  80      return $ret;
  81      
  82  }
  83  
  84  /*
  85   * Puts some data into the cache 
  86   * returns number of bytes written (something 'true') or false ... 
  87   * depending of the success of the function
  88   */
  89  function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL) {
  90  
  91      $path=$GLOBALS['PHORUM']['real_cache']."/$type/".wordwrap(md5($key), PHORUM_CACHE_SPLIT, "/", true);
  92      if(!file_exists($path)){
  93          phorum_cache_mkdir($path);
  94      }
  95      $file=$path."/data.php";
  96      $ttl_time=time()+$ttl;
  97      $fp=fopen($file,"w");
  98      $ret=fwrite($fp,serialize(array($ttl_time,$data)));
  99      fclose($fp);    
 100      
 101      return $ret;   
 102  }
 103  
 104  /*
 105   * Removes a key from the cache
 106   */
 107  function phorum_cache_remove($type,$key) {
 108  
 109      $ret  =true;
 110      $path=$GLOBALS['PHORUM']['real_cache']."/$type/".wordwrap(md5($key), PHORUM_CACHE_SPLIT, "/", true)."/data.php";
 111      if(file_exists($path)) {
 112          $ret=unlink($path);   
 113      }
 114      
 115      return $ret;
 116  } 
 117  
 118  /*
 119   * Clears all data from the cache
 120   */
 121  function phorum_cache_clear() {
 122      $dir = $GLOBALS['PHORUM']['real_cache'];
 123      $ret = false;
 124      
 125      if(!empty($dir) && $dir != "/") {
 126          phorum_cache_rmdir($dir);
 127      }
 128      
 129      return $ret;   
 130  }
 131  
 132  /*
 133   type can be nearly each value to specify a group of data
 134   used are currently:
 135   'user'
 136  */
 137  
 138  // helper functions
 139  
 140  // recursively deletes all files/dirs in a directory 
 141  
 142  // recursively creates a directory-tree
 143  function phorum_cache_mkdir($path) {
 144      if(empty($path)) return false;
 145      if(is_dir($path)) return true;
 146      if (!phorum_cache_mkdir(dirname($path))) return false;
 147      mkdir($path);
 148      return true;
 149  }
 150  
 151  // recursively deletes all files/dirs in a directory 
 152  function phorum_cache_rmdir( $path ) {
 153      $stack[]=$path;
 154  
 155      $dirs[]=$path;
 156  
 157      while(count($stack)){
 158          $path=array_shift($stack);
 159          $dir = opendir( $path ) ;
 160          while ( $entry = readdir( $dir ) ) {
 161              if ( is_file( $path . "/" . $entry ) ) {
 162                  unlink($path."/".$entry);
 163              } elseif ( is_dir( $path . "/" . $entry ) && $entry != '.' && $entry != '..' ) {
 164                  array_unshift($dirs, $path . "/" . $entry)  ;
 165                  $stack[]=$path . "/" . $entry  ;
 166              }
 167          }
 168          closedir( $dir ) ;
 169      }
 170      foreach($dirs as $dir){
 171          rmdir($dir);
 172      }
 173      return;
 174  }
 175  
 176  ?>


Généré le : Thu Nov 29 12:22:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics