title: Django, Borg pattern, API calls, caching results
body: <p>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.</p>
  
  <p>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.)</p>
  
  <p>After looking for singleton's in Python I discovered the borg pattern, which seems even cooler, so this is what i did:</p>
  
  <pre><code>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 &gt;= 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()
  </code></pre>
  
  <p>The idea is to use this in the following way:</p>
  
  <pre><code>store = PriceStore()
  url = store.prices['2.9900']['url']
  </code></pre>
  
  <p>And the store should initialize correctly and only fetch new price point info, if the existing info is older than one hour.</p>
  
  <p>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 <code>__shared_state</code> in django and expect it to still contain pricing info?</p>
  
  <p>Thanks!</p>