Skip to content
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

Why remove_expired is not a public function? #18

Open
vi opened this issue Jan 28, 2020 · 5 comments
Open

Why remove_expired is not a public function? #18

vi opened this issue Jan 28, 2020 · 5 comments

Comments

@vi
Copy link

vi commented Jan 28, 2020

let _ = the_cache.iter() is obvious workaround, but less readable and maintainable.

Removing expired elements can be significant if their destructors do something, so that function may be called periodically on timer.

@stusmall
Copy link
Owner

I guess this question is why would you want to clean it out?

@vi
Copy link
Author

vi commented Jan 28, 2020

To make expired entries actually expire and free up resources (e.g. file descriptors).

If you just don't touch TtlCache, I expect it to still hold even long-expired entries, which may in turn refer to more things.

@stusmall
Copy link
Owner

I've resisted adding one because if a program relies on cleaning up resources by manually calling remove_expire() they either have too long cache times or intialized the cache with too high of a capacity. Manually going in and calling remove_expired() will help delay the potential resource exhaustion issue but doesn't remove it.

V's that impl Drop make a better case for prematurely purging the cache though. Mind if I think about this one a bit more?

@vi
Copy link
Author

vi commented Jan 30, 2020

either have too long cache times or intialized the cache with too high of a capacity

Or sometimes idles and not uses the cache for prolonged periods of time.

If cached entries are heavy (refers to a lof of memory, contains sockets, etc.), it can be problematic even with low times and low capacity.

stusmall added a commit that referenced this issue Feb 6, 2020
@alopatindev
Copy link

alopatindev commented Feb 20, 2020

It'd be essential to make both remove_expired and len public.

In my case I need to measure current number of unexpired items; every new added item has the same TTL, so it's okay to call remove_expired before each insert (although making remove_expired removing no more than a constant number of items at a time might be even a better idea).

Can we make len public as well? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants