vagento/cacheable

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:vagento-module

0.0.1 2021-12-11 16:34 UTC

This package is auto-updated.

Last update: 2024-11-12 04:12:13 UTC


README

Version License: MIT Twitter: WWoshid

This package adds cacheability to method calls. It requires the Laravel framework.

Installation

composer require vagento/cacheable

Usage

Trait: Cacheable - Method Cacheability

Add the \Vagento\Cacheable\Traits\Cacheable trait to any class that needs the results of a method call to be cached.

use Vagento\Cacheable\Traits\Cacheable;

class MyClass
{
    use Cacheable; // Add this trait
    
    public function myMethod()
    {
        // Big computational task
        return $bigData;
    }
}

Now you can call your method with the 'Cached' suffix like this:

$myClass = new MyClass();

$result = $myClass->myMethodCached();

Optional parameters can be passed before the method call (Can be chained):

$myClass = new MyClass();

// Optional: Cache usage, will toggle caching on or off
// Default: true
$myClass->setCacheUsage(false);

// Optional: Laravel cache tags @see https://laravel.com/docs/8.x/cache#cache-tags
// Default: null
$myClass->setCacheTags('my-method-tag');

// Optional: Time to live
// Default: null
$myClass->setTtl(60);
$myClass->setTtl(Carbon::now()->addMinutes(30)->toDateTime());

// Chaining
$result = $myClass->setCacheUsage(true)->setCacheTags(['method', 'something-else'])->setTtl(60)->myMethodCached();

For IDE support you can add the @method annotation to your class like this:

/**
 * @method array myMethodCached()
 */
class MyClass
{
    use Cacheable;

    public function myMethod(): array
    {
        // Big computational task
        return $bigData;
    }
}

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 Valentin Wotschel.
This project is MIT licensed.