Skip to content

Conversation

@WyriHaximus
Copy link
Member

Implements a TTL as RFCd in #11.

Implements / Closes #11

@WyriHaximus WyriHaximus added this to the v0.5.0 milestone Mar 30, 2018
$this->data[$key] = [
'value' => $value,
'expires' => $expires,
];
Copy link
Member

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?

Copy link
Member Author

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 👍


if (isset($this->expires[$key]) && $this->expires[$key] < time()) {
unset($this->data[$key], $this->expires[$key]);
return Promise\resolve();
Copy link
Member

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.

*
* @param string $key
* @param mixed $value
* @param int|null $ttl
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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 🤷‍♂️

Copy link
Member Author

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)

reset($this->data);
unset($this->data[key($this->data)]);
$key = key($this->data);
unset($this->data[$key], $this->expires[$key]);
Copy link
Member

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?).

Copy link
Member Author

@WyriHaximus WyriHaximus Apr 6, 2018

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 SplPriorityQueue optimization in a follow up PR with benchmarks to see how big the difference is for different cache sizes. I'll try out both to see which turns out to be the simplest to implement and read.

Copy link
Member Author

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 👍

@WyriHaximus
Copy link
Member Author

@clue updated to both your suggestions.

@WyriHaximus WyriHaximus changed the title [WIP] TTL TTL Apr 13, 2018
@WyriHaximus WyriHaximus requested a review from jsor April 23, 2018 14:45
@clue clue closed this in #29 Jun 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants