PHP code example of pluggit / cache

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

    

pluggit / cache example snippets


/** @var \Cmp\Cache\Cache $cache */
$cache = (new CacheBuilder)
  ->withLogging($psrLogger)
  ->withoutExceptions()
  ->withArrayCache()
  ->withRedisCacheFromParams($host, $port, $dbNumber)
  ->build();

// Set an item
$cache->set('foo', 'bar');

// Demand an item, throws an exception if not present
$bar = $cache->demand('foo');

// Get an item, if not present in any cache it will return the given default
$default = $cache->get('not found', 'default');

// Delete an item
$cache->delete('foo');

// Empty the cache
$cache->flush();

//Create a tagged cache
$taggedCache = $cache->tag('myTag');

//Set a tagged item
$taggedCache->set('key','value');

//Remove all tagged items
$taggedCache->flush();

// Store one photo in the cache
$cache->set('photo.1', $photoOne);

// Store a set of photos and tag them
$cache->tag('photos')->set('photo.1', $taggedPhotoOne);
$cache->tag('photos')->set('photo.2', $taggedPhotoTwo);

// The photos are into different buckets
$cache->has('photo.1');                // true 
$cache->has('photo.2')                 // false
$cache->tag('photos')->has('photo.1'); // true
$cache->tag('photos')->has('photo.2')  // true

// You can flush all items with the same tag at once
$cache->tag('photos')->flush();
$cache->has('photo.1');                // true 
$cache->tag('photos')->has('photo.1'); // false

$pimple->register(new CacheServiceProvider());

/** @var LoggerCache $cache */
$cache = $pimple['cache'];

/** @var ArrayCache $cache */
$arrayCache = $pimple['cache']->getDecoratedCache();

// Redis from parameters
$pimple->register(new PimpleCacheProvider(), ['cache.backends' => [
    ['backend' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'db' => 1, 'timeout' => 2.5]
]]);

// ArrayCache + Redis from existing connection
$pimple->register(new PimpleCacheProvider(), ['cache.backends' => [
    ['backend' => 'array']
    ['backend' => 'redis', 'connection' => $redisConnection,]
]]);

// ArrayCache + Redis from a service registered in the container
$pimple->register(new PimpleCacheProvider(), ['cache.backends' => [
    ['backend' => 'array']
    ['backend' => 'redis', 'connection' => 'redis.connection']
]]);

// Chain caches with logging and muted exceptions
$pimple->register(new PimpleCacheProvider(), [
    'cache.backends'   => [
        ['backend' => 'array'],
        ['backend' => 'custom_cache_service_key'],
    ],
    'cache.exceptions' => false,
    'cache.logging'    => ['logger' => $psrLogger, 'log_level' => PsrLevel::CRITICAL],
]);