Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cache-ttl-in-seconds #762

Merged
merged 6 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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