Commits

Laurent Bachelier committed d4abbd9

Updates cache classes from Symfony 1.4

Comments (0)

Files changed (8)

lib/backport/makethem.sh

 
 if [ $SRC = "svn" ]
 then
-  svn co "http://svn.symfony-project.com/branches/1.3/lib/cache" || exit 1
+  svn co "http://svn.symfony-project.com/branches/1.4/lib/cache" || exit 1
 else
   cp -a $SRC/cache ./ || exit 1
 fi

lib/backport/sfAPCCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfAPCCacheBackport.class.php 7605 2008-02-25 12:58:56Z fabien $
+ * @version    SVN: $Id: sfAPCCacheBackport.class.php 21990 2009-09-13 21:09:18Z FabianLange $
  */
 class sfAPCCacheBackport extends sfStringObjectCache
 {
   */
   public function get($key, $default = null)
   {
-    $value = apc_fetch($this->getOption('prefix').$key);
-
-    return false === $value ? $default : $value;
+    $value = $this->fetch($this->getOption('prefix').$key, $has);
+    return $has ? $value : $default;
   }
 
   /**
    */
   public function has($key)
   {
-    return !(false === apc_fetch($this->getOption('prefix').$key));
+    $this->fetch($this->getOption('prefix').$key, $has);
+    return $has;
   }
 
+  private function fetch($key, &$success)
+  {
+    $has = null;
+    $value = apc_fetch($key, $has);
+    // the second argument was added in APC 3.0.17. If it is still null we fall back to the value returned
+    if (null !== $has)
+    {
+      $success = $has;
+    }
+    else
+    {
+      $success = $value !== false;
+    }
+    return $value;
+  }
+  
+  
   /**
    * @see sfCacheBackport
    */

lib/backport/sfCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfCacheBackport.class.php 17865 2009-05-02 09:23:55Z FabianLange $
+ * @version    SVN: $Id: sfCacheBackport.class.php 28842 2010-03-29 08:18:46Z fabien $
  */
 abstract class sfCacheBackport
 {
    * @param string $key     The cache key
    * @param mixed  $default The default value is the key does not exist or not valid anymore
    *
-   * @return mixed The data of the cache
+   * @return string The data of the cache
    */
   abstract public function get($key, $default = null);
 
    * Saves some data in the cache.
    *
    * @param string $key      The cache key
-   * @param mixed  $data     The data to put in cache
+   * @param string $data     The data to put in cache
    * @param int    $lifetime The lifetime
    *
    * @return Boolean true if no problem
    */
   public function getLifetime($lifetime)
   {
-    return is_null($lifetime) ? $this->getOption('lifetime') : $lifetime;
+    return null === $lifetime ? $this->getOption('lifetime') : $lifetime;
   }
 
   /**

lib/backport/sfEAcceleratorCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfEAcceleratorCacheBackport.class.php 6365 2007-12-07 16:03:14Z fabien $
+ * @version    SVN: $Id: sfEAcceleratorCacheBackport.class.php 21908 2009-09-11 12:06:21Z fabien $
  */
 class sfEAcceleratorCacheBackport extends sfStringObjectCache
 {
   {
     $value = eaccelerator_get($this->getOption('prefix').$key);
 
-    return is_null($value) ? $default : $value;
+    return null === $value ? $default : $value;
   }
 
   /**
    */
   public function has($key)
   {
-    return !is_null(eaccelerator_get($this->getOption('prefix').$key));
+    return null !== eaccelerator_get($this->getOption('prefix'.$key));
   }
 
   /**

lib/backport/sfFileCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfFileCacheBackport.class.php 17865 2009-05-02 09:23:55Z FabianLange $
+ * @version    SVN: $Id: sfFileCacheBackport.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  */
 class sfFileCacheBackport extends sfStringObjectCache
 {

lib/backport/sfFunctionCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfFunctionCacheBackport.class.php 17865 2009-05-02 09:23:55Z FabianLange $
+ * @version    SVN: $Id: sfFunctionCacheBackport.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  */
 class sfFunctionCacheBackport
 {
   public function call($callable, $arguments = array())
   {
     // Generate a cache id
-    $key = md5(serialize($callable).serialize($arguments));
+    $key = $this->computeCacheKey($callable, $arguments);
 
     $serialized = $this->cache->get($key);
     if ($serialized !== null)
 
     return $data['result'];
   }
+
+  /**
+   * Returns the cache instance.
+   *
+   * @return sfCacheBackport The sfCache instance
+   */
+  public function getCache()
+  {
+    return $this->cache;
+  }
+
+  /**
+   * Computes the cache key for a given callable and the arguments.
+   *
+   * @param mixed $callable  A PHP callable
+   * @param array $arguments An array of arguments to pass to the callable
+   *
+   * @return string The associated cache key
+   */
+  public function computeCacheKey($callable, $arguments = array())
+  {
+    return md5(serialize($callable).serialize($arguments));
+  }
 }

lib/backport/sfMemcacheCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfMemcacheCacheBackport.class.php 17865 2009-05-02 09:23:55Z FabianLange $
+ * @version    SVN: $Id: sfMemcacheCacheBackport.class.php 24607 2009-11-30 21:39:08Z FabianLange $
  */
 class sfMemcacheCacheBackport extends sfStringObjectCache
 {
    */
   public function set($key, $data, $lifetime = null)
   {
-    $lifetime = is_null($lifetime) ? $this->getOption('lifetime') : $lifetime;
+    $lifetime = null === $lifetime ? $this->getOption('lifetime') : $lifetime;
 
     // save metadata
     $this->setMetadata($key, $lifetime);
       $this->setCacheInfo($key);
     }
 
-    return $this->memcache->set($this->getOption('prefix').$key, $data, false, $lifetime);
+    if (false !== $this->memcache->replace($this->getOption('prefix').$key, $data, false, time() + $lifetime))
+    {
+      return true;
+    }
+
+    return $this->memcache->set($this->getOption('prefix').$key, $data, false, time() + $lifetime);
   }
 
   /**
    */
   public function remove($key)
   {
+    // delete metadata
     $this->memcache->delete($this->getOption('prefix').'_metadata'.self::SEPARATOR.$key);
-
+    if ($this->getOption('storeCacheInfo', false))
+    {
+      $this->setCacheInfo($key, true);
+    }
     return $this->memcache->delete($this->getOption('prefix').$key);
   }
 
     }
 
     $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
-
     foreach ($this->getCacheInfo() as $key)
     {
       if (preg_match($regexp, $key))
       {
-        $this->memcache->delete($key);
+        $this->remove(substr($key, strlen($this->getOption('prefix'))));
       }
     }
   }
    * Updates the cache information for the given cache key.
    *
    * @param string $key The cache key
+   * @param boolean $delete Delete key or not
    */
-  protected function setCacheInfo($key)
+  protected function setCacheInfo($key, $delete = false)
   {
     $keys = $this->memcache->get($this->getOption('prefix').'_metadata');
     if (!is_array($keys))
     {
       $keys = array();
     }
-    $keys[] = $this->getOption('prefix').$key;
+
+    if ($delete)
+    {
+       if (($k = array_search($this->getOption('prefix').$key, $keys)) !== false)
+       {
+         unset($keys[$k]);
+       }
+    }
+    else
+    {
+      if (!in_array($this->getOption('prefix').$key, $keys))
+      {
+        $keys[] = $this->getOption('prefix').$key;
+      }
+    }
+
     $this->memcache->set($this->getOption('prefix').'_metadata', $keys, 0);
   }
 

lib/backport/sfSQLiteCacheBackport.class.php

  * @package    symfony
  * @supackage cacheBackport
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version    SVN: $Id: sfSQLiteCacheBackport.class.php 9084 2008-05-20 01:29:54Z Carl.Vondrick $
+ * @version    SVN: $Id: sfSQLiteCacheBackport.class.php 23501 2009-11-01 22:54:37Z FabianLange $
  */
 class sfSQLiteCacheBackport extends sfStringObjectCache
 {
    */
   public function initialize($options = array())
   {
-    if (!extension_loaded('sqlite'))
+    if (!extension_loaded('SQLite') && !extension_loaded('pdo_SQLite'))
     {
-      throw new sfConfigurationException('sfSQLiteCacheBackport class needs "sqlite" extension to be loaded.');
+      throw new sfConfigurationException('sfSQLiteCacheBackport class needs "sqlite" or "pdo_sqlite" extension to be loaded.');
     }
 
     parent::initialize($options);
   {
     $data = $this->dbh->singleQuery(sprintf("SELECT data FROM cache WHERE key = '%s' AND timeout > %d", sqlite_escape_string($key), time()));
 
-    return is_null($data) ? $default : $data;
+    return null === $data ? $default : $data;
   }
 
   /**
       }
     }
   }
-}
+}