ElggMemcache::save PHP Method

save() public method

Saves a name and value to the cache
public save ( string $key, string $data, integer $ttl = null ) : boolean
$key string Name
$data string Value
$ttl integer TTL of cache item (seconds), 0 for no expiration, null for default.
return boolean
    public function save($key, $data, $ttl = null)
    {
        $key = $this->makeMemcacheKey($key);
        if ($ttl === null) {
            $ttl = $this->ttl;
        }
        $item = $this->stash_pool->getItem($key);
        $item->set($data);
        $item->expiresAfter($ttl);
        $result = $item->save();
        if ($result) {
            _elgg_services()->logger->info("MEMCACHE: SAVE SUCCESS {$key}");
        } else {
            _elgg_services()->logger->error("MEMCACHE: SAVE FAIL {$key}");
        }
        return $result;
    }

Usage Example

Example #1
0
/**
 * Gets the metastring identifier for a value.
 *
 * Elgg normalizes the names and values of annotations and metadata. This function
 * provides the identifier used as the index in the metastrings table. Plugin
 * developers should only use this if denormalizing names/values for performance
 * reasons (to avoid multiple joins on the metastrings table).
 *
 * @param string $string         The value
 * @param bool   $case_sensitive Should the retrieval be case sensitive?
 *                               If not, there may be more than one result
 *
 * @return int|array metastring id or array of ids
 * @since 1.9.0
 */
function elgg_get_metastring_id($string, $case_sensitive = true)
{
    global $CONFIG, $METASTRINGS_CACHE;
    // caching doesn't work for case insensitive requests
    if ($case_sensitive) {
        $result = array_search($string, $METASTRINGS_CACHE, true);
        if ($result !== false) {
            return $result;
        }
        // Experimental memcache
        $msfc = null;
        static $metastrings_memcache;
        if (!$metastrings_memcache && is_memcache_available()) {
            $metastrings_memcache = new ElggMemcache('metastrings_memcache');
        }
        if ($metastrings_memcache) {
            $msfc = $metastrings_memcache->load($string);
        }
        if ($msfc) {
            return $msfc;
        }
    }
    $escaped_string = sanitise_string($string);
    if ($case_sensitive) {
        $query = "SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE string = BINARY '{$escaped_string}' LIMIT 1";
    } else {
        $query = "SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE string = '{$escaped_string}'";
    }
    $id = false;
    $results = get_data($query);
    if (is_array($results)) {
        if (!$case_sensitive) {
            $ids = array();
            foreach ($results as $result) {
                $ids[] = $result->id;
            }
            // return immediately because we don't want to cache case insensitive results
            return $ids;
        } else {
            if (isset($results[0])) {
                $id = $results[0]->id;
            }
        }
    }
    if (!$id) {
        $id = _elgg_add_metastring($string);
    }
    $METASTRINGS_CACHE[$id] = $string;
    if ($metastrings_memcache) {
        $metastrings_memcache->save($string, $id);
    }
    return $id;
}
All Usage Examples Of ElggMemcache::save