8000 [Cache] Clean RedisAdapter pipelining + FilesystemAdapter by nicolas-grekas · Pull Request #18714 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Clean RedisAdapter pipelining + FilesystemAdapter #18714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000 Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,8 @@ protected function doSave(array $values, $lifetime)
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX;

foreach ($values as $id => $value) {
$file = $this->getFile($id);
$dir = dirname($file);
if (!file_exists($dir)) {
@mkdir($dir, 0777, true);
}
$file = $this->getFile($id, true);

$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
if (false !== @file_put_contents($file, $value, LOCK_EX)) {
@touch($file, $expiresAt);
Expand All @@ -148,10 +145,15 @@ protected function doSave(array $values, $lifetime)
return $ok;
}

private function getFile($id)
private function getFile($id, $mkdir = false)
{
$hash = str_replace('/', '-', base64_encode(md5($id, true)));
$dir = $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR;

if ($mkdir && !file_exists($dir)) {
@mkdir($dir, 0777, true);
}

return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.substr($hash, 2, -2);
return $dir.substr($hash, 2, -2);
}
}
85 changes: 48 additions & 37 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
10000
Original file line number Diff line number Diff line change
Expand Up @@ -233,51 +233,62 @@ protected function doSave(array $values, $lifetime)
if (!$serialized) {
return $failed;
}
if ($lifetime > 0) {
if ($this->redis instanceof \RedisArray) {
$redis = array();
foreach ($serialized as $id => $value) {
if (!isset($redis[$h = $this->redis->_target($id)])) {
$redis[$h] = $this->redis->_instance($h);
$redis[$h]->multi(\Redis::PIPELINE);
}
$redis[$h]->setEx($id, $lifetime, $value);
}
foreach ($redis as $h) {
if (!$h->exec()) {
$failed = false;
}
}
} else {
$this->pipeline(function ($pipe) use ($serialized, $lifetime) {
foreach ($serialized as $id => $value) {
$pipe->setEx($id, $lifetime, $value);
}
});
}
} elseif (!$this->redis->mSet($serialized)) {
return false;

if (0 >= $lifetime) {
$this->redis->mSet($serialized);

return $failed;
Copy link
Member
@stof stof May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not return an array rather than false in some cases, and does not thech the return value from mSet. Is it expected ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep: mset can't fail (connection errors trigger exceptions)

}

$this->pipeline(function ($pipe) use (&$serialized, $lifetime) {
foreach ($serialized as $id => $value) {
$pipe('setEx', $id, array($lifetime, $value));
}
});

return $failed;
}

private function execute($command, $id, array $args, $redis = null)
{
array_unshift($args, $id);
call_user_func_array(array($redis ?: $this->redis, $command), $args);
}

private function pipeline(\Closure $callback)
{
if ($this->redis instanceof \Predis\Client) {
return $this->redis->pipeline($callback);
}
$pipe = $this->redis instanceof \Redis && $this->redis->multi(\Redis::PIPELINE);
$redis = $this->redis;

try {
$e = null;
$callback($this->redis);
} catch (\Exception $e) {
}
if ($pipe) {
$this->redis->exec();
}
if (null !== $e) {
throw $e;
if ($redis instanceof \Predis\Client) {
$redis->pipeline(function ($pipe) use ($callback) {
$this->redis = $pipe;
$callback(array($this, 'execute'));
});
} elseif ($redis instanceof \RedisArray) {
$connections = array();
$callback(function ($command, $id, $args) use (&$connections) {
if (!isset($connections[$h = $this->redis->_target($id)])) {
$connections[$h] = $this->redis->_instance($h);
$connections[$h]->multi(\Redis::PIPELINE);
}
$this->execute($command, $id, $args, $connections[$h]);
});
foreach ($connections as $c) {
$c->exec();
}
} else {
$pipe = $redis->multi(\Redis::PIPELINE);
try {
$callback(array($this, 'execute'));
} finally {
if ($pipe) {
$redis->exec();
}
}
}
} finally {
$this->redis = $redis;
}
}
}
0