Skip to content

Commit a82655d

Browse files
committed
refactoring
1 parent d3610bb commit a82655d

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

src/Bridges/CacheLatte/CacheMacro.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,18 @@ public static function createCache(
126126
public static function endCache(array &$parents, array $args = null): void
127127
{
128128
$helper = array_pop($parents);
129-
if ($helper instanceof Nette\Caching\OutputHelper) {
130-
if (isset($args['dependencies'])) {
131-
$args += $args['dependencies']();
132-
}
133-
if (isset($args['expire'])) {
134-
$args['expiration'] = $args['expire']; // back compatibility
135-
}
136-
$helper->dependencies[Cache::TAGS] = $args['tags'] ?? null;
137-
$helper->dependencies[Cache::EXPIRATION] = $args['expiration'] ?? '+ 7 days';
138-
$helper->end();
129+
if (!$helper instanceof Nette\Caching\OutputHelper) {
130+
return;
131+
}
132+
133+
if (isset($args['dependencies'])) {
134+
$args += $args['dependencies']();
135+
}
136+
if (isset($args['expire'])) {
137+
$args['expiration'] = $args['expire']; // back compatibility
139138
}
139+
$helper->dependencies[Cache::TAGS] = $args['tags'] ?? null;
140+
$helper->dependencies[Cache::EXPIRATION] = $args['expiration'] ?? '+ 7 days';
141+
$helper->end();
140142
}
141143
}

src/Caching/Storages/FileStorage.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ public function lock(string $key): void
132132
@mkdir($dir); // @ - directory may already exist
133133
}
134134
$handle = fopen($cacheFile, 'c+b');
135-
if ($handle) {
136-
$this->locks[$key] = $handle;
137-
flock($handle, LOCK_EX);
135+
if (!$handle) {
136+
return;
138137
}
138+
139+
$this->locks[$key] = $handle;
140+
flock($handle, LOCK_EX);
139141
}
140142

141143

@@ -269,12 +271,14 @@ public function clean(array $conditions): void
269271
} elseif ($namespaces) {
270272
foreach ($namespaces as $namespace) {
271273
$dir = $this->dir . '/_' . urlencode($namespace);
272-
if (is_dir($dir)) {
273-
foreach (Nette\Utils\Finder::findFiles('_*')->in($dir) as $entry) {
274-
$this->delete((string) $entry);
275-
}
276-
@rmdir($dir); // may already contain new files
274+
if (!is_dir($dir)) {
275+
continue;
276+
}
277+
278+
foreach (Nette\Utils\Finder::findFiles('_*')->in($dir) as $entry) {
279+
$this->delete((string) $entry);
277280
}
281+
@rmdir($dir); // may already contain new files
278282
}
279283
}
280284

@@ -358,12 +362,14 @@ private static function delete(string $file, $handle = null): void
358362
if (!$handle) {
359363
$handle = @fopen($file, 'r+'); // @ - file may not exist
360364
}
361-
if ($handle) {
362-
flock($handle, LOCK_EX);
363-
ftruncate($handle, 0);
364-
flock($handle, LOCK_UN);
365-
fclose($handle);
366-
@unlink($file); // @ - file may not already exist
365+
if (!$handle) {
366+
return;
367367
}
368+
369+
flock($handle, LOCK_EX);
370+
ftruncate($handle, 0);
371+
flock($handle, LOCK_UN);
372+
fclose($handle);
373+
@unlink($file); // @ - file may not already exist
368374
}
369375
}

src/Caching/Storages/SQLiteStorage.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ public function read(string $key)
5656
{
5757
$stmt = $this->pdo->prepare('SELECT data, slide FROM cache WHERE key=? AND (expire IS NULL OR expire >= ?)');
5858
$stmt->execute([$key, time()]);
59-
if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
60-
if ($row['slide'] !== null) {
61-
$this->pdo->prepare('UPDATE cache SET expire = ? + slide WHERE key=?')->execute([time(), $key]);
62-
}
63-
return unserialize($row['data']);
59+
if (!$row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
60+
return;
61+
}
62+
63+
if ($row['slide'] !== null) {
64+
$this->pdo->prepare('UPDATE cache SET expire = ? + slide WHERE key=?')->execute([time(), $key]);
6465
}
66+
return unserialize($row['data']);
6567
}
6668

6769

0 commit comments

Comments
 (0)