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

LRU cache is actually FIFO #71

Open
2e0byo opened this issue Aug 16, 2022 · 2 comments
Open

LRU cache is actually FIFO #71

2e0byo opened this issue Aug 16, 2022 · 2 comments

Comments

@2e0byo
Copy link
Collaborator

2e0byo commented Aug 16, 2022

Disclaimer: I may be being stupid here.

Currently LruCache is just a FIFO. When the cache overflows it pops the item added least recently. An LRU cache should (right?) take access into account.

I can add a PR to make it a true LruCache, or to rename it to FifoCache, or I could just need a gentle explainer about how caches work if I'm being daft.

Expected behaviour:

from mopidy_tidal import LruCache

def test_lru():
    lru_cache = LruCache(max_size=8, persist=True, directory="cache")
    lru_cache |= {f"tidal:uri:{val}": val for val in range(8)}
    lru_cache["tidal:uri:0"] # 0th entry becomes most recently accessed
    lru_cache["tidal:uri:8"] = 8
    assert lru_cache == {f"tidal:uri:{val}": val for val in (0, *range(2, 9))}
@blacklight
Copy link
Collaborator

It's still an LRU cache technically, it's just that its definition of "used" means "inserted", not "read" - the order of insertion is basically guaranteed by extending OrderedDict.

I think it makes sense to make it a "true" LRU cache - meaning that it should move an item to the top also when it's read.

@2e0byo
Copy link
Collaborator Author

2e0byo commented Sep 2, 2022

Great---I'll have a think about the implementation and suggest a PR. With a bit of thought it should be possible to abstract the interface and have the track cache inherit as well, so that'll solve another worry.

(Isn't that just a FIFO? Insertion only happens if the item isn't in the cache anyhow, so effectively an item is in the cache until it's the oldest, at which point it's evicted. Or more generally, FIFO is a special case of LRU where insertion = updated and re-insertion is forbidden. Anyhow it's moot.)

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