PHP code example of becklyn / cache

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

    

becklyn / cache example snippets


use Becklyn\Cache\Cache\SimpleCacheFactory;

class MyService
{
    private SimpleCacheItemInterface $cache;

    /**
     */
    public function __construct (SimpleCacheFactory $cacheFactory)
    {
        $this->cache = $cacheFactory->getItem(
            "my.cache.key",
            fn () => $this->loadItems()
        );
    }


    /**
     * Returns the cached or fresh items, depending on several conditions. 
     */
    public function getItems () : array
    {
        return $this->cache->get();
    }
    

    /**
     * Loads the items from the database
     */
    public function loadItems () : array
    {
        // ...
    }
}

$cacheFactory->getItem(
    "my.cache.key",
    fn () => $this->loadItems(),
    // eg. if your cache status depends on routing resources
    $this->router->getRouteCollection()->getResources()
 );