Hi everyone!
I'm using an API from a different site that returns a couple of 'pricepoint URL's that my users use to buy Virtual Goods.
I'm supposed to cache those results for at least an hour, since they don't change the price points on their system much. (And we want to save both our's and their bandwidth.)
After looking for singleton's in Python I discovered the borg pattern, which seems even cooler, so this is what i did:
def fetchPrices():
#uses urllib2.urlopen() to fetch prices
#parses results with ElementTree
return prices
class PriceStore():
__shared_state = {}
def update(self):
if self.lastUpdate is not None and (datetime.now() - self.lastUpdate).seconds >= 3600:
self.prices = fetchPrices()
self.lastUpdate = datetime.now()
elif self.lastUpdate is not None:
return
else:
self.lastUpdate = datetime.now() - timedelta(hours=1)
self.update()
def __init__(self):
self.__dict__ = self.__shared_state
self.lastUpdate = None
self.update()
The idea is to use this in the following way:
store = PriceStore()
url = store.prices['2.9900']['url']
And the store should initialize correctly and only fetch new price point info, if the existing info is older than one hour.
I seem to be hitting their API with every time that PriceStore is initialized, though. Can anyone spot my problem? Can I use a global variable like __shared_state
in django and expect it to still contain pricing info?
Thanks!