Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from gdsmith/mget-values
Browse files Browse the repository at this point in the history
Use mget to reduce trips to redis
  • Loading branch information
marlonbaeten authored Jan 10, 2022
2 parents f604258 + 15a26d2 commit f97fc95
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/Storage/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,33 @@ protected function getPrefix()
public function getValues($key)
{
$items = [];
foreach ($this->redis->sMembers($this->getSetName($key)) as $entry) {
// get the value
$value = $this->redis->get($entry);
$value = $value === false ? null : floatval($value);

// get all the members for the key set
$members = $this->redis->sMembers($this->getSetName($key));

// return early if we have no members
if ( ! count($members)) return $items;

$labelMembers = [];
// get all the label versions of the key
foreach ($members as $entry) {
// get the labels
list(, , $key, $extra) = explode('|', $entry, 4);
$labelMembers[] = "{$this->prefix}|" . StorageAdapterInterface::LABEL_PREFIX . "|{$key}|{$extra}";
}

// get all labels with mget to reduce trips to redis
$labelValues = array_combine($members, $this->redis->mget($labelMembers));

// get all values with mget to reduce trips to redis
foreach (array_combine($members, $this->redis->mget($members)) as $entry => $value) {
// cast value to float unless null or false
if ($value !== null) {
$value = $value === false ? null : floatval($value);
}

// get the labels
list(,,$key,$extra) = explode('|', $entry, 4);
$labelValuesKey = "{$this->prefix}|".StorageAdapterInterface::LABEL_PREFIX."|{$key}|{$extra}";
$labelData = $this->redis->get($labelValuesKey);
$labelData = isset($labelValues[$entry]) ? $labelValues[$entry] : null;

// check label data
if (is_string($labelData) && strlen($labelData) > 0) {
Expand Down

0 comments on commit f97fc95

Please sign in to comment.