-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathCache.php
115 lines (100 loc) · 2.77 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace ActiveRecord;
use Closure;
/**
* Cache::get('the-cache-key', function() {
* # this gets executed when cache is stale
* return "your cacheable datas";
* });
*/
class Cache
{
static $adapter = null;
static $options = array();
/**
* Initializes the cache.
*
* With the $options array it's possible to define:
* - expiration of the key, (time in seconds)
* - a namespace for the key
*
* this last one is useful in the case two applications use
* a shared key/store (for instance a shared Memcached db)
*
* Ex:
* $cfg_ar = ActiveRecord\Config::instance();
* $cfg_ar->set_cache('memcache://localhost:11211',array('namespace' => 'my_cool_app',
* 'expire' => 120
* ));
*
* In the example above all the keys expire after 120 seconds, and the
* all get a postfix 'my_cool_app'.
*
* (Note: expiring needs to be implemented in your cache store.)
*
* @param string $url URL to your cache server
* @param array $options Specify additional options
*/
public static function initialize($url, $options=array())
{
if ($url)
{
$url = parse_url($url);
$file = ucwords(Inflector::instance()->camelize($url['scheme']));
$class = "ActiveRecord\\$file";
require_once __DIR__ . "/cache/$file.php";
static::$adapter = new $class($url);
}
else
static::$adapter = null;
static::$options = array_merge(array('expire' => 30, 'namespace' => ''),$options);
}
public static function flush()
{
if (static::$adapter)
static::$adapter->flush();
}
/**
* Attempt to retrieve a value from cache using a key. If the value is not found, then the closure method
* will be invoked, and the result will be stored in cache using that key.
* @param $key
* @param $closure
* @param $expire in seconds
* @return mixed
*/
public static function get($key, $closure, $expire=null)
{
if (!static::$adapter)
return $closure();
if (is_null($expire))
{
$expire = static::$options['expire'];
}
$key = static::get_namespace() . $key;
if (!($value = static::$adapter->read($key)))
static::$adapter->write($key, ($value = $closure()), $expire);
return $value;
}
public static function set($key, $var, $expire=null)
{
if (!static::$adapter)
return;
if (is_null($expire))
{
$expire = static::$options['expire'];
}
$key = static::get_namespace() . $key;
return static::$adapter->write($key, $var, $expire);
}
public static function delete($key)
{
if (!static::$adapter)
return;
$key = static::get_namespace() . $key;
return static::$adapter->delete($key);
}
private static function get_namespace()
{
return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . "::") : "";
}
}