includes/cache.php
changeset 607 935f3799b654
child 685 17ebe24cdf85
equal deleted inserted replaced
605:d2d4e40ecd29 607:935f3799b654
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.4 (Caoineag alpha 4)
       
     6  * Copyright (C) 2006-2008 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14  
       
    15 /**
       
    16  * Framework for caching arbitrary data around the site.
       
    17  * @package Enano
       
    18  * @subpackage Core
       
    19  * @author Dan Fuhry <dan@enanocms.org>
       
    20  * @license GNU General Public License
       
    21  */
       
    22 
       
    23 class CacheManager
       
    24 {
       
    25   /**
       
    26    * Fetch a cached piece of data.
       
    27    * @param string Cache ID. The timestamp is checked automatically.
       
    28    */
       
    29   
       
    30   public function fetch($cache_id)
       
    31   {
       
    32     if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) )
       
    33     {
       
    34       throw new Exception('Cache ID must be letters, numbers, and underscores.');
       
    35     }
       
    36     $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php";
       
    37     if ( file_exists($cache_file) )
       
    38     {
       
    39       require($cache_file);
       
    40       if ( isset($_cache_data) && isset($_cache_ttl) && isset($_cache_timestamp) )
       
    41       {
       
    42         $cache_expire = $_cache_timestamp + ( 60 * $_cache_ttl);
       
    43         if ( $cache_expire >= time() || $_cache_ttl === -1 )
       
    44         {
       
    45           return $_cache_data;
       
    46         }
       
    47       }
       
    48     }
       
    49     return false;
       
    50   }
       
    51   
       
    52   /**
       
    53    * Stores an array by var_export()ing it and saving it to disk.
       
    54    * @param string Cache ID - human readable name for this store (letters, numbers, underscores)
       
    55    * @param array Data to store
       
    56    * @param int TTL for the cached array, in minutes. Defaults to 20. If set to -1, caches indefinitely.
       
    57    * @return bool True on success, false on failure
       
    58    */
       
    59   
       
    60   public function store($cache_id, $data, $ttl = 20)
       
    61   {
       
    62     if ( getConfig('cache_thumbs') != '1' )
       
    63     {
       
    64       // caching disabled
       
    65       return false;
       
    66     }
       
    67     if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) )
       
    68     {
       
    69       throw new Exception('Cache ID must be letters, numbers, and underscores.');
       
    70     }
       
    71     if ( !is_int($ttl) )
       
    72     {
       
    73       throw new Exception('TTL must be an integer');
       
    74     }
       
    75     
       
    76     $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php";
       
    77     if ( file_exists($cache_file) )
       
    78     {
       
    79       @unlink($cache_file);
       
    80     }
       
    81     $fh = @fopen($cache_file, 'w');
       
    82     if ( !$fh )
       
    83     {
       
    84       throw new Exception('Failed to open file for writing.');
       
    85     }
       
    86     $exported = Language::var_export_string($data);
       
    87     $now = time();
       
    88     $content = <<<EOF
       
    89 <?php
       
    90 /**
       
    91  * Automatically generated cache file.
       
    92  * Do not edit this file as it will expire and be overwritten $ttl minutes from its creation.
       
    93  */
       
    94 
       
    95 \$_cache_ttl = $ttl;
       
    96 \$_cache_timestamp = $now;
       
    97 \$_cache_data = $exported;
       
    98 
       
    99 EOF;
       
   100     fwrite($fh, $content);
       
   101     fclose($fh);
       
   102     
       
   103     return true;
       
   104   }
       
   105   
       
   106   /**
       
   107    * Deletes a cached item.
       
   108    * @param string Cache ID.
       
   109    * @return bool true on success or if item doesn't exist, false on failure
       
   110    */
       
   111   
       
   112   public function purge($cache_id)
       
   113   {
       
   114     if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) )
       
   115     {
       
   116       throw new Exception('Cache ID must be letters, numbers, and underscores.');
       
   117     }
       
   118     
       
   119     $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php";
       
   120     if ( file_exists($cache_file) )
       
   121     {
       
   122       if ( unlink($cache_file) )
       
   123       {
       
   124         return true;
       
   125       }
       
   126       return false;
       
   127     }
       
   128     return true;
       
   129   }
       
   130 }
       
   131