Ignore:
Timestamp:
Feb 20, 2014 3:03:59 AM (10 years ago)
Author:
anonymous
Message:

Completed integrating /branches/eli_branch into /trunk. Changes include:

  • Removed closing ?> from end of files
  • Upgrade old-style contructor methods to use construct() instead.
  • Class properties and methods defined as public, private, static or protected
  • Ensure code runs under E_ALL with only mysql_* deprecated warnings
  • Search for the '@' symbol anywhere it might be used to supress runtime errors, then replace with proper error recovery.
  • Run the php cli -l option to check files for syntax errors.
  • Bring tests up-to-date with latest version and methods of PHPUnit
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/lib/Cache.inc.php

    r405 r468  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    3131 * @since   2001
    3232 */
    33  
    34 // Flags.
    35 define('CACHE_ALLOW_OVERSIZED', 1);
    3633
    3734class Cache {
    3835
     36    // A place to keep object instances for the singleton pattern.
     37    private static $instances = array();
     38
    3939    // Namespace of this instance of Prefs.
    40     var $_ns;
     40    private $_ns;
    4141
    4242    // Configuration parameters for this object.
    43     var $_params = array(
    44        
     43    private $_params = array(
     44
     45        // Type of cache. Currently only 'session' is supported.
     46        'type' => 'session',
     47
    4548        // If false nothing will be cached or retrieved. Useful for testing realtime data requests.
    4649        'enabled' => true,
     
    4851        // The maximum size in bytes of any one variable.
    4952        'item_size_limit' => 4194304, // 4 MB
    50        
     53
    5154        // The maximum size in bytes before the cache will begin flushing out old items.
    5255        'stack_size_limit' => 4194304, // 4 MB
    53        
     56
    5457        // The minimum items to keep in the cache regardless of item or cache size.
    5558        'min_items' => 5,
    5659    );
    57    
     60
    5861    /*
    59     * Constructor
     62    * Constructor. This is publically accessible for compatability with older implementations,
     63    * but the preferred method of instantiation is by use of the singleton pattern:
     64    *   $cache =& Cache::getInstance('namespace');
     65    *   $cache->setParam(array('enabled' => true));
    6066    *
    6167    * @access   public
     
    6571    * @since    05 Jun 2006 23:14:21
    6672    */
    67     function Cache($namespace='')
     73    public function __construct($namespace='')
    6874    {
    6975        $app =& App::getInstance();
    70        
     76
    7177        $this->_ns = $namespace;
    7278
     
    7581            $this->setParam(array('enabled' => false));
    7682        }
    77        
     83
    7884        if (!isset($_SESSION['_cache'][$this->_ns])) {
    7985            $this->clear();
     
    8894     * @static
    8995     */
    90     static function &getInstance($namespace='')
    91     {
    92         static $instances = array();
    93 
    94         if (!array_key_exists($namespace, $instances)) {
    95             $instances[$namespace] = new Cache($namespace);
    96         }
    97 
    98         return $instances[$namespace];
     96    public static function &getInstance($namespace='')
     97    {
     98        if (!array_key_exists($namespace, self::$instances)) {
     99            self::$instances[$namespace] = new self($namespace);
     100        }
     101        return self::$instances[$namespace];
    99102    }
    100103
     
    105108     * @param  array    $params     Array of parameters (key => val pairs).
    106109     */
    107     function setParam($params)
     110    public function setParam($params)
    108111    {
    109112        $app =& App::getInstance();
     
    124127     * @return mixed               Configured parameter value.
    125128     */
    126     function getParam($param)
     129    public function getParam($param)
    127130    {
    128131        $app =& App::getInstance();
    129    
     132
    130133        if (isset($this->_params[$param])) {
    131134            return $this->_params[$param];
     
    138141    /**
    139142     * Stores a new variable in the session cache. The $key should not be numeric
    140      * because the array_shift function will reset the key to the next largest 
     143     * because the array_shift function will reset the key to the next largest
    141144     * int key. Weird behavior I can't understand. For example $cache["123"] will become $cache[0]
    142145     *
    143      * @param str   $key        An identifier for the cached object.
    144      * @param mixed $var        The var to store in the session cache.
    145      * @param bool  $flags      If we have something really big that we
    146      *                          still want to cache, setting this to
    147      *                          CACHE_ALLOW_OVERSIZED allows this.
    148      * @return bool             True on success, false otherwise.
    149      */
    150     function set($key, $var, $flags=0)
     146     * @param str   $key                An identifier for the cached object.
     147     * @param mixed $var                The data to store in the session cache.
     148     * @param bool  $allow_oversized    If we have something really big that we still want to cache, setting this to true allows this.
     149     * @return bool                     True on success, false otherwise.
     150     */
     151    public function set($key, $var, $allow_oversized=false)
    151152    {
    152153        $app =& App::getInstance();
     
    157158        }
    158159
     160        if (is_numeric($key)) {
     161            $app->logMsg(sprintf('Cache::set key value should not be numeric (%s given)', $key), LOG_WARNING, __FILE__, __LINE__);
     162        }
     163
    159164        $var = serialize($var);
    160165        $var_len = mb_strlen($var);
     
    165170        }
    166171
    167         if ($flags & CACHE_ALLOW_OVERSIZED == 0 && $var_len >= $this->getParam('stack_size_limit')) {
     172        if ($allow_oversized && $var_len >= $this->getParam('stack_size_limit')) {
    168173            $app->logMsg(sprintf('Serialized variable (%s bytes) more than stack_size_limit (%s bytes).', $var_len, $this->getParam('stack_size_limit')), LOG_NOTICE, __FILE__, __LINE__);
    169174            return false;
    170         }       
     175        }
    171176
    172177        // Remove any value already stored under this key.
     
    197202     * @return mixed          The requested datum, or false on failure.
    198203     */
    199     function get($key)
     204    public function get($key)
    200205    {
    201206        $app =& App::getInstance();
     
    226231     * @return bool         True if a value exists for the given key.
    227232     */
    228     function exists($key)
     233    public function exists($key)
    229234    {
    230235        $app =& App::getInstance();
     
    244249     * @return bool         True if the value existed before being unset.
    245250     */
    246     function delete($key)
     251    public function delete($key)
    247252    {
    248253        if (array_key_exists($key, $_SESSION['_cache'][$this->_ns])) {
     
    253258        }
    254259    }
    255    
     260
    256261    /*
    257262    * Delete all existing items from the cache.
     
    262267    * @since    05 Jun 2006 23:51:34
    263268    */
    264     function clear()
     269    public function clear()
    265270    {
    266271        $_SESSION['_cache'][$this->_ns] = array();
     
    270275}
    271276
    272 ?>
Note: See TracChangeset for help on using the changeset viewer.