定制 stevenmaguire/laravel-cache 二次开发

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

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

stevenmaguire/laravel-cache

Composer 安装命令:

composer require stevenmaguire/laravel-cache

包简介

Seamlessly adding caching to Laravel service objects

README 文档

README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Seamlessly adding caching to Laravel Eloquent service objects.

Install

Via Composer

$ composer require stevenmaguire/laravel-cache

Usage

Include the base service

Extend your service class with the EloquentCache class.

class UserRegistrar extends Stevenmaguire\Laravel\Services\EloquentCache
{
    //
}

Implement the interface and use the trait.

class UserRegistrar implements Stevenmaguire\Laravel\Contracts\Cacheable
{
    use \Stevenmaguire\Laravel\Services\EloquentCacheTrait;
}

Construct queries

Build queries using Eloquent and request cache object.

use App\User;
use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function getAllUsers()
    {
        $query = $this->user->query();

        return $this->cache('all', $query);
    }

    public function getUserById($id)
    {
        $query = $this->user->where('id', $id);

        return $this->cache('id('.$id.')', $query, 'first');
    }

    public function getRecent($skip = 0, $take = 100)
    {
        $query = $this->user->orderBy('created_at', 'desc')
            ->take($take)
            ->skip($skip);

        return $this->cache('recent('.$skip.','.$take.')', $query);
    }
}

The cache method takes three parameters:

  • The unique key associated with the method's intentions
  • The query Builder object for the Eloquent query
  • The optional verb, get, first, list, paginate etc; get by default

If the method associated with the optional verb takes parameters, like paginate, the parameters can be expressed as a comma separated list following the verb and a colon. If a parameter expects an array of literal values, these may be expressed as a pipe delimited sting.

/**
 * Paginate users with all pagination parameters
 */
public function getAllUsers()
{
    $query = $this->user->query();

    return $this->cache('all', $query, 'paginate:15,id|email|name,sheet,2');
    // $query->paginate(15, ['id', 'email', 'name'], 'sheet', 2);
}

The cache service will automatically index all of the unique keys used by your application. These keys will be used when the flushCache method is called on each service implementing the base cache service.

Configure caching

For each of the services you implement using the EloquentCache you can configure the following:

Duration of cache minutes

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheForMinutes = 15;

    //
}

Disable caching

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableCaching = false;

    //
}

Disable logging

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $enableLogging = false;

    //
}

Set a custom cache index for the keys

use Stevenmaguire\Laravel\Services\EloquentCache;

class UserRegistrar extends EloquentCache
{
    protected $cacheIndexKey = 'my-service-keys-index';

    //
}

Flushing cache

Each service object that implements caching via Stevenmaguire\Laravel\Services\EloquentCache can flush its own cache, independently of other consuming services.

Your application can flush the cache for all keys within the service object serviceKey group.

$userRegistrar->flushCache();

Your application can only flush the cache for keys within the service object serviceKey group that match a particular regular expression pattern.

// Flush cache for all cached users with a single digit user id
$userRegistrar->flushCache('^id\([0-9]{1}\)$');

Bind to IoC Container

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->when('App\Handlers\Events\UserHandler')
          ->needs('Stevenmaguire\Laravel\Contracts\Cacheable')
          ->give('App\Services\UserRegistrar');
    }
}

In this particular example, UserHandler is responsible for flushing the user service cache when a specific event occurs. The UserHandler takes a dependacy on the flushCache method within the UserRegistrar service.

Testing

$ ./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email stevenmaguire@gmail.com instead of using the issue tracker.

Credits

License

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

stevenmaguire/laravel-cache 适用场景与选型建议

stevenmaguire/laravel-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.76k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2015 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 17
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-28