PHP code example of ropi / in-memory-cache

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

    

ropi / in-memory-cache example snippets



$cache = new \Ropi\InMemoryCache\FifoInMemoryCache(defaultTtl: null, maxSize: 3);

$cache->set('a', 10);
$cache->set('b', 20);
$cache->set('c', 30);

var_dump($cache->get('b')); // Prints 20 
var_dump($cache->count()); // Prints 3

$cache->set('d', 40);

var_dump($cache->count()); // Still prints 3, because cache size is limited to 3 and thus first cache entry was deleted
var_dump($cache->get('a')); // Prints NULL, because 'a' was the first cache entry
var_dump($cache->get('d')); // Prints 40


$cache = new \Ropi\InMemoryCache\FixedSizedInMemoryCache(defaultTtl: null, maxSize: 3);

$cache->set('a', 10);
$cache->set('b', 20);
$cache->set('c', 30);

var_dump($cache->get('b')); // Prints 20 
var_dump($cache->count()); // Prints 3

$cache->set('d', 40); // Throws OverflowException, because cache size is limited to 3


$cache = new \Ropi\InMemoryCache\InMemoryCache(defaultTtl: 2);

$cache->set('a', 10);

var_dump($cache->get('a')); // Prints 10 

sleep(3);

var_dump($cache->get('a')); // Prints NULL, because TTL expired