Skip to content

Easy way to cache the result of service method as demand.

License

Notifications You must be signed in to change notification settings

xinningsu/laravel-easy-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Easy Cache

Easy way to cache the result of a service method on demand.

MIT licensed Build Status Coverage Status Scrutinizer Code Quality Code Intelligence Status Quality Gate Status Reliability Rating Security Rating Maintainability

Installation

Require this package with composer.

composer require xinningsu/laravel-easy-cache

[Optional] Copy config/easy-cache.php of this package to config/easy-cache.php under laravel project and custom it. see Global Configuration for more detail.

Usage

Use EasyCache trait in a service class

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);
    }
}

Now the result of the service method can be cached on demand.

$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);

Global Configuration

If you want custom global config, copy config/easy-cache.php of this package to config/easy-cache.php under your laravel project.

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,
];

Refresh Page Cache

Firstly have to defined refresh_key in config/easy-cache.php, such as

    'refresh_key' => 'clear_cache',

Then open the page and add the query string like this

http://localhots/?clear_cache=1

Will refresh all the caches of that page, notice that just the caches of the page, not all the caches in the store.

License

MIT

About

Easy way to cache the result of service method as demand.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages