Skip to content

Add support for something like an event handler, which can be used to refresh expiring entry? #381

@tatsuya6502

Description

@tatsuya6502

#372 (comment)

I ended up implementing my own cache type that stores Arcs and only evicts from the cache if the ttl is passed AND if the strong count is 1, in which case it can safely unwrap the Arc.

I am glad you found a solution. Perhaps, I should have shown you the sample code on this comment (#344). It uses dashmap::DashMap to store the keys and Arcs, and use moka::sync::Cache to control the TTL of the entries in the DashMap. It does not store the Arcs on moka cache, so it does not have the strong count problem.

The sample code does not exactly match your use case, because moka cache cannot delay the eviction until the strong count becomes 1. You will have to reinsert the evicted entry like this one (#298), so it is not an elegant solution.

After #298 was filed, I was thinking to add a support for an event handler closure, which will be called when a cached entry is about to expire by the TTL (but is not yet evicted). The handler can then check the strong count of the Arc to decide whether it lets the entry to be evicted or extends its expiry.

CC: @unikzforce @rtdavis22

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions