| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Container for all shared caching methods. This is not intended to be instantiated directly, but is
|
|
|
4 |
* extended by the cache-specific classes.
|
|
|
5 |
*
|
|
|
6 |
* @version 2012.01.28
|
|
|
7 |
* @copyright 2006-2012 Ryan Parman
|
|
|
8 |
* @copyright 2006-2010 Foleeo, Inc.
|
|
|
9 |
* @copyright 2012 Amazon.com, Inc. or its affiliates.
|
|
|
10 |
* @copyright 2008-2010 Contributors
|
|
|
11 |
* @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
|
|
|
12 |
* @link http://github.com/skyzyx/cachecore CacheCore
|
|
|
13 |
* @link http://getcloudfusion.com CloudFusion
|
|
|
14 |
*/
|
|
|
15 |
class CacheCore
|
|
|
16 |
{
|
|
|
17 |
/**
|
|
|
18 |
* A name to uniquely identify the cache object by.
|
|
|
19 |
*/
|
|
|
20 |
var $name;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Where to store the cache.
|
|
|
24 |
*/
|
|
|
25 |
var $location;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The number of seconds before a cache object is considered stale.
|
|
|
29 |
*/
|
|
|
30 |
var $expires;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Used internally to uniquely identify the location + name of the cache object.
|
|
|
34 |
*/
|
|
|
35 |
var $id;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Stores the time when the cache object was created.
|
|
|
39 |
*/
|
|
|
40 |
var $timestamp;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Stores whether or not the content should be gzipped when stored
|
|
|
44 |
*/
|
|
|
45 |
var $gzip;
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
/*%******************************************************************************************%*/
|
|
|
49 |
// CONSTRUCTOR
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Constructs a new instance of this class.
|
|
|
53 |
*
|
|
|
54 |
* @param string $name (Required) A name to uniquely identify the cache object.
|
|
|
55 |
* @param string $location (Required) The location to store the cache object in. This may vary by cache method.
|
|
|
56 |
* @param integer $expires (Required) The number of seconds until a cache object is considered stale.
|
|
|
57 |
* @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
|
|
|
58 |
* @return object Reference to the cache object.
|
|
|
59 |
*/
|
|
|
60 |
public function __construct($name, $location, $expires, $gzip = true)
|
|
|
61 |
{
|
|
|
62 |
if (!extension_loaded('zlib'))
|
|
|
63 |
{
|
|
|
64 |
$gzip = false;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$this->name = $name;
|
|
|
68 |
$this->location = $location;
|
|
|
69 |
$this->expires = $expires;
|
|
|
70 |
$this->gzip = $gzip;
|
|
|
71 |
|
|
|
72 |
return $this;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Allows for chaining from the constructor. Requires PHP 5.3 or newer.
|
|
|
77 |
*
|
|
|
78 |
* @param string $name (Required) A name to uniquely identify the cache object.
|
|
|
79 |
* @param string $location (Required) The location to store the cache object in. This may vary by cache method.
|
|
|
80 |
* @param integer $expires (Required) The number of seconds until a cache object is considered stale.
|
|
|
81 |
* @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
|
|
|
82 |
* @return object Reference to the cache object.
|
|
|
83 |
*/
|
|
|
84 |
public static function init($name, $location, $expires, $gzip = true)
|
|
|
85 |
{
|
|
|
86 |
if (version_compare(PHP_VERSION, '5.3.0', '<'))
|
|
|
87 |
{
|
|
|
88 |
throw new Exception('PHP 5.3 or newer is required to use CacheCore::init().');
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$self = get_called_class();
|
|
|
92 |
return new $self($name, $location, $expires, $gzip);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Provides a simple, straightforward cache-logic mechanism. Useful for non-complex response caches.
|
|
|
97 |
*
|
|
|
98 |
* @param string|function $callback (Required) The name of the function to fire when we need to fetch new data to cache.
|
|
|
99 |
* @param array params (Optional) Parameters to pass into the callback function, as an array.
|
|
|
100 |
* @return array The cached data being requested.
|
|
|
101 |
*/
|
|
|
102 |
public function response_manager($callback, $params = null)
|
|
|
103 |
{
|
|
|
104 |
// Automatically handle $params values.
|
|
|
105 |
$params = is_array($params) ? $params : array($params);
|
|
|
106 |
|
|
|
107 |
if ($data = $this->read())
|
|
|
108 |
{
|
|
|
109 |
if ($this->is_expired())
|
|
|
110 |
{
|
|
|
111 |
if ($data = call_user_func_array($callback, $params))
|
|
|
112 |
{
|
|
|
113 |
$this->update($data);
|
|
|
114 |
}
|
|
|
115 |
else
|
|
|
116 |
{
|
|
|
117 |
$this->reset();
|
|
|
118 |
$data = $this->read();
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
else
|
|
|
123 |
{
|
|
|
124 |
if ($data = call_user_func_array($callback, $params))
|
|
|
125 |
{
|
|
|
126 |
$this->create($data);
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
return $data;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
/*%******************************************************************************************%*/
|
|
|
136 |
// CORE DEPENDENCIES
|
|
|
137 |
|
|
|
138 |
// Include the ICacheCore interface.
|
|
|
139 |
if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'icachecore.interface.php'))
|
|
|
140 |
{
|
|
|
141 |
include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'icachecore.interface.php';
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
/*%******************************************************************************************%*/
|
|
|
146 |
// EXCEPTIONS
|
|
|
147 |
|
|
|
148 |
class CacheCore_Exception extends Exception {}
|