PHP code example of molajo / cache

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

    

molajo / cache example snippets



    // 1. Instantiate a Cache Handler.
    $options = array();
    $options['cache_folder']  = SITE_BASE_PATH . '/' . $application->get('system_cache_folder', 'cache');
    $options['cache_time']    = $application->get('system_cache_time', 900);
    $options['cache_handler'] = $application->get('cache_handler', 'File');

    use Molajo\Cache\Adapter\File as FileCache;
    $adapter_handler = new FileCache($options);

    // 2. Instantiate the Adapter, injecting it with the Handler.
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);

    // 3. Set cache.
    $adapter->set('key value', 'cache this value for seconds =>', 86400);

    // 4. Get Cache.
    $cacheItem = $adapter->get('this is the key for a cache item');

    if ($cacheItem->isHit() === false) {
        // deal with no cache
    } else {
        echo $cacheItem->value; // Use the Cached Value
    }

    // 5. Remove cache.
    $adapter->remove('key value');

    // 6. Clear cache.
    $adapter->clear();


    try {
        $cacheItem = $adapter->get($key);

    } catch (Exception $e) {
        // deal with the exception
    }

    if ($cacheItem->isHit() === true) {
        $cached_value = $cacheItem->getValue();
    } else {
        // cache is not available - do what you have to do.
    }

    try {
        $adapter->set($key, $value, $ttl);

    } catch (Exception $e) {
        // deal with the exception
    }


    try {
        $adapter->remove($key);

    } catch (Exception $e) {
        // deal with the exception
    }


    try {
        $adapter->clear();

    } catch (Exception $e) {
        // deal with the exception
    }


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Apc;
    $adapter_handler = new Apc($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the Database Handler
    $options['database_connection'] = $connection;
    $options['database_table']      = 'xyz_cache_table';
    $options['database_quote']      = "'";
    $options['database_namequote']  = '`';

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Database;
    $adapter_handler = new Database($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Dummy as DummyCache;
    $adapter_handler = new DummyCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the File Handler
    $options['cache_handler']       = '/Absolute/Path/To/Cache/Folder';

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\File as FileCache;
    $adapter_handler = new FileCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Specific to the Memcached Handler
    $options['memcached_pool']         = $connection;
    $options['memcached_compression']  = 'xyz_cache_table';
    $options['memcached_servers']      = "'";

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Memcached
    $adapter_handler = new Memcached($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Memory
    $adapter_handler = new Memory($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Redis
    $adapter_handler = new Redis($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\Wincache
    $adapter_handler = new Wincache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    $options = array();

    // Standard Cache Options
    $options['cache_service']       = 1;
    $options['cache_time']          = 86400;

    // Instantiate Cache Handler
    use Molajo\Cache\Adapter\XCache
    $adapter_handler = new XCache($options);

    // Instantiate Cache Adapter, injecting the Handler
    use Molajo\Cache\Adapter;
    $adapter = new Driver($adapter_handler);


    curl -s https://getcomposer.org/installer | php

{
    "  "Molajo/Cache": "1.*"
    }
}

    php composer.phar install