Solar_Cache_Adapter::add()

public bool Solar_Cache_Adapter::add ( string $key , string $data , int $life = NULL )

Inserts cache entry data only if it does not already exist.

Parameters

  • (string) $key: The entry ID.

  • (string) $data: The data to store.

  • (int) $life: A custom lifespan, in seconds, for the entry; if null, uses the default lifespan for the adapter instance.

Returns

  • (bool) True on success, false on failure.

Description

Inserts cache entry data only if it does not already exist.

Useful for avoiding race conditions when one or more processes may be writing to the same entry.

This method will not update an existing entry; if the entry already exists, this method will not replace it.

For example, to add an array in the cache ...

<?php
// create a cache object
$cache = Solar::factory('Solar_Cache');

// create a unique ID
$id = 'my_array';

// set up some data to cache (this could be string output, or
// an object, or almost anything else)
$data = array('foo' => 'bar', 'baz' => 'dib', 'zim' => 'gir');

// store to the cache (fails if $id already exists)
$cache->add($id, $data);

// now fetch back the data for the $id entry
$result = $cache->fetch($id);


Local