<?php
 
/*! \mainpage Inleitung
 
 *
 
 * \section intro_sec Beschreibung
 
 *
 
 * Mit dieser Classe koennen Sie ausgaben von
 
 * Programmteilen in ein Textfile speichern um eine
 
 * neuausfuehrung des Programmteil fuer eine bestimmte
 
 * Zeit zu unterbinden
 
 *
 
 * <b>Beispiel:</b>
 
 * @code
 
 * <?php
 
 * include (dirname(__FILE__).'/cachen.class.php');
 
 * $cache=new  cachen(dirname(__FILE__),'testfile',300);
 
 * if ($cache->cachen_check()==true){
 
 * $text='Das ist mein Text';
 
 * $ausgabe=$cache->cach_load($text);
 
 * echo $ausgabe;
 
 * } else {
 
 * $ausgabe=$cache->cach_load();
 
 * echo $ausgabe;
 
 * }
 
 * ?>
 
 * @endcode
 
 *
 
 *   <b>LICENSE:</b> GENERAL PUBLIC LICENSE
 
 * @see <http://www.php-space.info/forum/> - Support
 
 * Forum
 
 * @author Nico Schubert
 
 * @version 1.01
 
 * @copyright Copyright © 2012, Nico Schubert
 
 * @package CachClass
 
 */
 
 
/**
 
 * @author Nico Schubert
 
 * @version 1.01
 
 * @copyright Copyright © 2012, Nico Schubert
 
 * @package CachClass
 
 * @see <http://www.php-space.info/forum/> - Support
 
 * Forum
 
 */
 
class cachen {
 
  /**
 
   * Pfad zum Ordner wo die Cachfiles gespeichert werden
 
   * @param string $dir
 
   */
 
  private string $dir='';
 
  /**
 
   * Name des Cachfiles
 
   * @param string $datei
 
   */
 
  private string $datei='';
 
  /**
 
   * Zeit wie lange das Cachfile gespeichert werden soll, bis es neu erstellt wird, Zeit wird in Sec. angegeben. Beispiel: 300 = 5 Min.
 
   * @param int $cachzeit
 
   */
 
  private int $cachzeit=300;
 
  /**
 
   * Type vom Cachfile
 
   * @param string $cach_filetype
 
   */
 
  private string $cach_filetype='txt';
 
  protected string $savefile;
 
 
  /**
 
   * @param string $dir
 
   * @param string $datei
 
   * @param int $cachzeit
 
   */
 
  function __construct(string $dir, string $datei, int $cachzeit){
 
    $this->dir=$dir;
 
    $this->datei=$datei;
 
    $this->cachzeit=(int)$cachzeit;
 
  }
 
  /**
 
   * Mit der Methode cachen_check() kann man pr?fen, ob das Cachfile neu geschrieben werden muss
 
   * @return boolean true or false
 
   */
 
  function cachen_check(): bool
 
  {
 
    $this->savefile = $this->dir.'/'.$this->datei.'.'.$this->cach_filetype;
 
    if(file_exists($this->savefile)){
 
      if ( filemtime($this->savefile)<(time()-$this->cachzeit)){
 
        return true;
 
      } else {
 
        return false;
 
      }
 
    } else {
 
      return true;
 
    }
 
  }
 
  /**
 
   * Mit der Methode cach_load() wird das Cachfile geschrieben bzw. ausgelesen
 
   * @param string $inhalt Text welcher im Cachfile
 
   * gespeichert werden soll
 
   * @return string
 
   */
 
  function cach_load(string $inhalt='' ): string
 
  {
 
    $inhalt     = preg_replace('/\s\s+/', ' ', $inhalt);
 
    $ausgabe='';
 
    if($this->datei!=''){
 
      $this->savefile = $this->dir.'/'.$this->datei.'.'.$this->cach_filetype;
 
      if(file_exists($this->savefile)){
 
        if ( filemtime($this->savefile)<(time()-$this->cachzeit)){
 
          $handle = @fopen($this->savefile, "w");
 
          fwrite($handle, $inhalt);
 
          fclose ($handle);
 
          if(file_exists($this->savefile)){
 
            @chmod ($this->savefile, 0757);
 
          }
 
          $ausgabe.= $inhalt;
 
        } else {
 
          $handle = @fopen ($this->savefile, "r");
 
          while (!feof($handle)) {
 
            $ausgabe.=  fgets($handle, 4096);
 
          }
 
          fclose ($handle);
 
        }
 
 
      } else {
 
        $handle = @fopen($this->savefile, "w");
 
        fwrite($handle, $inhalt);
 
        fclose ($handle);
 
        if(file_exists($this->savefile)){
 
          @chmod ($this->savefile, 0757);
 
        }
 
        $ausgabe.= $inhalt;
 
      }
 
    }
 
    return $ausgabe;
 
  }
 
}
 
 |