title: Yii an Memcache
body: <p>We are using Yii framework and Memcached for caching.</p>
  
  <p>We have the following problem:</p>
  
  <ol>
  <li>We make a db query q</li>
  <li>if q is already cached we get the data from memcache</li>
  <li>Otherwise yii queries Mysql</li>
  <li>set the cache value</li>
  <li>we get back the results from memcache</li>
  </ol>
  
  <p>If we request again the same query q before the memcache key is set ( before step 4 )
  then because memcache key is not set we again query the db.</p>
  
  <p>We would like to change this behavior to something like:</p>
  
  <ol>
  <li>We make a db query q</li>
  <li>if key for q exists in memcache and value is not null return value</li>
  <li>Otherwise if key is set and value is null set memcache key => null</li>
  <li>set the cache value</li>
  <li>we get back the results from memcache</li>
  </ol>
  
  <p>Here is the pseudocode of what happens and what we want:</p>
  
  <pre><code>get(q):
      if q not in memcache:
          value = query_db(q)
          memcache[q] = value
          return memcache[q]
      else:
          return memcache[q]
  
  getNew(q):
      if q not in memcache:
          memcache[q] = null
          value = query_db(q)
          memcache[q] = value
          return memcache[q]
      elif q in memcache and memcache[q] != null:
          return memcache[q]
      else:
          while True:
              if memcached[q] != null:
                  return memcached[q]
              else:
                 sleep(3)
  </code></pre>
  
  <p>In other words, we would like to set the memcache key before having the results to null, and the other requests for the same query check if the value is null and wait until the value is not null (it means that the query is already processing).</p>
  
  <p>Do you know which part of the yii frameworkd should be modified?</p>