PHP code example of oralunal / taggable-memcached

1. Go to this page and download the library: Download oralunal/taggable-memcached library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oralunal / taggable-memcached example snippets


use Oralunal\TaggableMemcached\Cache;

$memcached = new Cache::getInstance(server:'localhost', port:11211, prefix:'taggable_');

// Best Practise
$key = 'some_key';
try{
    if(!is_null($value = $memcached->get($cache_key))){
        // Do something with $value
    } else {
        // Generate the value and save it
        $value = 'some value';
        $memcached->set($cache_key, $value, 60);
    }
} catch(\Oralunal\TaggableMemcached\Exceptions\GetException $e){    
    // Memcached failed to get the value
    // Log the error and define value here
    $value = 'some value'; // Don't save it to memcached, maybe there is a problem with memcached server.
} catch(\Oralunal\TaggableMemcached\Exceptions\SetException $e){
    // Memcached failed to set the value
    // Log the error
    // We don't need to define value here because we did it before saving it to the memcached server.
}

// Delete a value
$memcached->delete($key);

// Flush all values
$memcached->flush();

// Set a value with tags
$tags = ['tag1', 'tag2'];
$memcached->withTags($tags)->set($key, $value));

$tag = 'tag3';
$memcached->withTags($tag)->set($key, $value);

// Delete values by tag
$memcached->deleteByTag($tag);