title: Google App Engine Datastore query returning stale data
body: <p>I have a project based on GAE using the <code>db</code> <code>Datastore</code>. In my project I run a job every 15 minutes, and update the data in <code>Datastore</code>. Then I have an endpoint that queries the <code>Datastore</code> and displays the result. But, the result that I get is stale and not the one that was updated. It seems that GAE doesn't hit the <code>Datastore</code> and returns the data from the cache, but I am not sure. Here is my code:</p>
  
  <pre><code>class MainHandler(webapp2.RequestHandler):
  
      def get(self):
          query = Contests.all()
          contests_data = query.fetch(1) # fetch the data from datastore
          self.response.write(contests_data[0].data)
  
  class DataBaseHandler(webapp2.RequestHandler):
  
      # the job that runs every 15 minutes
      def get(self):
          contests_data = get_all_contests() # get the new data
          query = Contests.all()
          contests = query.fetch(1)
          contests[1].data = contests_data # update the data
          db.put(contests[0])
          self.response.write(json.dumps({"message":"updated"}))
  </code></pre>
  
  <p>Here is my model:</p>
  
  <pre><code>class Contests(db.Model):
      """Models Contests"""
      data = db.TextProperty(default="{}")
      created = db.DateTimeProperty(auto_now_add=True)
      modified = db.DateTimeProperty(auto_now=True)
  </code></pre>
  
  <p>I tried clearing the cache using <code>memcache.flush_all()</code> but even that doesn't solve the problem. Why doesn't GAE return the updated data? And how do I solve the problem?</p>
  
  <p><strong>EDIT</strong>: I changed the query to ancestor query but the data is still stale(and it is days old). </p>
  
  <p><code>main.py</code></p>
  
  <pre><code>class MainHandler(webapp2.RequestHandler):
  
          def get(self):
              contest_list = ContestList.get_or_insert('contest_list', name='ContestList')
              query = Contests.all()
              query.ancestor(contest_list) 
              contests = query.fetch(1)
              self.response.write(contests[0].data)
  
      class DataBaseHandler(webapp2.RequestHandler):
  
          def get(self):
              contests_data = get_all_contests()
              contest_list = ContestList.get_or_insert('contest_list', name='ContestList')
              query = Contests.all()
              query.ancestor(contest_list)
              contests = query.fetch(1)
              contests[0].data = contests_data
              contests[0].put()
              self.response.write(json.dumps({"message":"updated"}))
  </code></pre>
  
  <p><code>models.py</code>:</p>
  
  <pre><code>class ContestList(db.Model):
      name = db.StringProperty()
  
  class Contests(db.Model):
      """Models Contests"""
      data = db.TextProperty(default="{}")
      created = db.DateTimeProperty(auto_now_add=True)
      modified = db.DateTimeProperty(auto_now=True)
  </code></pre>
  
  <p><strong>I even tried getting data by using <code>key</code></strong>:</p>
  
  <pre><code>class MainHandler(webapp2.RequestHandler):
  
      def get(self):
          q = ndb.Key('Contests', 'contest_data').get()
          if q:
              self.response.write(q.data)
          else:
              self.response.write("error")
  
  class DataBaseHandler(webapp2.RequestHandler):
  
      def get(self):
          Contests(key=ndb.Key('Contests', "contest_data"), data=get_all_contests()).put()
          self.response.write(json.dumps({"message":"updated"})) 
  </code></pre>
  
  <p>What doesn't GAE hit the datastore? What am I missing here?</p>