forked from flightphp/cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_cache_meta.php
More file actions
30 lines (22 loc) · 985 Bytes
/
Copy pathsimple_cache_meta.php
File metadata and controls
30 lines (22 loc) · 985 Bytes
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
<?php
use flight\Cache;
require_once __DIR__ . "/../vendor/autoload.php";
$cache = new Cache(__DIR__ . "/../testcache/");
$data = $cache->refreshIfExpired("simple-cache-meta-test", function () {
echo "Refreshing data!" . PHP_EOL;
return date("H:i:s"); // return data to be cached
}, 10, true); // true = return with metadata
/*
Example cached item retrieved with metadata:
{
"time":1511667506, <-- save unix timestamp
"expire":10, <-- expire time in seconds
"data":"04:38:26", <-- unserialized data
"permanent":false
}
Using metadata, we can, for example, calculate when item was saved or when it expires
We can also access the data itself with the "data" key
*/
$expiresin = ($data["time"] + $data["expire"]) - time(); // get unix timestamp when data expires and subtract current timestamp from it
$cacheddate = $data["data"]; // we access the data itself with the "data" key
echo "Latest cache save: $cacheddate, expires in $expiresin seconds";