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

Mechanism for invalidating (parts of) the cache #5

Open
dimo414 opened this issue Nov 21, 2021 · 4 comments
Open

Mechanism for invalidating (parts of) the cache #5

dimo414 opened this issue Nov 21, 2021 · 4 comments

Comments

@dimo414
Copy link
Owner

dimo414 commented Nov 21, 2021

An easy option is to add an --invalidate flag that, rather than retrieving the command simply deletes the cache key associated with the given command, like so:

$ bkt -- date
Sun 21 Nov 2021 11:17:03 AM PST

$ bkt -- date
Sun 21 Nov 2021 11:17:03 AM PST

$ bkt --invalidate -- date

$ bkt -- date
Sun 21 Nov 2021 11:17:13 AM PST

But it might be preferable to support some sort of more powerful invalidation, such as the ability to invalidate all calls to a given command, e.g.:

$ bkt -- date +%T
11:18:19

$ bkt -- date +%T
11:18:19

$ bkt --invalidate=date

$ bkt -- date +%T
11:18:32

However bkt doesn't currently have any way to introspect the cache like this, short of a linear search. That might be fine, but otherwise either the key space would need to be redesigned or some secondary index would need to be maintained.

Also --scope can be used to invalidate the cache, as long as all callers participate in using the right scope.

@rrx
Copy link

rrx commented Nov 28, 2021

I like the idea of using the scope key for the invalidate. It's simple and predictable. I was also thinking of tagging a command run with multiple tags, that would provide some further granularity. Using --scope would be the same as tagging, just using a single tag.

--tag user=woof,group=cows
--invalidate user=woof
--invalidate group=cows

# if we have multiple tags, it's not clear if it's an AND/OR
--invalidate group=cows,user=woof
--invalidate group=sheep,user=woof

Perhaps --invalidate-or, --invalidate-and to be explicit, otherwise it's ambiguous. Maybe it can fail if --invalidate is specified with multiple tags with a message suggesting --invalidate-and/--invalidate-or. Just an idea.

I'm glad I found this project, I was planning to write pretty much exactly this, and in Rust.

@dimo414
Copy link
Owner Author

dimo414 commented Nov 29, 2021

Tags are an interesting idea; would you mind filing a separate FR with some more thoughts on that, e.g. how you might use them (beyond invalidation)?

Invalidation via the existing scoping mechanism is straightforward and easy, but it requires some form of collaboration amongst callers. For example if multiple callers are caching reads to a database and then one caller makes a cache-invalidating write they would need some way to notify the other callers to update their scope value. Obviously achievable (e.g. by storing the scope name in a file all callers share), but not straightforward.

Glad you like the project, it's something I've wanted to implement for a long time!

@dimo414
Copy link
Owner Author

dimo414 commented Mar 31, 2023

Related: the newly added --modtime flag provides another mechanism for invalidation.

@dimo414
Copy link
Owner Author

dimo414 commented May 5, 2023

Idea: reimplement scopes as a modtime file in the cache directory. Then scope-wide invalidation would simply involve touch-ing that file, avoiding the need for some sort of more bespoke cleanup mechanism. Obviously users can implement this themselves with --modtime, but it'd be nice to abstract it away from them via scopes.

This needs a little more thought and experimentation, but if someone's interested in playing around with it this would probably be a reasonable PR to pull together.

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

2 participants