承接 ulex/epic-repositories 相关项目开发

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

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

ulex/epic-repositories

最新稳定版本:2.2.3

Composer 安装命令:

composer create-project ulex/epic-repositories

包简介

EPIC Repositories - An epic Laravel package

README 文档

README

Documentation, Installation, and Usage Instructions

First, install the package via Composer:

composer require ulex/epic-repositories

Service Provider

For Laravel

You should publish the RepositoriesServiceProvider:

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

Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file: Laravel

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

For Lumen

In your bootstrap/app.php add this:

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

Config

If config file epic-repositories.php was not published copy it to config folder with:

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

Take some time to review this config file. Here you can adjust various configurations for your repositories setup.

  • Set a custom TTL for each of you models.
  • Set the namespaces of your folders
  • Enable/add the repositories and decorators you will use.
  • Add your model bindings for each repository and which decorators to use for that repository.

Note: In order to use elastic repository you will have to add "elasticsearch/elasticsearch" package to your composer.json.

Create Repository/ies, Decorator/s with their Interfaces for a Model

First declare your models in config/epic-repositories

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

Run the following php artisan command:

php artisan make:epic

Depending on you configuration, the necessary folders & classes will be created in your app/Repositories folder. Example:

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

How to use

This package provides an abstract structure that uses the Repository design pattern with caching decorator/s for you application.

Once installed you can create Repositories for your models that cache the data from your queries. Eloquent and Elastic Repositories are provided and ready to use if enabled. Follow the same principle for any data resource you have on your application.

# 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);
} 

Extending a model's CachingDecorator

For GET functions use the remember function the same way as in the AbstractCachingDecorator. This will ensure that this function is cached and invalidated properly.

Function remember signature

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

Functions that return a single result:

PostsEloquentCachingDecorator.php

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

Note: Remember to add the cache invalidation of the new function by extending flushGetKeys in the model's CachingDecorator.

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

PostsEloquentRepository.php

Add the query in the model's repository

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

Functions that return a collection of results:

For GET functions that return collections you MUST pass TRUE to the $isCollection param on remember function.

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

The above will be flushed in flushGetKeys which calls flushCollections().

Note: If you are adding functions that create a new column, you need to add $this->flushCollections(); after the entry is created so that all collections are flushed and can include your new entry/ies. Check create in AbstractCachingDecorator.

Contributing

This package is mostly based on Jeffrey Way's awesome Laracasts lessons when using the repository design pattern on Laravel From Scratch series.

License

The MIT License (MIT). Please see License File for more information.

ulex/epic-repositories 适用场景与选型建议

ulex/epic-repositories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.42k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 ulex/epic-repositories 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 7.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-24