FilesystemGhost   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 18
eloc 39
dl 0
loc 134
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 11 3
A set() 0 20 5
A has() 0 23 6
A clear() 0 19 4
1
<?php namespace Comodojo\Cache\Drivers;
2
3
use \Exception;
4
5
/**
6
 * @package     Comodojo Cache
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
class FilesystemGhost extends FilesystemXattr {
22
23
    const DRIVER_NAME = "filesystem-ghost";
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 28
    public function set($key, $namespace, $value, $ttl = null) {
29
30 28
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
31 28
        $cacheGhost = $this->cache_folder."$key-$namespace.expire";
32
33 28
        if ( $ttl == null || $ttl == 0 ) {
34 25
            $ttl = 0;
35
        } else {
36 4
            $ttl = time() + intval($ttl);
37
        }
38
39 28
        $cached = @file_put_contents($cacheFile, $value, LOCK_EX);
40
41 28
        if ( $cached === false ) throw new Exception("Error writing cache object $cacheFile");
42
43 28
        $tagged = @file_put_contents($cacheGhost, $ttl, LOCK_EX);
44
45 28
        if ( $tagged === false ) throw new Exception("Error writing cache ttl $cacheFile");
46
47 28
        return true;
48
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 7
    public function delete($key, $namespace) {
55
56 7
        $result = [];
57
58 7
        $file_list = glob($this->cache_folder."$key-$namespace.{cache,expire}", GLOB_BRACE);
59
60 7
        if ( empty($file_list) ) return false;
61
62 7
        foreach ( $file_list as $file ) $result[] = unlink($file);
63
64 7
        return !in_array(false, $result);
65
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 11
    public function clear($namespace = null) {
72
73 11
        $result = [];
74
75 11
        if ( $namespace === null ) {
76
77 9
            $file_list = glob($this->cache_folder."*.{cache,expire}", GLOB_BRACE);
78
79
        } else {
80
81 2
            $file_list = glob($this->cache_folder."*-$namespace.{cache,expire}", GLOB_BRACE);
82
83
        }
84
85 11
        if ( empty($file_list) ) return true;
86
87 5
        foreach ( $file_list as $file ) $result[] = unlink($file);
88
89 5
        return !in_array(false, $result);
90
91
    }
92
93
    // public function getMultiple(array $keys, $namespace) {
94
    //
95
    //     $result = [];
96
    //
97
    //     foreach ($keys as $key) {
98
    //         $result[$key] = $this->get($key, $namespace);
99
    //     }
100
    //
101
    //     return $result;
102
    //
103
    // }
104
    //
105
    // public function setMultiple(array $key_values, $namespace, $ttl = null) {
106
    //
107
    //     $result = [];
108
    //
109
    //     foreach ($keys as $key => $value) {
110
    //         $result[] = $this->set($key, $namespace, $value, $ttl);
111
    //     }
112
    //
113
    //     return !in_array(false, $result);
114
    //
115
    // }
116
    //
117
    // public function deleteMultiple(array $keys, $namespace) {
118
    //
119
    //     $result = [];
120
    //
121
    //     foreach ($keys as $key) {
122
    //         $result[] = $this->delete($key, $namespace);
123
    //     }
124
    //
125
    //     return !in_array(false, $result);
126
    //
127
    // }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 35
    public function has($key, $namespace) {
133
134 35
        $time = time();
135
136 35
        $cacheFile = $this->cache_folder."$key-$namespace.cache";
137 35
        $cacheGhost = $this->cache_folder."$key-$namespace.expire";
138
139 35
        if ( file_exists($cacheFile) && file_exists($cacheGhost) ) {
140
141 21
            $expire = @file_get_contents($cacheGhost);
142
143 21
            if ( $expire === false ) {
144
                throw new Exception("Error reading cache ttl for $cacheFile");
145 21
            } else if ( intval($expire) < $time && intval($expire) !== 0 ) {
146 1
                $this->delete($key, $namespace);
147 1
                return false;
148
            } else {
149 20
                return true;
150
            }
151
152
        }
153
154 22
        return false;
155
156
    }
157
158
    // public function stats() {
159
    //
160
    //     return ['objects' => count(glob($this->cache_folder."*.cache"))];
161
    //
162
    // }
163
164
}
165