承接 kolirt/laravel-cacheable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kolirt/laravel-cacheable

最新稳定版本:1.1.3

Composer 安装命令:

composer require kolirt/laravel-cacheable

包简介

Easily cache and control class methods without having to remember cache key names

README 文档

README

Easily cache and control class methods without having to remember cache key names

Structure

Buy Me A Coffee

Getting started

Easily cache and control class methods without having to remember cache key names

Requirements

  • PHP >= 8.1
  • Laravel >= 10

Installation

composer require kolirt/laravel-cacheable

Setup

Publish config file

php artisan cacheable:install

By default, Laravel has a problem with multitags, which leads to excessive duplication of data in the cache and unexpected behavior when clearing and reading the cache. You can read more about it here. To fix this issue, add the following code to the composer.json and run composer dump-autoload

{
    "autoload": {
        "exclude-from-classmap": [
            "vendor/laravel/framework/src/Illuminate/Cache/TaggedCache.php"
        ],
        "files": [
            "vendor/kolirt/laravel-cacheable/src/Overrides/TaggedCache.php"
        ]
    }
}

Use the Cacheable trait in the target class

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;
}

Console commands

  • cacheable:install - Install cacheable package
  • cacheable:publish-config - Publish the config file

Methods

cache

Using the cache method, cache everything you need

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function exampleMethod()
    {
        return $this->cache(fn () => 'example data');
    }

    public function exampleMethodWithParams(int $id)
    {
        return $this->cache(fn () => 'example data with id ' . $id);
    }
}

clearCache

To clear the cache, use the clearCache method

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function clearExampleMethod() 
    {
        $this->clearCache('exampleMethod');
    }

    public function clearExampleMethodWithParams(int $id) 
    {
        $this->clearCache('exampleMethodWithParams', $id);
    }
}

updateCache

To update the cache, use the updateCache method

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function updateExampleMethod() 
    {
        $this->updateCache('exampleMethod', 'new example data');
    }
}

refreshCache

To refresh the cache, use the refreshCache method

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function exampleMethod() {
        return $this->cache(fn () => 'example data');
    }

    public function refreshExampleMethod() 
    {
        $this->refreshCache('exampleMethod');
    }
}

setCacheTime

To set the cache time, use the setCacheTime method

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    public function __construct()
    {
        $this->setCacheTime(now()->endOfDay());
    }
}

flushAllCache

Clearing the all cache works on tags. You have to switch the class to taggable mode

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;
    
    protected bool $taggable = true;
}

Or you can add tags to the class by using the appendCacheTags method without taggable mode

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;
    
    public function __construct() {
        $this->appendCacheTags(['tag1', 'tag2']);
    }
}

To flush all cache, use the flushAllCache method

$example = new Example();
$example->flushAllCache();

appendCacheTags

In addition to the basic tag that is added automatically in taggable mode, you can add additional tags that you need using the appendCacheTags method

use Kolirt\Cacheable\Traits\Cacheable;

class Example
{
    use Cacheable;

    protected bool $taggable = true;

    /** add additional tags for all methods */
    public function __construct()
    {
        $this->appendCacheTags(['tag1', 'tag2']);
    }

    /** or add additional tags for specific method */
    public function exampleMethod()
    {
        $this->appendCacheTags(['tag1', 'tag2']);
        return $this->cache(fn () => 'example data');
    }
}

Then, through Cache facade, you can delete the cache for the tag you need

use Illuminate\Support\Facades\Cache;

Cache::tags(['tag1'])->flush();
Cache::tags(['tag2'])->flush();
Cache::tags(['tag1', 'tag2'])->flush();

FAQ

Check closed issues to get answers for most asked questions

License

MIT

Other packages

Check out my other packages on my GitHub profile

统计信息

  • 总下载量: 50
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固