diff --git a/README.md b/README.md index 47e2de2c..118e9984 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositorySe - setCacheRepository(CacheRepository $repository) - getCacheRepository() - getCacheKey($method, $args = null) -- getCacheMinutes() +- getCacheTime() - skipCache($status = true) ### Prettus\Repository\Contracts\PresenterInterface diff --git a/src/Prettus/Repository/Contracts/CacheableInterface.php b/src/Prettus/Repository/Contracts/CacheableInterface.php index 366e87bd..bf928d62 100644 --- a/src/Prettus/Repository/Contracts/CacheableInterface.php +++ b/src/Prettus/Repository/Contracts/CacheableInterface.php @@ -37,11 +37,11 @@ public function getCacheRepository(); public function getCacheKey($method, $args = null); /** - * Get cache minutes + * Get cache time * * @return int */ - public function getCacheMinutes(); + public function getCacheTime(); /** diff --git a/src/Prettus/Repository/Traits/CacheableRepository.php b/src/Prettus/Repository/Traits/CacheableRepository.php index fdb3bc16..d2b1030a 100644 --- a/src/Prettus/Repository/Traits/CacheableRepository.php +++ b/src/Prettus/Repository/Traits/CacheableRepository.php @@ -178,14 +178,24 @@ protected function serializeCriterion($criterion) } /** - * Get cache minutes + * Get cache time + * + * Return minutes: version < 5.8 + * Return seconds: version >= 5.8 * * @return int */ - public function getCacheMinutes() + public function getCacheTime() { $cacheMinutes = isset($this->cacheMinutes) ? $this->cacheMinutes : config('repository.cache.minutes', 30); + /** + * https://laravel.com/docs/5.8/upgrade#cache-ttl-in-seconds + */ + if ($this->versionCompare($this->app->version(), "5.7.*", ">")) { + return $cacheMinutes * 60; + } + return $cacheMinutes; } @@ -203,8 +213,8 @@ public function all($columns = ['*']) } $key = $this->getCacheKey('all', func_get_args()); - $minutes = $this->getCacheMinutes(); - $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($columns) { + $time = $this->getCacheTime(); + $value = $this->getCacheRepository()->remember($key, $time, function () use ($columns) { return parent::all($columns); }); @@ -230,8 +240,8 @@ public function paginate($limit = null, $columns = ['*'], $method = 'paginate') $key = $this->getCacheKey('paginate', func_get_args()); - $minutes = $this->getCacheMinutes(); - $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($limit, $columns, $method) { + $time = $this->getCacheTime(); + $value = $this->getCacheRepository()->remember($key, $time, function () use ($limit, $columns, $method) { return parent::paginate($limit, $columns, $method); }); @@ -255,8 +265,8 @@ public function find($id, $columns = ['*']) } $key = $this->getCacheKey('find', func_get_args()); - $minutes = $this->getCacheMinutes(); - $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($id, $columns) { + $time = $this->getCacheTime(); + $value = $this->getCacheRepository()->remember($key, $time, function () use ($id, $columns) { return parent::find($id, $columns); }); @@ -281,8 +291,8 @@ public function findByField($field, $value = null, $columns = ['*']) } $key = $this->getCacheKey('findByField', func_get_args()); - $minutes = $this->getCacheMinutes(); - $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($field, $value, $columns) { + $time = $this->getCacheTime(); + $value = $this->getCacheRepository()->remember($key, $time, function () use ($field, $value, $columns) { return parent::findByField($field, $value, $columns); }); @@ -306,8 +316,8 @@ public function findWhere(array $where, $columns = ['*']) } $key = $this->getCacheKey('findWhere', func_get_args()); - $minutes = $this->getCacheMinutes(); - $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($where, $columns) { + $time = $this->getCacheTime(); + $value = $this->getCacheRepository()->remember($key, $time, function () use ($where, $columns) { return parent::findWhere($where, $columns); }); @@ -330,8 +340,8 @@ public function getByCriteria(CriteriaInterface $criteria) } $key = $this->getCacheKey('getByCriteria', func_get_args()); - $minutes = $this->getCacheMinutes(); - $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($criteria) { + $time = $this->getCacheTime(); + $value = $this->getCacheRepository()->remember($key, $time, function () use ($criteria) { return parent::getByCriteria($criteria); });