php define ROOT realpath dirname __FILE__ DIRECTORY_SEPARATOR require_

 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
<?php
define('ROOT', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
require_once ROOT . 'cache.php';
define('CACHE_CLASS', 'file_cache');
define('CACHE_PATH', ROOT . 'tmp');
cache('foo', 'bar', 1);
echo (int) cached('foo'), PHP_EOL;
echo cache('foo'), PHP_EOL;
sleep(2);
echo (int) cached('foo'), PHP_EOL;
class sample_class extends stdClass {
private $value = null;
public function __construct() {
$this->value = (string) rand(0, 100);
}
public function fetch_value($foo, $bar) {
echo 'fetch_value', PHP_EOL;
return $this->value;
}
}
$object_instance = new sample_class();
$arguments = new ArrayObject(array('foo', 'bar'));
cache_decorator(array($object_instance, 'fetch_value'), $arguments, 1);
cache_decorator(array($object_instance, 'fetch_value'), $arguments, 1);
sleep(2);
cache_decorator(array($object_instance, 'fetch_value'), $arguments, 1);