includes/cache.php
author Dan Fuhry <dan@enanocms.org>
Mon, 19 Nov 2012 11:40:35 -0500
changeset 1373 851b91febb85
parent 1365 a52cac1b190d
permissions -rw-r--r--
Merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
607
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     1
<?php
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     2
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     3
/*
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
1081
745200a9cc2a Fixed some upgrade bugs; added support for choosing one's own date/time formats; rebrand as 1.1.7
Dan
parents: 801
diff changeset
     5
 * Copyright (C) 2006-2009 Dan Fuhry
607
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     6
 *
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     7
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     8
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
     9
 *
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    11
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    12
 */
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    13
 
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    14
/**
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    15
 * Framework for caching arbitrary data around the site.
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    16
 * @package Enano
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    17
 * @subpackage Core
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    18
 * @author Dan Fuhry <dan@enanocms.org>
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    19
 * @license GNU General Public License
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    20
 */
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    21
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    22
class CacheManager
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
    23
{
1365
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    24
	public static function factory($backend = 'auto')
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    25
	{
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    26
		if ( function_exists('xcache_set') )
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    27
			return new CacheManager_XCache();
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    28
		else
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    29
			return new CacheManager_File();
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    30
	}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    31
}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    32
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    33
class CacheManager_XCache
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    34
{
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    35
	private $prefix;
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    36
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    37
	/**
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    38
	 * Constructor. Merely generates the prefix that will be prepended to cache objects.
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    39
	 */
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    40
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    41
	public function __construct()
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    42
	{
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    43
		global $crypto_key;
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    44
		$this->prefix = 'enano' . substr(md5($crypto_key), 0, 8) . '_';
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    45
	}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    46
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    47
	/**
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    48
	 * @see CacheManager_File::fetch()
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    49
	 */
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    50
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    51
	public function fetch($cache_id)
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    52
	{
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    53
		return xcache_isset($this->prefix . $cache_id) ? xcache_get($this->prefix . $cache_id) : false;
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    54
	}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    55
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    56
	/**
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    57
	 * @see CacheManager_File::store()
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    58
	 */
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    59
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    60
	public function store($cache_id, $data, $ttl = 20)
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    61
	{
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    62
		return xcache_set($this->prefix . $cache_id, $data, $ttl * 60);
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    63
	}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    64
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    65
	/**
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    66
	 * @see CacheManager_File::purge()
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    67
	 */
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    68
	
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    69
	public function purge($cache_id)
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    70
	{
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    71
		return xcache_unset($this->prefix . $cache_id);
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    72
	}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    73
}
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    74
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    75
class CacheManager_File
a52cac1b190d Cacher: Add XCache support (partial)
Dan Fuhry <dan@enanocms.org>
parents: 1227
diff changeset
    76
{
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    77
	/**
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    78
 	* Fetch a cached piece of data.
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    79
 	* @param string Cache ID. The timestamp is checked automatically.
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    80
 	*/
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    81
	
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    82
	public function fetch($cache_id)
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    83
	{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    84
		if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    85
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    86
			throw new Exception('Cache ID must be letters, numbers, and underscores.');
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    87
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    88
		$cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php";
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    89
		if ( file_exists($cache_file) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    90
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    91
			require($cache_file);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    92
			if ( isset($_cache_data) && isset($_cache_ttl) && isset($_cache_timestamp) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    93
			{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    94
				$cache_expire = $_cache_timestamp + ( 60 * $_cache_ttl);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    95
				if ( $cache_expire >= time() || $_cache_ttl === -1 )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    96
				{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    97
					return $_cache_data;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    98
				}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
    99
			}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   100
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   101
		return false;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   102
	}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   103
	
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   104
	/**
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   105
 	* Stores an array by var_export()ing it and saving it to disk.
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   106
 	* @param string Cache ID - human readable name for this store (letters, numbers, underscores)
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   107
 	* @param array Data to store
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   108
 	* @param int TTL for the cached array, in minutes. Defaults to 20. If set to -1, caches indefinitely.
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   109
 	* @return bool True on success, false on failure
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   110
 	*/
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   111
	
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   112
	public function store($cache_id, $data, $ttl = 20)
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   113
	{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   114
		if ( getConfig('cache_thumbs') != '1' )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   115
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   116
			// caching disabled
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   117
			return false;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   118
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   119
		if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   120
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   121
			throw new Exception('Cache ID must be letters, numbers, and underscores.');
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   122
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   123
		if ( !is_int($ttl) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   124
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   125
			throw new Exception('TTL must be an integer');
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   126
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   127
		
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   128
		$cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php";
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   129
		if ( file_exists($cache_file) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   130
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   131
			@unlink($cache_file);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   132
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   133
		$fh = @fopen($cache_file, 'w');
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   134
		if ( !$fh )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   135
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   136
			throw new Exception('Failed to open file for writing.');
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   137
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   138
		$exported = Language::var_export_string($data);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   139
		$now = time();
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   140
		$content = <<<EOF
607
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   141
<?php
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   142
/**
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   143
 * Automatically generated cache file.
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   144
 * Do not edit this file as it will expire and be overwritten $ttl minutes from its creation.
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   145
 */
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   146
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   147
\$_cache_ttl = $ttl;
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   148
\$_cache_timestamp = $now;
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   149
\$_cache_data = $exported;
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   150
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   151
EOF;
1227
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   152
		fwrite($fh, $content);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   153
		fclose($fh);
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   154
		
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   155
		return true;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   156
	}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   157
	
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   158
	/**
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   159
 	* Deletes a cached item.
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   160
 	* @param string Cache ID.
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   161
 	* @return bool true on success or if item doesn't exist, false on failure
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   162
 	*/
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   163
	
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   164
	public function purge($cache_id)
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   165
	{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   166
		if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   167
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   168
			throw new Exception('Cache ID must be letters, numbers, and underscores.');
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   169
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   170
		
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   171
		$cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php";
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   172
		if ( file_exists($cache_file) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   173
		{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   174
			if ( unlink($cache_file) )
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   175
			{
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   176
				return true;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   177
			}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   178
			return false;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   179
		}
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   180
		return true;
bdac73ed481e Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents: 1081
diff changeset
   181
	}
607
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   182
}
935f3799b654 First stab at cache management backend. Everything seems to have been tested and working so far, but a number of things require a more specialized cache and can't go through the framework (e.g. user ranks which use references to map usernames to user IDs)
Dan
parents:
diff changeset
   183