piskvorky / sqlitedict

Persistent dict, backed by sqlite3 and pickle, multithread-safe.
Apache License 2.0
1.17k stars 131 forks source link

Cache operation #126

Closed ed2050 closed 3 years ago

ed2050 commented 3 years ago

From the description I expected sqlitedict to operate a memory cache. However it does not: every access to the dict hits the database. One would expect data would be cached in sqlitedict and only hit the db when needed - at least as a user parameter, if not the default behavior.

E.g. a simple len () request should cache the number of items and return it, rather than a linear scan with SELECT COUNT (*) every time. Either because only one process accesses the db, so it always has the latest info in memory. Or because short-term changes are unimportant and recent-ish data is good enough (think of a daily temperature graph with temp readings every minute, where the result barely changes if the last few readings aren't available ). Data can be synced with the db periodically according to user preference.

Does anyone know how to achieve this? Map a simple in-memory dict to sqlite, backed by syncs periodically or as-needed (on write)? Doesn't seem possible with this project. If not, any similar projects that fit the bill? Don't need or want something heavyweight like SQLAlchemy or SQLObject. Thanks.

piskvorky commented 3 years ago

You are correct – sqlitedict is not a cache. It's a persistent dict backed by SQLite.

I think you could add your own "caching" layer of application logic on top of sqlitedict, with periodic syncing, or any other functionality you like. But sqlitedict itself is not the place for it.

ed2050 commented 3 years ago

I think you could add your own "caching" layer of application logic on top of sqlitedict, with periodic syncing, or any other functionality you like.

I could also program a Motorola 68000 hooked up to drum memory with nothing but a piece of wool and an iron needle. The point isn't what I can do. The point is what's the best approach.

Happy holidays to all the kind, helpful people out there... wherever they are.