January 30th, 2013, 05:24 PM
-
How to improve a simple caching mechanism in Python?
Hi all, just registered so I could ask this question.
Right now I have this code that prevents a class from updating more than once every three minutes:
Code:
now = datetime.now()
delta = now - myClass.last_updated_date
seconds = delta.seconds
if seconds > 180
update(myClass)
else
retrieveFromCache(myClass)
Now what I'd like to do is allow myClass to update twice per 3 minutes, instead of just once.
I was thinking of creating a list to store the last two times myClass was updated, and comparing against those in the if statement, but I fear my code will get convoluted and harder to read if I go that route.
Does anybody have a recommendation of a better way to do this?
January 31st, 2013, 04:31 AM
-
Originally Posted by zmc123
Hi all, just registered so I could ask this question.
Right now I have this code that prevents a class from updating more than once every three minutes:
Code:
now = datetime.now()
delta = now - myClass.last_updated_date
seconds = delta.seconds
if seconds > 180
update(myClass)
else
retrieveFromCache(myClass)
Now what I'd like to do is allow myClass to update twice per 3 minutes, instead of just once.
I was thinking of creating a list to store the last two times myClass was updated, and comparing against those in the if statement, but I fear my code will get convoluted and harder to read if I go that route.
Does anybody have a recommendation of a better way to do this?
I may have misunderstood the question but it seems to me that it is updating every three minutes according to the conditional seconds > 180. So to get it update twice every three minutes, i.e twice as fast as before, divide 180 by 2 and you get 90. So seconds > 90 should update twice every three minutes or once every 1.5 minutes. right?