PHP code example of kuria / cache

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

    

kuria / cache example snippets



   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\Filesystem\FilesystemDriver


   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\Filesystem\Entry\File\PhpFileFormat;
   use Kuria\Cache\Driver\Filesystem\FilesystemDriver


   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\Apcu\ApcuDriver


   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\Memcached\MemcachedDriver


   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\Redis\RedisDriver


   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\Memory\MemoryDriver


   

   use Kuria\Cache\Cache;
   use Kuria\Cache\Driver\BlackHole\BlackHoleDriver


   

   $cache->setPrefix('prefix_')


   

   $fooCache = $cache->getNamespace('foo.')


   

   if ($cache->has('key')) {
       echo 'Entry exist';
   } else {
       echo 'Entry does not exist';
   }

.. WARNING::

   Beware of a possible race-condition between calls to ``has()`` and ``get()``.

   If possible, only call ``get()`` and check for a ``NULL`` result or use its
   ``$exists`` argument.


``get()`` - read a single entry
-------------------------------

The ``get()`` method returns the stored value or ``NULL`` if the entry does not exist.

.. code:: php

   

   $value = $cache->get('key')


   

   $value = $cache->get('key', $exists)


   

   $values = $cache->getMultiple(['foo', 'bar', 'baz'])


   

   $values = $cache->getMultiple(['foo', 'bar', 'baz'], $failedKeys)


   

   if ($cache->isFilterable()) {
       // list all keys
       foreach ($cache->listKeys() as $key) {
           echo "{$key}\n";
       }

       // list keys beginning with foo_
       foreach ($cache->listKeys('foo_') as $key) {
           echo "{$key}\n";
       }
   }


``getIterator()`` - list keys and values in the cache
-----------------------------------------------------

The ``getIterator()`` method will return an iterator for all keys and values in the
cache. This is a part of the ``IteratorAggregate`` interface.

If the driver doesn't support this operation, an ``UnsupportedOperationException``
exception will be thrown. You can check support using the ``isFilterable()`` method.

Listing all keys and values:

.. code:: php

   

   foreach ($cache as $key => $value) {
       echo $key, ': ';
       var_dump($value);
   }

Listing keys and values matching a prefix:

.. code:: php

   

   foreach ($cache->getIterator('foo_') as $key => $value) {
       echo $key, ': ';
       var_dump($value);
   }


``add()`` / ``set()`` - create a new entry
------------------------------------------

The ``add()`` and ``set()`` methods both create an entry in the cache.

The ``set()`` method will overwrite an existing entry, but ``add()`` will not.

See `Allowed value types`_.

.. code:: php

   

   $cache->add('foo', 'foo-value')


   

   $cache->set('foo', 'foo-value', 60)


   

   $cache->addMultiple(['foo' => 'foo-value', 'bar' => 'bar-value'])


   

   $cache->addMultiple(['foo' => 'foo-value', 'bar' => 'bar-value'], 60)


   

   $value = $cache->cached('key', 60, function () {
       // some expensive operation
       $result = 123


   

   if ($cache->delete('key')) {
       echo 'Entry deleted';
   }


``deleteMultiple()`` - delete multiple entries
----------------------------------------------

The ``deleteMultiple()`` method deletes multiple entries from the cache.

.. code:: php

   

   if ($cache->deleteMultiple(['foo', 'bar', 'baz'])) {
       echo 'All entries deleted';
   } else {
       echo 'One or more entries could not be deleted';
   }


``filter()`` - delete entries using a prefix
--------------------------------------------

The ``filter()`` method deletes all entries that match the given prefix.

If the driver doesn't support this operation, an ``UnsupportedOperationException``
exception will be thrown. You can check support using the ``isFilterable()`` method.

.. code:: php

   

   if ($cache->isFilterable()) {
       $cache->filter('foo_');
   }


``clear()`` - delete all entries
--------------------------------

The ``clear()`` method deletes all entries.

If a cache prefix is set and the cache is filterable, only entries matching
that prefix will be cleared.

.. code:: php

   

   $cache->clear()


   

   if ($cache->supportsCleanup()) {
       $cache->cleanup();
   }


Allowed value types
===================

All types except for the resource type can be stored in the cache.

Most drivers use standard `object serialization <http://php.net/manual/en/language.oop5.serialization.php>`_.


Cache events
============

``CacheEvents::HIT``
--------------------

Emitted when an entry has been read.

The listener is passed the key and value.

.. code:: php

   

   use Kuria\Cache\CacheEvents


   

   use Kuria\Cache\CacheEvents


   

   use Kuria\Cache\CacheEvents


   

   use Kuria\Cache\CacheEvents


   

   use Kuria\Cache\Psr\CacheItemPool


   

   use Kuria\Cache\Psr\SimpleCache