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
|
<?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(); }
public static function get($key, $closure) { $key = static::get_namespace() . $key; if (!static::$adapter) return $closure();
if (!($value = static::$adapter->read($key))) static::$adapter->write($key,($value = $closure()),static::$options['expire']);
return $value; }
private static function get_namespace() { return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . "::") : ""; } } ?>
|