Esempio n. 1
0
 function set($key, $value, $expire = CACHE_PERMANENT, $headers = NULL)
 {
     // Create new cache object.
     $cache = new stdClass();
     $cache->cid = $key;
     $cache->created = time();
     $cache->expire = $expire;
     $cache->headers = $headers;
     $cache->data = $value;
     if ($expire == CACHE_TEMPORARY || $expire == CACHE_PERMANENT) {
         $set_expire = 0;
     } else {
         $set_expire = $expire;
     }
     if (!empty($key)) {
         if ($this->settings['shared']) {
             if ($this->lock()) {
                 // Get lookup table to be able to keep track of bins
                 $lookup = $this->memcache->get($this->lookup);
                 // If the lookup table is empty, initialize table
                 if (!is_array($lookup)) {
                     $lookup = array();
                 }
                 // Set key to 1 so we can keep track of the bin
                 $lookup[$this->key($key)] = $expire;
                 // Attempt to store full key and value
                 if (!$this->memcache->set($this->key($key), $cache, $this->settings['compress'], $set_expire)) {
                     unset($lookup[$this->key($key)]);
                     $return = FALSE;
                 } else {
                     // Update static cache
                     parent::set($this->key($key), $cache);
                     $return = TRUE;
                 }
                 // Resave the lookup table (even on failure)
                 $this->memcache->set($this->lookup, $lookup, FALSE, 0);
                 // Remove lock.
                 $this->unlock();
             }
         } else {
             // Update memcache
             return $this->memcache->set($this->key($key), $cache, $this->settings['compress'], $set_expire);
         }
     }
 }
Esempio n. 2
0
 function set($key, $value, $expire = CACHE_PERMANENT, $headers = NULL)
 {
     // Create new cache object.
     $cache = new stdClass();
     $cache->cid = $key;
     $cache->created = time();
     $cache->headers = $headers;
     $cache->expire = $expire;
     if (!is_string($value)) {
         $cache->serialized = TRUE;
         $cache->data = serialize($value);
     } else {
         $cache->serialized = FALSE;
         $cache->data = $value;
     }
     db_query("UPDATE {" . $this->name . "} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $cache->data, $cache->created, $cache->expire, $cache->headers, $cache->serialized, $key);
     if (!db_affected_rows()) {
         @db_query("INSERT INTO {" . $this->name . "} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $key, $cache->data, $cache->created, $cache->expire, $cache->headers, $cache->serialized);
     }
     parent::set($key, $cache);
 }
Esempio n. 3
0
 /**
  * set()
  *   Add item into cache.
  *
  * @param string $key
  *   The key to set.
  * @param string $value
  *   The data to store in the cache.
  * @param string $expire
  *   The time to expire in seconds.
  * @param string $headers
  *   The page headers.
  * @return bool
  *   Returns TRUE on success or FALSE on failure
  */
 function set($key, $value, $expire = CACHE_PERMANENT, $headers = NULL)
 {
     // Create new cache object.
     $cache = new stdClass();
     $cache->cid = $key;
     $cache->created = time();
     $cache->expire = $expire;
     $cache->headers = $headers;
     $cache->data = $value;
     if ($expire != CACHE_PERMANENT && $expire != CACHE_TEMPORARY) {
         // Convert Drupal $expire, which is a timestamp, to a TTL
         $ttl = $expire - time();
     } else {
         $ttl = 0;
     }
     $return = FALSE;
     if (!empty($key) && $this->lock()) {
         // Get lookup table to be able to keep track of bins
         $lookup = xcache_get($this->lookup);
         // If the lookup table is empty, initialize table
         if (!is_array($lookup)) {
             $lookup = array();
         }
         $lookup[$this->key($key)] = $expire;
         // Attempt to store full key and value
         if (!xcache_set($this->key($key), serialize($cache), $ttl)) {
             unset($lookup[$this->key($key)]);
             $return = FALSE;
         } else {
             // Update static cache
             parent::set($this->key($key), $cache);
             $return = TRUE;
         }
         // Resave the lookup table (even on failure)
         xcache_set($this->lookup, $lookup);
         // Remove lock.
         $this->unlock();
     }
     return $return;
 }