|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpha\Util\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Alpha\Util\Logging\Logger; |
|
6
|
|
|
use Alpha\Util\Config\ConfigProvider; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* An implementation of the CacheProviderInterface interface that uses the |
|
10
|
|
|
* file system (app.file.store.dir) as the target store. |
|
11
|
|
|
* |
|
12
|
|
|
* @since 3.0.0 |
|
13
|
|
|
* |
|
14
|
|
|
* @author John Collins <[email protected]> |
|
15
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
|
16
|
|
|
* @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework). |
|
17
|
|
|
* All rights reserved. |
|
18
|
|
|
* |
|
19
|
|
|
* <pre> |
|
20
|
|
|
* Redistribution and use in source and binary forms, with or |
|
21
|
|
|
* without modification, are permitted provided that the |
|
22
|
|
|
* following conditions are met: |
|
23
|
|
|
* |
|
24
|
|
|
* * Redistributions of source code must retain the above |
|
25
|
|
|
* copyright notice, this list of conditions and the |
|
26
|
|
|
* following disclaimer. |
|
27
|
|
|
* * Redistributions in binary form must reproduce the above |
|
28
|
|
|
* copyright notice, this list of conditions and the |
|
29
|
|
|
* following disclaimer in the documentation and/or other |
|
30
|
|
|
* materials provided with the distribution. |
|
31
|
|
|
* * Neither the name of the Alpha Framework nor the names |
|
32
|
|
|
* of its contributors may be used to endorse or promote |
|
33
|
|
|
* products derived from this software without specific |
|
34
|
|
|
* prior written permission. |
|
35
|
|
|
* |
|
36
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
|
37
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
|
38
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
39
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
40
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
41
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
42
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
43
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
44
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
45
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
46
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
|
47
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
48
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
49
|
|
|
* </pre> |
|
50
|
|
|
*/ |
|
51
|
|
|
class CacheProviderFile implements CacheProviderInterface |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Trace logger. |
|
55
|
|
|
* |
|
56
|
|
|
* @var \Alpha\Util\Logging\Logger |
|
57
|
|
|
* |
|
58
|
|
|
* @since 3.0.0 |
|
59
|
|
|
*/ |
|
60
|
|
|
private static $logger = null; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Constructor. |
|
64
|
|
|
* |
|
65
|
|
|
* @since 3.0.0 |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct() |
|
68
|
|
|
{ |
|
69
|
|
|
self::$logger = new Logger('CacheProviderFile'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function get($key) |
|
76
|
|
|
{ |
|
77
|
|
|
self::$logger->debug('>>get(key=['.$key.'])'); |
|
78
|
|
|
|
|
79
|
|
|
self::$logger->debug('Getting value for key ['.$key.']'); |
|
80
|
|
|
|
|
81
|
|
|
$config = ConfigProvider::getInstance(); |
|
82
|
|
|
$filepath = $config->get('app.file.store.dir').'/cache/files/'.$key; |
|
83
|
|
|
|
|
84
|
|
|
if (file_exists($filepath)) { |
|
85
|
|
|
return unserialize(file_get_contents($filepath)); |
|
86
|
|
|
} else { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function set($key, $value, $expiry = 0) |
|
95
|
|
|
{ |
|
96
|
|
|
self::$logger->debug('Setting value for key ['.$key.']'); |
|
97
|
|
|
|
|
98
|
|
|
$config = ConfigProvider::getInstance(); |
|
99
|
|
|
$filepath = $config->get('app.file.store.dir').'/cache/files/'.$key; |
|
100
|
|
|
|
|
101
|
|
|
file_put_contents($filepath, serialize($value)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function delete($key) |
|
108
|
|
|
{ |
|
109
|
|
|
self::$logger->debug('Removing value for key ['.$key.']'); |
|
110
|
|
|
|
|
111
|
|
|
$config = ConfigProvider::getInstance(); |
|
112
|
|
|
$filepath = $config->get('app.file.store.dir').'/cache/files/'.$key; |
|
113
|
|
|
|
|
114
|
|
|
unlink($filepath); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|