PHP code example of eftec / cacheone

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

    

eftec / cacheone example snippets


use eftec\CacheOne;
redis","127.0.0.1","",6379);

$cacheValue=$cache->get('','countries'); // read the cache (if any) otherwise false
if($cacheValue===false) {
    echo "generating a new list of countries..<br>";
    $countries=['USA','Canada','Japan','Chile'];
    $cache->set('','countries',$countries,500); // store into the cache for 500 seconds.
} else {
    echo "read from cache<br>";
    $countries=$cacheValue;
}
var_dump($countries);

use eftec\CacheOne;
redis","127.0.0.1","",6379);

use eftec\CacheOne;
apcu");

use eftec\PdoOne;
use eftec\CacheOne;
oot','abc.123','travisdb',false,null,1,'KVTABLE');
$pdo->logLevel=3; // optional, if you want to debug the errors. 
$pdo->open();
// $pdo->createTableKV();  // you should create the key-value table if it doesn't exist.
$cache=new CacheOne("pdoone"); // the instance $pdo is injected automatically into CacheOne.

$cache=new CacheOne(
  "pdoone",
  ['mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLA']
  ); 

use eftec\CacheOne;
memcache","127.0.0.1"); // minimum configuration
$cache=new CacheOne("memcache","127.0.0.1",11211,'schema'); // complete configuration

use eftec\CacheOne;
documentone",__DIR__."/base","schema"); // folder /base/schema must exists

use eftec\CacheOne;
auto");

$cache->set("group","key1","hello world",500);
$cache->set("group","key2","hola mundo",500);

$result=$cache->get("group","key1");
$result=$cache->get("","key2");
$result=$cache->get("","key2","not found"); // if not key2 (groupless) then it returns not found 

$result=$cache->setDefaultTTL(50); // it sets the default time to live. "documentone" one uses it.
$result=$cache->getDefaultTTL();   // it gets the time to live

// cart could be [1,2,3]
$cache->push('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [1,2,3,4]
$cache->push('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5]
$cache->push('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]

// cart could be [1,2,3]
$cache->unshift('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [4,1,2,3]
$cache->unshift('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5]
$cache->unshift('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]

// cart could be [1,2,3,4];
$element=$this->pop('','cart'); // now cart is [1,2,3] and $element is 4

// cart could be [1,2,3,4];
$element=$this->shift('','cart'); // now cart is [2,3,4] and $element is 1

$cache->invalidate("group","key1"); // invalidate a key inside a group
$cache->invalidate("","key1"); // invalidate a key without a group.

$cache->invalidateGroup("group"); // invalidate all keys inside group
$cache->invalidateGroup(["group1","group2"]); // invalidate all key inside group1 and group2

$cache->invalidateAll(); 

$cache->setSerializer('php'); // set the serializer to php (default value). It is fastest but it uses more space.
$cache->setSerializer('json-array'); // set the serializer to json-array. It uses fewer space than PHP however it is a bit slower.
$cache->setSerializer('json-object'); // set the serializer to json-object.
$cache->setSerializer('none'); // set the serializer to none (the value must be serialized)
 

$type=$cache->getSerializer(); // get php,json-array,json-object or none

$cache->select(1);
$cache->select('table'); // PdoOne

 // eftec/CliOne(1.28) PHP configuration file (date gen: 2023-04-08 10:19). DO NOT EDIT THIS FILE 
/**
 * It is a configuration file
 */
$cacheOneConfig=[
    'type' => 'redis',
    'cacheserver' => '127.0.0.1',
    'schema' => '',
    'port' => '6379',
    'user' => NULL,
    'password' => NULL,
];

$cache=CacheOne::factory($cacheOneConfig);