PHP code example of webiny / cache

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

    

webiny / cache example snippets


    \Webiny\Components\ClassLoader::getInstance()->registerMap(['Jamm\Memory' => 'path to memory lib']);

    // APC
    $cache = \Webiny\Component\Cache\Cache::APC('cache-id');

    // Couchbase
    $cache = \Webiny\Component\Cache\Cache::Couchbase('CacheService', 'username', 'password', 'bucket', '127.0.0.1:8091');

    // Memcache
    $cache = \Webiny\Component\Cache\Cache::Memcache('CacheService', 'localhost', 11211);

    // Redis
    $cache = \Webiny\Component\Cache\Cache::Redis('CacheService', 'localhost', 6379);

    // write to cache
    $cache->save('myKey', 'some value', 600, ['tag1', 'tag2']);

    // read from cache
    $cache->read('myKey');

    // delete from cache
    $cache->delete('myKey');

    // delete by tag
    $cache->deleteByTag(['tag1']);

    class MyClass
    {
        use \Webiny\Component\Cache\CacheTrait;

        public function myMethod(){
            $this->cache('Frontend')->read('cache_key');
        }
    }

    class CustomCacheDriver implements \Webiny\Component\Cache\Bridge\CacheInterface
    {
        // implement the interface methods

        // static method that will return the CacheDriver
        function getDriver($cacheId, $param1, $param2, array $options){
            $driver = new static($cacheId, $param1, $param2);

            return \Webiny\Component\Cache\CacheDriver($driver, $options);
        }
    }