Completed
Pull Request — master (#1)
by Joao
02:45
created

FileSystemCacheEngine::get()   D

Complexity

Conditions 9
Paths 11

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
c 0
b 0
f 0
rs 4.909
cc 9
eloc 29
nc 11
nop 2
1
<?php
2
3
namespace ByJG\Cache\Engine;
4
5
use ByJG\Cache\CacheEngineInterface;
6
use Exception;
7
use Psr\Log\NullLogger;
8
9
class FileSystemCacheEngine implements CacheEngineInterface
10
{
11
12
    protected $logger = null;
13
14
    protected $prefix = null;
15
16
    public function __construct($prefix = 'cache', $logger = null)
17
    {
18
        $this->prefix = $prefix;
19
20
        $this->logger = $logger;
21
        if (is_null($logger)) {
22
            $this->logger = new NullLogger();
23
        }
24
    }
25
26
    /**
27
     * @param string $key The object KEY
28
     * @param int $ttl IGNORED IN MEMCACHED.
29
     * @return object Description
30
     */
31
    public function get($key, $ttl = 0)
32
    {
33
        if ($ttl === false) {
34
            $this->logger->info("[Filesystem cache] Ignored  $key because TTL=FALSE");
35
            return null;
36
        }
37
38
        // Check if file is Locked
39
        $fileKey = $this->fixKey($key);
40
        $lockFile = $fileKey . ".lock";
41
        if (file_exists($lockFile)) {
42
            $this->logger->info("[Filesystem cache] Locked! $key. Waiting...");
43
            $lockTime = filemtime($lockFile);
44
45
            while (true) {
46
                if (!file_exists($lockFile)) {
47
                    $this->logger->info("[Filesystem cache] Lock released for '$key'");
48
                    break;
49
                }
50
                if (intval(time() - $lockTime) > 20) {  // Wait for 10 seconds
51
                    $this->logger->info("[Filesystem cache] Gave up to wait unlock. Release lock for '$key'");
52
                    $this->unlock($key);
53
                    return null;
54
                }
55
                sleep(1); // 1 second
56
            }
57
        }
58
59
        // Check if file exists
60
        if (file_exists($fileKey)) {
61
            $fileAge = filemtime($fileKey);
62
63
            if (($ttl > 0) && (intval(time() - $fileAge) > $ttl)) {
64
                $this->logger->info("[Filesystem cache] File too old. Ignoring '$key'");
65
                return null;
66
            } else {
67
                $this->logger->info("[Filesystem cache] Get '$key'");
68
                return unserialize(file_get_contents($fileKey));
69
            }
70
        } else {
71
            $this->logger->info("[Filesystem cache] Not found '$key'");
72
            return null;
73
        }
74
    }
75
76
    /**
77
     * @param string $key The object Key
78
     * @param object $object The object to be cached
79
     * @param int $ttl The time to live in seconds of this objects
80
     * @return bool If the object is successfully posted
81
     */
82
    public function set($key, $object, $ttl = 0)
83
    {
84
        $fileKey = $this->fixKey($key);
85
86
        $this->logger->info("[Filesystem cache] Set '$key' in FileSystem");
87
88
        try {
89
            if (file_exists($fileKey)) {
90
                unlink($fileKey);
91
            }
92
93
            if (is_null($object)) {
94
                return false;
95
            }
96
97
            if (is_string($object) && (strlen($object) === 0)) {
98
                touch($fileKey);
99
            } else {
100
                file_put_contents($fileKey, serialize($object));
101
            }
102
        } catch (Exception $ex) {
103
            $this->logger->warning("[Filesystem cache] I could not write to cache on file '" . basename($key) . "'. Switching to nocache=true mode.");
104
            return false;
105
        }
106
107
        return true;
108
    }
109
110
    /**
111
     * Unlock resource
112
     * @param string $key
113
     */
114
    public function release($key)
115
    {
116
        $this->set($key, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
    }
118
119
    /**
120
     * @param string $key The object Key
121
     * @param string $content The object to be cached
122
     * @param int $ttl The time to live in seconds of this objects
123
     * @return bool If the object is successfully posted
124
     */
125
    public function append($key, $content, $ttl = 0)
126
    {
127
        $fileKey = $this->fixKey($key);
128
129
        $this->logger->info("[Filesystem cache] Append '$key' in FileSystem");
130
131
        try {
132
            file_put_contents($fileKey, serialize($content), true);
133
        } catch (Exception $ex) {
134
            $this->logger->warning("[Filesystem cache] I could not write to cache on file '" . basename($key) . "'. Switching to nocache=true mode.");
135
            return false;
136
        }
137
138
        return true;
139
    }
140
141
    /**
142
     * Lock resource before set it.
143
     * @param string $key
144
     */
145
    public function lock($key)
146
    {
147
        $this->logger->info("[Filesystem cache] Lock '$key'");
148
149
        $lockFile = $this->fixKey($key) . ".lock";
150
151
        try {
152
            file_put_contents($lockFile, date('c'));
153
        } catch (Exception $ex) {
154
            // Ignoring... Set will cause an error
155
        }
156
    }
157
158
    /**
159
     * UnLock resource after set it.
160
     * @param string $key
161
     */
162
    public function unlock($key)
163
    {
164
        
165
        $this->logger->info("[Filesystem cache] Unlock '$key'");
166
167
        $lockFile = $this->fixKey($key) . ".lock";
168
169
        if (file_exists($lockFile)) {
170
            unlink($lockFile);
171
        }
172
    }
173
174
    public function isAvailable()
175
    {
176
        return is_writable(dirname($this->fixKey('test')));
177
    }
178
179
    protected function fixKey($key)
180
    {
181
        return sys_get_temp_dir() . '/'
182
            . $this->prefix
183
            . '-' . preg_replace("/[\/\\\]/", "#", $key)
184
            . '.cache';
185
    }
186
}
187