-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
122 lines (107 loc) · 3.28 KB
/
index.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
<?php
use JoeBengalen\Cache\Pool;
use JoeBengalen\Cache\Repository;
error_reporting(-1);
ini_set('display_errors', 1);
// Set default timezone
date_default_timezone_set('Europe/Amsterdam');
require_once 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$repo = new Repository\FileRepository(__DIR__);
//$repo = new Repository\SessionRepository();
$pool = new Pool($repo);
$item = $pool->getItem('test2');
if (!$item->isHit()) {
$item->set('some value', 5);
$pool->save($item);
}
// Note that the order of keys is the same as requested.
d($pool->getItems(['test1', 'test2', 'test3']));
// All contain invalid keys!
//$pool->getItems(['d{mmy', 'd}mmy', 'd(mmy', 'd)mmy', 'd/mmy', 'd\mmy', ]);
/**
* Gets a list of available widgets.
*
* In this case, we assume the widget list changes so rarely that we want
* the list cached forever until an explicit clear.
*/
function get_widget_list()
{
$pool = get_cache_pool('widgets');
$item = $pool->getItem('widget_list');
if (!$item->isHit()) {
$value = compute_expensive_widget_list();
$item->set($value);
$pool->save($item);
}
return $item->get();
}
/**
* Caches a list of available widgets.
*
* In this case, we assume a list of widgets has been computed and we want
* to cache it, regardless of what may already be cached.
*/
function save_widget_list($list)
{
$pool = get_cache_pool('widgets');
$item = $pool->getItem('widget_list');
$item->set($list);
$pool->save($item);
}
/**
* Clears the list of available widgets.
*
* In this case, we simply want to remove the widget list from the cache. We
* don't care if it was set or not; the post condition is simply "no longer set".
*/
function clear_widget_list()
{
$pool = get_cache_pool('widgets');
$pool->deleteItems(['widget_list']);
}
/**
* Clears all widget information.
*
* In this case, we want to empty the entire widget pool. There may be other
* pools in the application that will be unaffected.
*/
function clear_widget_cache()
{
$pool = get_cache_pool('widgets');
$pool->clear();
}
/**
* Load widgets.
*
* We want to get back a list of widgets, of which some are cached and some
* are not. This of course assumes that loading from the cache is faster than
* whatever the non-cached loading mechanism is.
*
* In this case, we assume widgets may change frequently so we only allow them
* to be cached for an hour (3600 seconds). We also cache newly-loaded objects
* back to the pool en masse.
*
* Note that a real implementation would probably also want a multi-load
* operation for widgets, but that's irrelevant for this demonstration.
*/
function load_widgets(array $ids)
{
$pool = get_cache_pool('widgets');
$keys = array_map(function($id) { return 'widget.' . $id; }, $ids);
$items = $pool->getItems($keys);
$widgets = array();
foreach ($items as $key => $item) {
if ($item->isHit()) {
$value = $item->get();
}
else {
$value = expensive_widget_load($id);
$item->set($value, 3600);
$pool->saveDeferred($item, true);
}
$widget[$value->id()] = $value;
}
$pool->commit(); // If no items were deferred this is a no-op.
return $widgets;
}
function d() { call_user_func_array('var_dump', func_get_args()); }