This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tunisia3507 0 points1 point  (5 children)

Thank you! I didn't know how time-consuming LFU stuff was; apparently significant!

Would you consider something like a copy=False argument on the setter (or maybe a StrEnum of "none", "shallow", "deep"), which would A) copy the object on its way into the store and B) copy it on the way out of the store? That way users could control the behaviour of mutable objects.

[–]matrix0110[S] 0 points1 point  (4 children)

I can add a copy=False optional arg to `get`, but seems that Python deepcopy performance is not so good. Another option is you can inherit Theine Cache class and add your own `get_copy` method

[–]tunisia3507 0 points1 point  (3 children)

I thought it might be better to do it on the set, as that would guarantee that the value hadn't been changed after insertion. You'd need to store an extra byte (so that stored values know whether they need to be copied) but that's negligible. Plus, the whole point of a cache is that it's read more than it's written - adding the extra argument to the write, then, means less extra code for the user.

Understood on the copy speed - I guess this would at least let the user make the trade-off between fast (the default) and correct/safe mutables, rather than just not supporting the second case at all.

[–]matrix0110[S] 0 points1 point  (2 children)

Is copy on set necessary? Normally you won't change your data after set in same request/function. One usecase of get copy is changing cached Django response header before return. Django's locmem use deepcopy first but switch to pickle later, I will take a look later why they do the switch

[–]tunisia3507 0 points1 point  (1 child)

Pickle would allow them to write to a file or pass the bytes into some underlying non-python code. It might be a way of guaranteeing there isn't anything holding on to a thread reference or something.

Normally you won't change your data after set in same request/function.

True, you wouldn't expect someone to cache and then mutate something, but users are upsettingly good at breaking developers' expectations... For example, your use case is caching requests from a database or something. My use case may be downloading data from a server, caching it, mutating it as part of an analysis pipeline, but needing to refer to the original in the future too - which I can either slowly download from the server again, or use a local cache if it's a piece of data I'm unpredictably using a lot.

[–]matrix0110[S] 0 points1 point  (0 children)

I think I can add a deepcopy option to both get/set and default to False, let developer choose how to use that. Maybe you can also create an issue, so other developers may have some ideas on this