You are here

public function MemcachedDriver::set in Memcache API and Integration 8.2

Adds an item into memcache.

Parameters

string $key: The string with which you will retrieve this item later.

mixed $value: The item to be stored.

int $exp: Parameter expire is expiration time in seconds. If it's 0, the item never expires (but memcached server doesn't guarantee this item to be stored all the time, it could be deleted from the cache to make place for other items).

bool $flag: If using the older memcache PECL extension as opposed to the newer memcached PECL extension, the MEMCACHE_COMPRESSED flag can be set to use zlib to store a compressed copy of the item. This flag option is completely ignored when using the newer memcached PECL extension.

Return value

bool Whether or not the add was successful.

Overrides DrupalMemcacheInterface::set

File

src/Driver/MemcachedDriver.php, line 13

Class

MemcachedDriver
Class MemcachedDriver.

Namespace

Drupal\memcache\Driver

Code

public function set($key, $value, $exp = 0, $flag = FALSE) {
  $collect_stats = $this
    ->statsInit();
  $full_key = $this
    ->key($key);
  $result = $this->memcache
    ->set($full_key, $value, $exp);

  // Something bad happened. Let's log the problem.
  if (!$result && $this->settings
    ->get('debug')) {
    $result_code = $this->memcache
      ->getResultCode();
    $result_message = $this->memcache
      ->getResultMessage();
    $this
      ->getLogger('memcache')
      ->error('MemcachedDriver::set() error key=@key error=[@error_code]@error_msg', [
      '@key' => $full_key,
      '@error_code' => $result_code,
      '@error_msg' => $result_message,
    ]);
  }
  if ($collect_stats) {
    $this
      ->statsWrite('set', 'cache', [
      $full_key => (int) $result,
    ]);
  }
  return $result;
}