-
-
Notifications
You must be signed in to change notification settings - Fork 45
added bulk read support #42
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
Conversation
5ee1b40
to
c25b0c1
Compare
* @param callable | ||
* @return array | ||
*/ | ||
public function bulkLoad(array $keys, $fallback = NULL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to have one fallback? Woulldn't make more sense to have fallback for each key? Or support both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key is passed as a first parameter to the fallback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but then you can't easily reuse callback for load and bulkLoad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So each time you call bulkLoad you will build an array of callbacks? that does not sound right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you want e.g. three values whose value are calculated in a different way? You would have to build this logic by if/switch branching inside the callback. That feels a bit ugly...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think bulk reuse for single operation is a good idea, for performance reasons. |
throw new Nette\InvalidArgumentException('Only scalar keys are allowed in a bulkLoad method.'); | ||
} | ||
} | ||
$keys = array_combine(array_map([$this, 'generateKey'], $keys), $keys); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaces whole line with
$storageKeys = array_map([$this, 'generateKey'], $keys);
Have you seen how doctrine/cache handles this? https://github.com/doctrine/cache/blob/master/lib/Doctrine/Common/Cache/CacheProvider.php |
* @param string key | ||
* @return array key => value pairs, missing items are omitted | ||
*/ | ||
function bulkRead(array $keys); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: consider returning [$value1, $value2]
instead of more complex [$key1 => $value1, $key2 => $value2]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storage would have to maintain the item order and check for missing values. This is easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but the implementation in Cache
would be simpler and faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but the same logic would have to be in the storage itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would depend on the storage. Now you assume that no storage can implement this efficiently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I do assume that. Or do you know any storage that doesn't need that logic?
The ideas is awesome, but the implementation needs a lot polishing. |
require __DIR__ . '/Cache.php'; | ||
|
||
//storage without bulk load support | ||
test(function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think dg uses test(function () { // storage without bulk load support
pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not matter :-)
btw, the performance impact of bulkRead reuse for a single read is about 10-20% |
} | ||
} | ||
$storageKeys = array_map([$this, 'generateKey'], $keys); | ||
$storage = $this->getStorage(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop this line, replace all usages of $storage
with $this->storage
updated - storages are optimized |
$keys = array_combine(array_map(function ($key) { | ||
return urlencode($this->prefix . $key); | ||
}, $keys), $keys); | ||
$metas = $this->memcached->getMulti(array_keys($keys)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace with
$prefixedKeys = array_map(function ($key) { return urlencode($this->prefix . $key); });
$keys = array_combine($prefixedKeys, $keys); // TODO: can we remove this?
$metas = $this->memcached->getMulti($prefixedKeys);
0aed862
to
a0c400e
Compare
54c618b
to
62a27d9
Compare
Uh oh!
There was an error while loading. Please reload this page.