Skip to content

Commit

Permalink
fix cache-ttl-in-seconds (andersao#762)
Browse files Browse the repository at this point in the history
* fill up search fields

* Update CacheableRepository.php

* clean
  • Loading branch information
ThanhSonITNIC authored Oct 14, 2021
1 parent 2425139 commit 834d61f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Prettus/Repository/Contracts/CacheableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();


/**
Expand Down
38 changes: 24 additions & 14 deletions src/Prettus/Repository/Traits/CacheableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand Down

0 comments on commit 834d61f

Please sign in to comment.