PHP code example of philasearch / cache

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

    

philasearch / cache example snippets




use Philasearch\Cache\Cache;
use Philasearch\Cache\CacheProviders;

// a redis cache at localhost and database of 0
$cache = new Cache ();

// a more specified cache
$cache = new Cache( CacheProviders::REDIS, 'tcp://127.0.0.1:6379?database=0');



use Philasearch\Cache\Cache;
use Philasearch\Cache\CacheProviders;

$cache = new Cache();

// a basic cached object
$object = $cache->createObject('cache_key'); 

// a cached object that expires in 10 seconds
$object = $cache->createObject('cache_key', 10);

// a cached object with a value foo that equals bar.
$object = $cache->createObject('cache_key', 0, ['foo' => 'bar']);

// filling the object with an array of variables
$object->fill(['foo' => 'bar']);

// setting a variable directly
$object->set('foo', 'bar');

// getting a variable
$foo = $object->get('foo');



use Philasearch\Cache\Cache;
use Philasearch\Cache\CacheProviders;

$cache = new Cache();

// a basic tree
$tree = $cache->createTree('cached_key');

// a tree that expires after 10 seconds
$tree = $cache->createTree('cached_key', 10);

// a root node with the id of 1 ('the id can also be a string')
$root = $tree->makeRootNode(1);

// a root node with the id of 1 and a value of foo that equals bar
$root = $tree->makeRootNode(1, ['foo' => 'bar']);

// set the value foo to bar
$root->set('foo', 'bar');

// get the value stored under bar
$root->get('foo');

// add a new child
$node = $root->addChild(2);

// add a new child with the value foo equaling bar
$node = $root->addChild(2, ['foo' => 'bar']);

// saving the tree
$tree->save();

// turns the tree to an array starting from root
$tree->getArray();

// turns the tree to an array starting at the child with the id of 2
$tree->getArray(2);