Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. /**
  3.  * SCache - Simple Data Caching to Cache Files
  4.  *
  5.  * SCache is free software and is distributed WITHOUT ANY WARRANTY
  6.  *
  7.  * @version $v: 1.2 $;
  8.  * @copyright Copyright (c) 2011 Shay Anderson
  9.  * @license http://www.gnu.org/licenses/gpl.html GPL License
  10.  * @link http://www.w3tools.info/2011/09/scache-simple-data-caching-class.html
  11.  */
  12.  
  13. /**
  14.  * Define SCache settings
  15.  */
  16.  
  17. // default cache write directory (ex: "/var/www/project/cache/")
  18. define("SCACHE_DIR", "");
  19.  
  20. // default flag to allow empty cache (will check for empty data or empty array if array)
  21. define("SCACHE_EMPTY_CACHE_ALLOWED", true);
  22.  
  23. // default cache file extension (default ".scache")
  24. define("SCACHE_EXT", ".scache");
  25.  
  26. // defaul cache header data (default: "")
  27. define("SCACHE_HEADER", "");
  28.  
  29. // default cache default lifetime seconds (default 1 hour: "3600")
  30. define("SCACHE_LIFETIME", 3600);
  31.  
  32. /**
  33.  * SCache Class - Simple Data Caching to Cache Files
  34.  *
  35.  * @package SCache
  36.  * @name SCache
  37.  * @author Shay Anderson 06.11
  38.  */
  39. final class SCache {
  40.     /**
  41.      * Cache write directory
  42.      *
  43.      * @var string
  44.      */
  45.     private static $_cache_dir = SCACHE_DIR;
  46.  
  47.     /**
  48.      * Allow empty cache flag
  49.      *
  50.      * @var bool
  51.      */
  52.     private static $_cache_empty_allowed = SCACHE_EMPTY_CACHE_ALLOWED;
  53.  
  54.     /**
  55.      * Cache header data
  56.      *
  57.      * @var string
  58.      */
  59.     private static $_cache_header = SCACHE_HEADER;
  60.  
  61.     /**
  62.      * Cache lifetime seconds
  63.      *
  64.      * @var int
  65.      */
  66.     private static $_cache_lifetime = SCACHE_LIFETIME;
  67.  
  68.     /**
  69.      * Cache filename getter
  70.      *
  71.      * @param string $cache_id
  72.      * @param bool $static_naming
  73.      * @return string
  74.      */
  75.     private static function _getCacheFilename($cache_id = null, $static_naming = false) {
  76.         // check for subdir
  77.         $subdir = null;
  78.         if(strpos($cache_id, "/") !== false) {
  79.             $subdir = substr($cache_id, 0, (strrpos($cache_id, "/") + 1));
  80.             $cache_id = substr($cache_id, (strrpos($cache_id, "/") + 1), strlen($cache_id));
  81.         }
  82.  
  83.         return self::$_cache_dir . $subdir . ( $static_naming ? rawurlencode($cache_id) : rawurlencode(md5($_SERVER["REQUEST_URI"])
  84.             . ( $cache_id !== null ? "-{$cache_id}" : null )) ) . SCACHE_EXT;
  85.     }
  86.  
  87.     /**
  88.      * Flush cache file
  89.      *
  90.      * @param string $cache_id
  91.      * @param bool $static_naming
  92.      */
  93.     public static function flush($cache_id = null, $static_naming = false) {
  94.         // check if file cached
  95.         if(self::isCached($cache_id, $static_naming)) {
  96.             // flush cache file
  97.             unlink(self::_getCacheFilename($cache_id, $static_naming));
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Flush all cache files
  103.      */
  104.     public static function flushCacheFiles() {
  105.         array_map("unlink", glob(self::$_cache_dir . "*" . SCACHE_EXT));
  106.     }
  107.  
  108.     /**
  109.      * Cache file content getter
  110.      *
  111.      * @param mixed $cache_id
  112.      * @param bool $unserialize
  113.      * @param bool $static_naming
  114.      * @return string
  115.      */
  116.     public static function get($cache_id = null, $unserialize = false, $static_naming = false) {
  117.         // check if caching is on
  118.         if((int)self::$_cache_lifetime > 0) {
  119.             // check if cache file exists
  120.             if(self::isCached($cache_id, $static_naming)) {
  121.                 // check if cache is not expired
  122.                 if( (time() - filemtime(self::_getCacheFilename($cache_id, $static_naming))) < self::$_cache_lifetime ) {
  123.                     // return cache file
  124.                     return $unserialize ? unserialize(file_get_contents(self::_getCacheFilename($cache_id, $static_naming)))
  125.                         : file_get_contents(self::_getCacheFilename($cache_id, $static_naming));
  126.                 // cache file has expired
  127.                 } else {
  128.                     // flush cache file
  129.                     self::flush($cache_id, $static_naming);
  130.                 }
  131.             }
  132.         }
  133.  
  134.         // no cache file
  135.         return null;
  136.     }
  137.  
  138.     /**
  139.      * Cache file exists getter
  140.      *
  141.      * @param mixed $cache_id
  142.      * @param bool $static_naming
  143.      * @return bool
  144.      */
  145.     public static function isCached($cache_id = null, $static_naming = false) {
  146.         return file_exists(self::_getCacheFilename($cache_id, $static_naming));
  147.     }
  148.  
  149.     /**
  150.      * Cache directory setter
  151.      *
  152.      * @param string $cache_directory
  153.      */
  154.     public static function setCacheDir($cache_directory = null) {
  155.         self::$_cache_dir = $cache_directory;
  156.     }
  157.  
  158.     /**
  159.      * Cache header data setter
  160.      *
  161.      * @param string $header_data
  162.      */
  163.     public static function setCacheHeader($header_data = null) {
  164.         self::$_cache_header = $header_data;
  165.     }
  166.  
  167.     /**
  168.      * Cache lifetime setter (0 will turn caching off)
  169.      *
  170.      * @param int $cache_lifetime (seconds)
  171.      */
  172.     public static function setCacheLifetime($cache_lifetime = 0) {
  173.         self::$_cache_lifetime = (int)$cache_lifetime;
  174.     }
  175.  
  176.     /**
  177.      * Cache file writer
  178.      *
  179.      * @param mixed $content
  180.      * @param string $cache_id
  181.      * @param bool $serialize
  182.      * @param bool $static_naming
  183.      * @return bool
  184.      */
  185.     public static function write($content = null, $cache_id = null, $serialize = false, $static_naming = false) {
  186.         // check if caching is on
  187.         if((int)self::$_cache_lifetime > 0) {
  188.             // check if empty cache is allowed
  189.             if(!self::$_cache_empty_allowed) {
  190.                 // empty cache not allowed, check if cache content is empty
  191.                 if(!is_array($content) && !$content || is_array($content) && !count($content)) {
  192.                     // cache is empty, do not write empty cache (not allowed)
  193.                     return false;
  194.                 }
  195.             }
  196.  
  197.             // check for subdir
  198.             $subdir = null;
  199.             if(strpos($cache_id, "/") !== false) {
  200.                 $parts = explode("/", $cache_id);
  201.                 if(count($parts) > 1) {
  202.                     $cache_id_rem = array_pop($parts);
  203.                 }
  204.                 // create dirs if needed
  205.                 foreach($parts as $dir) {
  206.                     if($dir !== null) {
  207.                         if(!is_dir(self::$_cache_dir . $subdir . $dir) ) mkdir(self::$_cache_dir . $subdir . $dir);
  208.                         $subdir .= "{$dir}/";
  209.                     }
  210.                 }
  211.             }
  212.  
  213.             // check if cache directory writable
  214.             if(is_writable(self::$_cache_dir . $subdir)) {
  215.                 // write cache file (with cache header data)
  216.                 return file_put_contents(self::_getCacheFilename($cache_id, $static_naming),
  217.                     self::$_cache_header . ( $serialize ? serialize($content) : $content )) ? true : false;
  218.             // cache directory not writable
  219.             } else {
  220.                 trigger_error("Failed to write cache file, cache directory \""
  221.                     . self::$_cache_dir . "\" is not writable (" . __METHOD__ . ")", E_USER_WARNING);
  222.             }
  223.         }
  224.     }
  225. }
  226. ?>
  227.