Exemplo n.º 1
0
 /**
  * Read an object from memcached if it exists, 
  * otherwise load the object from the database and then cache it
  * @param mixed $pkValue Primary key value
  * @param integer|boolean $cache Number of seconds to cache the object for
  *   0 = cache never expires, FALSE = do not cache
  * @return boolean
  */
 public function read($pkValue = FALSE, $cache = FALSE)
 {
     // If we're not caching or there is no memcached object just call parent
     if ($cache === FALSE || !$this->memcached instanceof \Memcached) {
         return parent::read($pkValue);
     }
     // If there is a key value passed set it
     if ($pkValue !== FALSE) {
         $this->iset(static::$pk, $pkValue);
     }
     // Load memcached object attributres
     $obj = $this->memcached->get($this->getMemcachedID());
     // If the object is not cached, load and cache it
     if ($obj === FALSE) {
         // Read object
         if (parent::read($pkValue) === FALSE) {
             return FALSE;
         }
         // Cache object attributes
         $this->memcached->set($this->getMemcachedID(), $this->getCacheAttributes(), $cache);
     } else {
         $this->setCacheAttributes($obj);
     }
     // Return
     return TRUE;
 }