1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: jaredchu |
5
|
|
|
* Date: 03/08/2017 |
6
|
|
|
* Time: 17:17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace JC\Cache; |
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|mixed|bool |
36
|
|
|
* |
37
|
|
|
* Fetch data from cache |
38
|
|
|
*/ |
39
|
|
|
public static function fetch($key, $className = '', $isArray = false) |
40
|
|
|
{ |
41
|
|
|
if (Manager::has($key)) { |
42
|
|
|
$dataString = file_get_contents(static::getTempFile($key)); |
43
|
|
|
$rawData = static::decode($dataString); |
44
|
|
|
|
45
|
|
|
if (!empty($className)) { |
46
|
|
|
$mapper = new JCMapper(); |
47
|
|
|
return $mapper->map($rawData, (new ReflectionClass($className))->newInstanceWithoutConstructor()); |
48
|
|
|
} else { |
49
|
|
|
return $isArray ? (array)$rawData : $rawData; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $key |
58
|
|
|
* @return array |
59
|
|
|
* |
60
|
|
|
* Fetch array from cache |
61
|
|
|
*/ |
62
|
|
|
public static function fetchArray($key) |
63
|
|
|
{ |
64
|
|
|
return static::fetch($key, '', true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $key |
69
|
|
|
* @return bool |
70
|
|
|
* |
71
|
|
|
* Remove object from cache |
72
|
|
|
*/ |
73
|
|
|
public static function remove($key) |
74
|
|
|
{ |
75
|
|
|
if (Manager::has($key)) { |
76
|
|
|
unlink(Manager::get($key)); |
77
|
|
|
return Manager::remove($key); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $key |
85
|
|
|
* @return bool |
86
|
|
|
* |
87
|
|
|
* Check object is cached or not |
88
|
|
|
*/ |
89
|
|
|
public static function exists($key) |
90
|
|
|
{ |
91
|
|
|
return file_exists(static::getTempFile($key)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $key |
96
|
|
|
* @return bool|string |
97
|
|
|
*/ |
98
|
|
|
protected static function getTempFile($key) |
99
|
|
|
{ |
100
|
|
|
if (Manager::has($key)) { |
101
|
|
|
return Manager::get($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $key |
109
|
|
|
* @return bool|string |
110
|
|
|
*/ |
111
|
|
|
protected static function createTempFile($key, $ttl) |
112
|
|
|
{ |
113
|
|
|
$tempFilePath = tempnam(sys_get_temp_dir(), $key); |
114
|
|
|
Manager::set($key, $tempFilePath, $ttl); |
115
|
|
|
|
116
|
|
|
return $tempFilePath; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param object $object |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected static function encode($object) |
124
|
|
|
{ |
125
|
|
|
return json_encode($object); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $string |
130
|
|
|
* @return object |
131
|
|
|
*/ |
132
|
|
|
protected static function decode($string) |
133
|
|
|
{ |
134
|
|
|
return json_decode($string); |
135
|
|
|
} |
136
|
|
|
} |