10000 [Cache] Clean RedisAdapter pipelining + FilesystemAdapter · symfony/symfony@e68f2e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e68f2e4

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

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = false)
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: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -229,55 +229,65 @@ 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);
274-
} catch (\Exception $e) {
275-
}
276-
if ($pipe) {
277-
$this->redis->exec();
278-
}
279-
if (null !== $e) {
280-
throw $e;
262+
if ($redis instanceof \Predis\Client) {
263+
$redis->pipeline(function ($pipe) use ($callback) {
264+
$this->redis = $pipe;
265+
$callback(array($this, 'execute'));
266+
});
2 B41A 67+
} 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+
} finally {
284+
if ($pipe) {
285+
$redis->exec();
286+
}
287+
}
288+
}
289+
} finally {
290+
$this->redis = $redis;
281291
}
282292
}
283293
}

0 commit comments

Comments
 (0)
0