ulex/epic-repositories

EPIC Repositories - 一个史诗级的 Laravel 扩展包

2.2.3 2024-05-07 10:30 UTC

This package is auto-updated.

Last update: 2024-09-07 11:10:04 UTC


README

文档、安装和使用说明

首先,通过 Composer 安装此包

composer require ulex/epic-repositories

服务提供者

对于 Laravel

您应该发布 RepositoriesServiceProvider

php artisan vendor:publish --provider="Ulex\EpicRepositories\RepositoriesServiceProvider" --tag=epic-repositories-config

可选:服务提供者将自动注册。或者,您可以在 config/app.php 文件中手动添加服务提供者:Laravel

'providers' => [
// ...
Ulex\EpicRepositories\RepositoriesServiceProvider::class,
];

对于 Lumen

在您的 bootstrap/app.php 中添加以下内容

$app->register(Ulex\EpicRepositories\RepositoriesServiceProvider::class);

配置

如果配置文件 epic-repositories.php 未发布,请使用以下命令将其复制到配置文件夹中

cp vendor/ulex/epic-repositories/config/epic-repositories.php config/epic-repositories.php

花一些时间来审查此配置文件。在这里,您可以调整各种配置以设置您的仓库。

  • 为您的每个模型设置自定义的 TTL
  • 设置您的文件夹的命名空间
  • 启用/添加您将使用的仓库和装饰器。
  • 为每个仓库添加您的模型绑定以及为该仓库使用哪个装饰器。

注意:为了使用 elastic 仓库,您必须在 composer.json 中添加 "elasticsearch/elasticsearch" 包。

为模型创建仓库/装饰器及其接口

首先在 config/epic-repositories 中声明您的模型

...
'bindings' => [
        'eloquent' => [
            'decorators' => ['caching'],
            'models' => [
                'User' => App\Models\User::class,
                //'NewModel => App\Models\NewModel::class,
            ]
        ],
...

运行以下 php artisan 命令

php artisan make:epic

根据您的配置,必要的文件夹和类将在您的 app/Repositories 文件夹中创建。示例

UserEloquentRepository created successfully.
UserEloquentInterface created successfully.
UserEloquentCachingDecorator created successfully.

如何使用

此包提供了一个抽象结构,该结构使用带有缓存装饰器的 Repository 设计模式为您应用程序提供。

安装后,您可以创建用于模型的仓库,该仓库缓存查询数据。Eloquent 和 Elastic 仓库已提供并启用,可以立即使用。对于您应用程序上的任何数据资源,遵循相同的原则。

# Example when injecting to a controller 
use App\Repositories\Interfaces\UserEloquentInterface;

public function __construct(UserEloquentInterface $userRepository)
{
    $this->userRepository = $userRepository;
}

...

public function find($id)
{
    //retrieve from db and then cache the result
    $user = $this->userRepository->find($id);
    //retrieve straight from source, uncached
    $user = $this->userRepository->fromSource()->find($id);
} 

扩展模型's CachingDecorator

对于 GET 函数,使用与 AbstractCachingDecorator 中相同的方式使用 remember 函数。这将确保此函数被正确缓存和无效化。

remember 函数签名

protected function remember(string $function, $arguments, bool $isCollection = false)

返回单个结果的函数

PostsEloquentCachingDecorator.php

public function getLatestPost($user_id)
    {
        return $this->remember(__FUNCTION__, func_get_args());
    }

注意:请记住通过在模型的 CachingDecorator 中扩展 flushGetKeys 来添加新函数的缓存无效化。

public function flushGetKeys($model, $attributes = null)
{
    $this->flushFunction('getLatestPost', ['user_id' => $model->user_id]);
    parent::flushGetKeys($model, $attributes);
}

PostsEloquentRepository.php

在模型的仓库中添加查询

public function getLatestPost($user_id)
{
    return $this->model->query()->where('user_id', '=', $user_id)->latest()->first();
}

返回集合的函数

对于返回集合的 GET 函数,您必须在 remember 函数的 $isCollection 参数上传递 TRUE

public function getUserPosts($user_id)
{
    return $this->remember(__FUNCTION__, func_get_args(), true);
}

上述将在 flushGetKeys 中刷新,该函数调用 flushCollections()

注意:如果您正在添加创建新列的函数,您需要在条目创建后添加 $this->flushCollections();,以便刷新所有集合并包含您的新条目。请检查 AbstractCachingDecorator 中的 create

贡献

此包主要基于 Jeffrey Way 的出色 Laracasts 课程,该课程在 Laravel From Scratch 系列中使用仓库设计模式。

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件