PHP code example of sugiphp / cache

1. Go to this page and download the library: Download sugiphp/cache 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/ */

    

sugiphp / cache example snippets



use SugiPHP\Cache\Cache;
use SugiPHP\Cache\ApcStore;

$apcStore = new ApcStore();
$cache = new Cache($apcStore);

$cache->add("foo", "bar");


use SugiPHP\Cache\Cache;
use SugiPHP\Cache\FileStore;

$cacheDir = "/path/to/tmp";
$fileStore = new FileStore($cacheDir);
$cache = new Cache($fileStore);


use SugiPHP\Cache\Cache;
use SugiPHP\Cache\MemcachedStore;

// make a regular Memcached instance
$memcached = new Memcached();
// connect to a memcached server
$memcached->addServer("127.0.0.1", 11211);

// make a store
$mcStore = new MemcachedStore($memcached);
$cache = new Cache($mcStore);



use SugiPHP\Cache\Cache;
use SugiPHP\Cache\ArrayStore;

$cache = new Cache(new ArrayStore());



use SugiPHP\Cache\Cache;
use SugiPHP\Cache\NullStore;

$cache = new Cache(new NullStore());

$cache->set("foo", "bar");
$cache->get("foo"); // will return NULL


$cache->set("foo", "bar");    // store a value for a maximum allowed time
$cache->set("foo", "foobar"); // store a new value with the same key


$cache->add("key", "foo");    // this will store a value
$cache->add("key", "foobar"); // this will fail


$cache->set("key", "value", 60); // store a value for 60 seconds
$cache->get("key"); // this will return "value" if 60 seconds are not passed and NULL after that
$cache->get("baz"); // will return NULL


$cache->delete("key");


$cache->has("key"); // will return TRUE if the "key" was set and the expiration time was not passed, and FALSE otherwise


$cache->set("num", 1); // store a numeric value
$cache->inc("num"); // will return 2
$cache->inc("num", 10); // will return 12
$cache->dec("num"); // will return 11