-
-
Notifications
You must be signed in to change notification settings - Fork 18
TTL #28
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
TTL #28
Conversation
src/ArrayCache.php
Outdated
| $this->data[$key] = [ | ||
| 'value' => $value, | ||
| 'expires' => $expires, | ||
| ]; |
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.
Not a big fan of mixing these data structures. Probably makes more sense to store expiration in a separate array or possibly SplPriorityQueue?
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.
@clue good point, changed it 👍
src/ArrayCache.php
Outdated
|
|
||
| if (isset($this->expires[$key]) && $this->expires[$key] < time()) { | ||
| unset($this->data[$key], $this->expires[$key]); | ||
| return Promise\resolve(); |
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.
This should be updated to take advantage of #27? Also applies to documentation.
src/CacheInterface.php
Outdated
| * | ||
| * @param string $key | ||
| * @param mixed $value | ||
| * @param int|null $ttl |
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.
Should this use float instead of int seconds?
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.
If storing things in a cache for sub second times then yes, not sure how often that happens though
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.
Admittedly, my use cases would be fulfilled by having just second precision.
However, at the very least this PR would have to be updated to use microtime(true) instead of time() everywhere, so that storing an item at 09:49.999 with 2s TTL would expire at 09:51.999 instead of 09:51.000 already.
At this point I would argue we might as well accept a float TTL (also for consistency with react/event-loop). But given I don't really have a use case that requires this right now, I guess I'm okay with this either way 🤷♂️
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.
Ok changed it to float and microtime(true)
src/ArrayCache.php
Outdated
| reset($this->data); | ||
| unset($this->data[key($this->data)]); | ||
| $key = key($this->data); | ||
| unset($this->data[$key], $this->expires[$key]); |
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.
This will need to check if any items are expired first and then remove this one. Only if no expired items can be found, it should overwrite the oldest item (LRU from #26).
Right now, this may overwrite an item that an item that is not expired already.
For practical reasons it may overwrite any expired item. We can either loop over this array with O(n) or use a SplPriorityQueue with O(log n) which would make this faster for larger arrays (premature optimization?).
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.
Good point I'll fix that 👍 .
I'de vote for making it working now and do the I'll try out both to see which turns out to be the simplest to implement and read.SplPriorityQueue optimization in a follow up PR with benchmarks to see how big the difference is for different cache sizes.
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.
Took me a bit, but got it to work with SplPriorityQueue pushing soon 👍
|
@clue updated to both your suggestions. |
Implements a TTL as RFCd in #11.
Implements / Closes #11