PHP code example of xinningsu / laravel-easy-cache

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

    

xinningsu / laravel-easy-cache example snippets


class News
{
    use \Sulao\EasyCache\EasyCache;

    public function getTopNews($limit = 5)
    {
        $news = [
            ['id' => 1, 'title' => 'news 1'],
            ['id' => 2, 'title' => 'news 2'],
            ['id' => 3, 'title' => 'news 3'],
            ['id' => 4, 'title' => 'news 4'],
            ['id' => 5, 'title' => 'news 5'],
        ];

        return array_slice($news, 0, $limit);
    }
}


$news = new News();

// without caching
$topNews = $news->getTopNews(2);

// cache it with default configuration, ttl: 3600,
// key: serialize class name, method name and parameters as cache key,
// store: laravel default cache store
// see Global Configuration below to custom default configuration.
$topNews = $news->cache()->getTopNews(2);

// or specify the ttl
$topNews = $news->cache(300)->getTopNews(2);

// specify the ttl and cache key,
// please notice that the cache key has to be specified if there is
// a closure in parameters, because closure can not be serialized.
$topNews = $news->cache(300, 'cache-key')->getTopNews(2);

// specify ttl, cache key and store,
// store is the store defined in laravel config/cache.php
$topNews = $news->cache(300, 'cache-key', 'array')->getTopNews(2);

return [
    // If the ttl parameter is not specified when calling cache method,
    // then use this one, default value is 3600.
    'ttl' => 3600,
    
    // Value can be the store defined in config/cache.php of laravel project,
    // such as memcached, redis ... If null, using laravel default cache store.
    'store' => null,

    // This prefix will be added to the front of each cache key, so it can
    // easily refresh the whole cache by changing this. default value is null.
    'prefix' => null,

    // If this specified, the caches in the page can be refreshed via query string,
    // see Refresh Page Cache below.
    'refresh_key' => null,
];