This might be more of a Python question than a Django one, but in
this ticket I'm hoping to make the DB cache a bit more performant by having it not cull stale entries
every time somebody adds, changes, or touches a cache key.
The idea is to use a setting or or class attribute so that the cache is instead culled every 50 times or 1000 times or whatever. Most of this is easy, but is there a good way to maintain a counter of how many times a key has been set?
The best I've come up with is just to do a mod of a random number, and let that be good enough. E.g.:
random_number = randint(0, CULL_EVERY_X)
if random_number == CULL_EVERY_X:
cull()
That's pretty sloppy, but it'd work (on average), and it'd handle things like counting in a multi-process set up.
The other approach, I suppose, would be to keep a counter in the DB and just increment it as needed, but it'd be nice to do something in memory even if it's a little less accurate, since this counter doesn't have to be exact.
Anybody have thoughts on this?
Thanks,
Mike