Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9e0eab

Browse files
[Cache] Clean RedisAdapter pipelining + FilesystemAdapter
1 parent b85ab60 commit d9e0eab

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function doFetch(array $ids)
5959
$now = time();
6060

6161
foreach ($ids as $id) {
62-
$file = $this->getFile($id);
62+
$file = $this->getFile($id, false);
6363
if (!$h = @fopen($file, 'rb')) {
6464
continue;
6565
}
@@ -89,7 +89,7 @@ protected function doFetch(array $ids)
8989
*/
9090
protected function doHave($id)
9191
{
92-
$file = $this->getFile($id);
92+
$file = $this->getFile($id, false);
9393

9494
return file_exists($file) && (@filemtime($file) > time() || $this->doFetch(array($id)));
9595
}
@@ -116,7 +116,7 @@ protected function doDelete(array $ids)
116116
$ok = true;
117117

118118
foreach ($ids as $id) {
119-
$file = $this->getFile($id);
119+
$file = $this->getFile($id, false);
120120
$ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
121121
}
122122

@@ -132,11 +132,8 @@ protected function doSave(array $values, $lifetime)
132132
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX;
133133

134134
foreach ($values as $id => $value) {
135-
$file = $this->getFile($id);
136-
$dir = dirname($file);
137-
if (!file_exists($dir)) {
138-
@mkdir($dir, 0777, true);
139-
}
135+
$file = $this->getFile($id, true);
136+
140137
$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
141138
if (false !== @file_put_contents($file, $value, LOCK_EX)) {
142139
@touch($file, $expiresAt);
@@ -148,10 +145,15 @@ protected function doSave(array $values, $lifetime)
148145
return $ok;
149146
}
150147

151-
private function getFile($id)
148+
private function getFile($id, $mkdir)
152149
{
153150
$hash = str_replace('/', '-', base64_encode(md5($id, true)));
151+
$dir = $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR;
152+
153+
if ($mkdir && !file_exists($dir)) {
154+
@mkdir($dir, 0777, true);
155+
}
154156

155-
return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.substr($hash, 2, -2);
157+
return $dir.substr($hash, 2, -2);
156158
}
157159
}

src/Symfony/Component/Cache/Adapter/RedisAdapter.php

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,53 +229,70 @@ protected function doSave(array $values, $lifetime)
229229
$failed[] = $id;
230230
}
231231
}
232-
233232
if (!$serialized) {
234233
return $failed;
235234
}
236-
if ($lifetime > 0) {
237-
if ($this->redis instanceof \RedisArray) {
238-
$redis = array();
239-
foreach ($serialized as $id => $value) {
240-
if (!isset($redis[$h = $this->redis->_target($id)])) {
241-
$redis[$h] = $this->redis->_instance($h);
242-
$redis[$h]->multi(\Redis::PIPELINE);
243-
}
244-
$redis[$h]->setEx($id, $lifetime, $value);
245-
}
246-
foreach ($redis as $h) {
247-
if (!$h->exec()) {
248-
$failed = false;
249-
}
250-
}
251-
} else {
252-
$this->pipeline(function ($pipe) use ($serialized, $lifetime) {
253-
foreach ($serialized as $id => $value) {
254-
$pipe->setEx($id, $lifetime, $value);
255-
}
256-
});
257-
}
258-
} elseif (!$this->redis->mSet($serialized)) {
259-
return false;
235+
if (0 >= $lifetime) {
236+
$this->redis->mSet($serialized);
237+
238+
return $failed;
260239
}
261240

241+
$this->pipeline(function ($pipe) use (&$serialized, $lifetime) {
242+
foreach ($serialized as $id => $value) {
243+
$pipe('setEx', $id, array($lifetime, $value));
244+
}
245+
});
246+
262247
return $failed;
263248
}
264249

250+
private function execute($command, $id, array $args, $redis = null)
251+
{
252+
array_unshift($args, $id);
253+
call_user_func_array(array($redis ?: $this->redis, $command), $args);
254+
}
255+
265256
private function pipeline(\Closure $callback)
266257
{
267-
if ($this->redis instanceof \Predis\Client) {
268-
return $this->redis->pipeline($callback);
269-
}
270-
$pipe = $this->redis instanceof \Redis && $this->redis->multi(\Redis::PIPELINE);
258+
$e = null;
259+
$redis = $this->redis;
260+
271261
try {
272-
$e = null;
273-
$callback($this->redis);
262+
if ($redis instanceof \Predis\Client) {
263+
$redis->pipeline(function ($pipe) use ($callback) {
264+
$this->redis = $pipe;
265+
$callback(array($this, 'execute'));
266+
});
267+
} elseif ($redis instanceof \RedisArray) {
268+
$connections = array();
269+
$callback(function ($command, $id, $args) use (&$connections) {
270+
if (!isset($connections[$h = $this->redis->_target($id)])) {
271+
$connections[$h] = $this->redis->_instance($h);
272+
$connections[$h]->multi(\Redis::PIPELINE);
273+
}
274+
$this->execute($command, $id, $args, $connections[$h]);
275+
});
276+
foreach ($connections as $c) {
277+
$c->exec();
278+
}
279+
} else {
280+
$pipe = $redis->multi(\Redis::PIPELINE);
281+
try {
282+
$callback(array($this, 'execute'));
283+
} catch (\Exception $e) {
284+
}
285+
if ($pipe) {
286+
$redis->exec();
287+
}
288+
if (null !== $e) {
289+
throw $e;
290+
}
291+
}
274292
} catch (\Exception $e) {
275293
}
276-
if ($pipe) {
277-
$this->redis->exec();
278-
}
294+
295+
$this->redis = $redis;
279296
if (null !== $e) {
280297
throw $e;
281298
}

0 commit comments

Comments
 (0)