PHP code example of aloframework / cache

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

    

aloframework / cache example snippets




    use AloFramework\Cache\Clients\RedisClient;

    $redis = new RedisClient();
    $redis->connect('localhost');

    $redis->setKey('foo', 'bar', 300); // expire the key in 5 minutes
    $redis->setKey('foo', 'bar', new DateTime('2015-01-01 05:05:05')); //Expire the key on 2015-01-01 05:05:05
    $redis->setKey('foo', 'bar'); //Use default expiration time
    $redis['foo'] = 'bar'; //Use default expiration time

    //Echo the keys
    echo $redis->getKey('foo');
    echo $redis['foo'];

    //Echo all the keys
    print_r($redis->getAll());

    //Loop through all the keys
    foreach ($redis as $k => $v) {
        echo 'Key: ' . $k . ', Value: ' . $v . '<br/>';
    }

    //Count the number of items in the database
    echo count($redis);

    //Or do anything you would with the standard Redis class - RedisClient extends it.



    use AloFramework\Cache\Clients\RedisClient;
    use AloFramework\Cache\CacheItem;

    //Save an item
    $server = new RedisClient();
    $server->connect();

    $cacheItem           = new CacheItem('key', 'value', $server);
    $cacheItem->lifetime = 600;
    $cacheItem->saveToServer();

    // Get or load an item
    $cacheItem = new CacheItem('key');
    $cacheItem->getFromServer($server);

    echo $cacheItem->value;