|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: jaredchu |
|
5
|
|
|
* Date: 03/08/2017 |
|
6
|
|
|
* Time: 17:17 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace JC; |
|
10
|
|
|
|
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class SimpleCache |
|
15
|
|
|
* @package JC |
|
16
|
|
|
*/ |
|
17
|
|
|
class BaseCache |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param $key |
|
21
|
|
|
* @param $data |
|
22
|
|
|
* @return bool |
|
23
|
|
|
* |
|
24
|
|
|
* Add object to cache list and save object as json file |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function add($key, $data, $ttl = 0) |
|
27
|
|
|
{ |
|
28
|
|
|
$tempFilePath = static::getTempFile($key) ?: static::createTempFile($key, $ttl); |
|
29
|
|
|
return (bool)file_put_contents($tempFilePath, static::encode($data)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param $key |
|
34
|
|
|
* @param string $className |
|
35
|
|
|
* @return object|bool |
|
36
|
|
|
* |
|
37
|
|
|
* Fetch object from cache |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function fetch($key, $className) |
|
40
|
|
|
{ |
|
41
|
|
|
if (BaseManager::has($key)) { |
|
42
|
|
|
$dataString = file_get_contents(static::getTempFile($key)); |
|
43
|
|
|
|
|
44
|
|
|
$mapper = new JCMapper(); |
|
45
|
|
|
return $mapper->map(static::decode($dataString), (new ReflectionClass($className))->newInstanceWithoutConstructor()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $key |
|
53
|
|
|
* @return bool |
|
54
|
|
|
* |
|
55
|
|
|
* Remove object from cache |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function remove($key) |
|
58
|
|
|
{ |
|
59
|
|
|
if (BaseManager::has($key)) { |
|
60
|
|
|
unlink(BaseManager::get($key)); |
|
61
|
|
|
return BaseManager::remove($key); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $key |
|
69
|
|
|
* @return bool |
|
70
|
|
|
* |
|
71
|
|
|
* Check object is cached or not |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function exists($key) |
|
74
|
|
|
{ |
|
75
|
|
|
return file_exists(static::getTempFile($key)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $key |
|
80
|
|
|
* @return bool|string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected static function getTempFile($key) |
|
83
|
|
|
{ |
|
84
|
|
|
if (BaseManager::has($key)) { |
|
85
|
|
|
return BaseManager::get($key); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $key |
|
93
|
|
|
* @return bool|string |
|
94
|
|
|
*/ |
|
95
|
|
|
protected static function createTempFile($key, $ttl) |
|
96
|
|
|
{ |
|
97
|
|
|
$tempFilePath = tempnam(sys_get_temp_dir(), $key); |
|
98
|
|
|
BaseManager::set($key, $tempFilePath, $ttl); |
|
99
|
|
|
|
|
100
|
|
|
return $tempFilePath; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param object $object |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
protected static function encode($object) |
|
108
|
|
|
{ |
|
109
|
|
|
return json_encode($object); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $string |
|
114
|
|
|
* @return object |
|
115
|
|
|
*/ |
|
116
|
|
|
protected static function decode($string) |
|
117
|
|
|
{ |
|
118
|
|
|
return json_decode($string); |
|
119
|
|
|
} |
|
120
|
|
|
} |