python - Django, Borg pattern, API calls, caching results -
I am using an API from a different site which returns a 'pricepoint URL' which is used by my user virtual goods Use to buy.
I should cache those results for at least one hour, because they do not change the value points of their system very much (and we want to save both of them and their bandwidth.) < / P>
After searching singleton in Python, I searched the Borg pattern, which also looks cooler, so I did the same:
def fetchPrices (): # ElementsTree returns to get prices using urllib2.urlopen () Student # Perrs results Price PriceStore (): __shared_state = {} def update (self): if self.lastUpdate is no (Datetime.now () - self.lastUpdate) .seconds & gt; = 3600: self.prices = fetchPrices () self.lastUpdate = datetime.now () Alif self.lastUpdate is none: come back: self.lastUpdate = datetime now () - Timeliness (hours = 1) self.update () Def __init __ (self): self .__ dict__ = self. _ Share_state self Unknown person = no one himself. Update ()
The idea is to use it as follows:
store = PriceStore () url = store.prices ['2.9900'] ['url']
And the store should start properly and should only get new price point information, if the existing information is more than an hour old.
I think that the value of that value starts at all times, though. Can anyone see my problem? Can I use a global variable like in the __ share_state
in the digengo and hope that it can still keep pricing information?
Thank you!
I think the primer starts with them all the time, though I kill the API Does matter. Can someone reveal my problem?
Yes, it is easy to find:
def __init __ (self): self .__ dict__ = self .__ share_state self.lastUpdate = any self.lastUpdate = none
Completely Guarantees immediately after calling self.update () <<>> Anyone get
the value of self.lastUpdate
- you've compelled it so much! Delete that in self.lastUpdate = none
in __init __
and, for example, instead of
< Code> lastUpdate = none
class body level, eg after the __ share_state = {}
assignment and similar to that assignment Alignment with. He things you would like to work for.
Comments
Post a Comment