vemcogroup/model-cache

Static cache instead of re-hitting cache server

1.0.1 2020-12-01 10:34 UTC

This package is auto-updated.

Last update: 2021-09-29 02:16:52 UTC


README

Latest Version on Packagist Total Downloads

Description

This package allows to have static local cache on your models.

It can both be used with a cache server or without.

Installation

You can install the package via composer:

composer require vemcogroup/model-cache

Usage

It's easy to use the trait in your own class just Use the trait as below

use Vemcogroup\ModelCache\Traits\HaslocalCache;

class DataRepository 
{
    use HasLocalCache;
    
    public function getData($userId)
    {
        $localCacheKey = 'getData.'.$userId;
        
        /*
         * If localCache exists with key return cache
         */
        if ($cache = self::getLocalCache($localCacheKey)) {
           return $cache;
        }
         
         // Run DB query or Cache code
         $result = [1, 2, 3, 4, 5, 6];
         
         /*
          * Remember to save your DB query or Cache result
          */
         self::setLocalCache($localCacheKey, $result);
         
         return $result;
    }
}

If you need to clear the Cache you can use the clear method

self::clearLocalCache();