定制 kolirt/laravel-cacheable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kolirt/laravel-cacheable

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

kolirt/laravel-cacheable 适用场景与选型建议

kolirt/laravel-cacheable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cache」 「laravel」 「cacheable」 「laravel-cache」 「cacheable-methods」 「cacheable-method」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kolirt/laravel-cacheable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kolirt/laravel-cacheable 我们能提供哪些服务?
定制开发 / 二次开发

基于 kolirt/laravel-cacheable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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