4 ms·
I have extensively used https://pypi.org/project/diskcache/ https://pypi.org/project/diskcache/. Is there a reason you decided to make an in house solution?
by rmholt 3y ago
I have extensively used https://pypi.org/project/diskcache/ https://pypi.org/project/diskcache/. Is there a reason you decided to make an in house solution?
- quickslowdown 3y agoI found DiskCache sometime last year, it's amazing. Very simple to set up and works great as a cache for so many different things.
- dotancohen 3y agoWhat are you using it for? A disk based cache seems almost contradictory for my use cases, I would love to hear yours. Anything that I would store on disk, even as a cache, I can generally put in SQLite.
- akx 3y agoYou'll be happy to know that Diskcache is backed by SQLite (and/or spill files for large enough (size configurable) blobs).
- rmholt 3y agoIt pretty much does that, if I was to be a little reductive, DiskCache is just a wrapper around sqlite and pickle
- p10_user 3y agoThe degree of reduction is nice considering the countless times in the past where I wrote my own file cache logic using if/else statements, temporary files, pickle, and bespoke sqlite databases. Why give myself the headache of maintaining so much extra code when someone already wrote it.
- quickslowdown 3y agoI write some toy Python scripts/apps for a couple APIs (the Pokeapi, Spacetraders, etc). I use the HTTPX library as a request client, and wasn't aware of the Hishel library for caching requests. I used DiskCache to cache responses for ~15 minutes so I wasn't sending live requests every time I tested the app. I'm not building anything "cloud scale," most things I run are off my local machine. Having a convenient, fast local cache that's simple to use (DiskCache) has so many uses, it's hard to think of them all! I might use a cache with no expiration to store some configs, or to store a serialized object for later retrieval. I might use it as an in-memory object cache while the program loads, so I don't have to spin up a Redis server.
- varispeed 3y agoHow it fares with several millions of cached objects?
- BiteCode_dev 3y agoIt has sqlite performances, which is the fastest you can get with Pareto effort.
- Twirrim 3y agoBeing sqlite backed, it's really fast and threadsafe, the cache is shared safely between all threads or processes. It's a very mature library, too, nice and polished, I've never once experienced a bug with it.
- williamzeng0 3y agoThread safety is a big issue with ours, we'll run into issues when two different processes attempt to write to the same location, or we'll get a bad read. This is a better solution for large scale workloads. Ours is more meant for single-process scripts like an LLM workflow.
- anentropic 3y agoI was thinking of this as soon as I read the OP mature and works well
- CGamesPlay 3y agoI was curious to see an alternative to this, but how is this an alternative? You're saying I can implement my own caching of function calls that invalidates when the arguments or source code change...? These feel like entirely separate layers. Did I miss where diskcache does this stuff?
- nicwolff 3y agohttps://grantjenks.com/docs/diskcache/api.html#diskcache.Cache.memoize https://grantjenks.com/docs/diskcache/api.html#diskcache.Cac... Decorator to wrap callable with memoizing function using cache. Repeated calls with the same arguments will lookup result in cache and avoid function evaluation.
- williamzeng0 3y agoIt does feel like diskcache is for performance gains in production rather than dev time gains. It seems like it would be bothersome to quickly invalidate the cache in case of a bad write.
- williamzeng0 3y agoDiskcache works well, we just wanted a dependency free version that we had more control over (easier cache key deletion). I think you'd have to write a custom hashing function for diskcache to use the function source code as a key. I'm also unsure if Diskcache supports ignoring certain fields in the function call.
- rmholt 3y agoGood questions! You made me check the docs because those seem like very legitimate issues. So firstly, DiskCache by default just checks the function name, not the source code, but you could hack it to include the source code. I personally usually just deleted the cache if I knew the function meaningfully changed. And it does support ignoring certain args yes.
- williamzeng0 3y agoThank you for checking! That works, we could just override the key logic to check the source code (I'm guessing it's easily accessible)