-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheingMemoizer.php
147 lines (118 loc) · 3.33 KB
/
CacheingMemoizer.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace CyberDuck\CacheingMemoizer;
class CacheingMemoizer {
protected static $instance = null;
protected static $cacheDuration = 300;
protected $memoizeKeys = [];
protected $shutdownFunctions = [];
use MemoizesResults {
memoize as traitMemoize;
}
protected $cacheRetrievalFunction = null;
private function __construct(){
$this->cacheRetrievalFunction = function () {
return false;
};
}
/**
* Add function to be called at the end of request to
* cache memoized values
*
* signature should be ($key, $value)
*
* @param callable $function
*/
public static function addShutdownFunction($function)
{
self::getInstance()->shutdownFunctions[] = $function;
}
/**
* Retrieve singleton instance of SimpleMemoizer
*
*@return \CyberDuck\CacheingMemoizer\CacheingMemoizer
*/
public static function getInstance(){
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Set function to retrieve value from cache (if it's there)
*
* @param $function
*/
public static function setCacheRetrievalFunction($function)
{
self::getInstance()->cacheRetrievalFunction = $function;
}
/**
* Should be called at end of request by framework. Effect
* will be to call the shutdown functions added with
* addShutdownFunction
*/
public static function shutdown()
{
self::getInstance()->doShutdown();
}
protected function doShutdown()
{
array_walk($this->shutdownFunctions, function($function){
foreach($this->memoizeKeys as $key => $value) {
$function($key, $this->memoizedStuff[$key]);
}
});
}
/**
* Convenience method for sharing cache duration amongst
* different cache systems
*
* @return int
*/
public static function getCacheDuration()
{
return self::$cacheDuration;
}
/**
* Convenience method for sharing cache duration amongst
* different cache systems
*
* @return int
*/
public static function setCacheDuration($duration)
{
self::$cacheDuration = $duration;
}
/**
* Memoize result globally with retrieval and storage
* in cache
*
* @param $key
* @param $function
* @param array ...$params
*
* @return mixed
*/
public function memoizeWithCache($key, $function, ...$params)
{
return $this->memoize($key, function($key, $function, $params) {
if ($value = call_user_func($this->cacheRetrievalFunction, $key)) {
return $value;
}
//only set memo key here so it's not continually refreshed
$this->memoizeKeys[$this->getMemoizeKey($key)] = $this->getMemoizeKey($key);
return $function(...$params);
}, $key, $function, $params);
}
/**
* See trait documentation. This adds public access
* to protected method.
*/
public function memoize($key, $function, ...$params)
{
return $this->traitMemoize($key, $function, ...$params);
}
protected function getCacheKey($key) {
return 'saved-memo-' . md5($key);
}
}