在Laravel上使用存储库模式和缓存

我想知道如何在带有存储库模式的Laravel中使用缓存?

目前我有这个:

BucketRepository.php
<?php
namespace App\Model\Repositories\Bucket;

use App\Model\Entities\BucketEntity as Bucket;

class BucketRepository implements BucketRepositoryInterface
{
    /**
     * @var
     */
    private $bucket;

    /**
     * BucketRepository constructor.
     * @param Bucket $bucket
     */
    public function __construct(Bucket $bucket)
    {
        $this->bucket = $bucket;
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getById($id)
    {
        return $this->bucket
            ->where('id', $id)
            ->first();
    }
}
BucketRepositoryInterface.php
<?php

namespace App\Model\Repositories\Bucket;

interface BucketRepositoryInterface
{
    /**
     * @param $id
     * @return mixed
     */
    public function getById($id);
}
CacheBucketRepository.php
<?php

namespace App\Model\Repositories\Bucket;

use Illuminate\Support\Facades\Cache;

class CacheBucketRepository implements BucketRepositoryInterface
{
    /**
     * @var BucketRepository
     */
    private $bucket_repository;

    /**
     * @var
     */
    private $expiration = 120*60;

    /**
     * CacheCountryRepository constructor.
     * @param BucketRepository $bucket_repository
     */
    public function __construct(BucketRepository $bucket_repository)
    {
        $this->bucket_repository = $bucket_repository;
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getById($id)
    {
        dd('ok');
        return Cache::remember('bucket_' . $id, $this->expiration, function () use ($id) {
            return $this->bucket_repository->getById($id);
        });
    }
}

我添加了这个$​​ this-> app-> bind(BucketRepositoryInterface :: class,CacheBucketRepository :: class);

AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

use App\Model\Repositories\Bucket\CacheBucketRepository;
use App\Model\Repositories\Bucket\BucketRepositoryInterface;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Interface
        $this->app->bind(BucketRepositoryInterface::class, CacheBucketRepository::class);

    }
}

但是我从来没有看到我的“ dd('ok');”在CacheBucketRepository.php中的getById函数中

评论