litermi/cache-query-builder

这是一个从查询生成和在使用特定方法时清除缓存的包提供程序

1.0.49 2024-09-13 14:21 UTC

README

Software License

关于

cache-query-builder 包允许您提高性能并降低数据库请求的压力。

如何创建 composer 包的教程

功能

  • 缓存查询获取列和值
  • 缓存子查询/关系
  • 根据模型设置时间
  • 从缓存获取和获取第一个值的方法
  • 保存、插入、删除到缓存的方法

安装

在您的 composer.json 中添加 litermi/cache-query-builder 包并更新依赖关系

composer require litermi/cache-query-builder

配置

设置提供者

'providers' => [
    // ...
    Litermi\Cache\Providers\ServiceProvider::class,
],

默认设置在 config/cache-query.php 中。发布配置以复制文件到您自己的配置

php artisan vendor:publish --provider="Litermi\Cache\Providers\ServiceProvider"

注意:这是必要的,以便您可以更改默认配置

使用方法

要缓存查询,您需要使用扩展类

class Product extends CacheModel
{
}

要缓存查询,您需要使用方法:getFromCache 或 firstCache

        return Product::query()
            ->where('active', ModelConst::ENABLED)
            ->with($relations)
            ->getFromCache(['*'], $tags);

如果您想清除缓存,可以使用方法:saveWithCache、insertWithCache、deleteWithCache

            $product = new Product();
            $product->saveWithCache();
            Product::insertWithCache($values);
            $product->deleteWithCache();

许可证

在 MIT 许可证下发布,请参阅 LICENSE