#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    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?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    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?

IMN logo majestic logo threadwatch logo seochat tools logo