Skip to content
This repository was archived by the owner on Sep 6, 2019. It is now read-only.

Commit bfc7bc5

Browse files
author
pulkit
committed
added cache expiry
1 parent 0aea901 commit bfc7bc5

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,28 @@ class Model extends \Eloquent
4949

5050
Caching the model only works with using the `find`, `findMany` or `findOrFail` methods.
5151

52-
If you would like caching behavior like in Laravel 4 then consider using [dwightwatson/rememberable](https://github.com/dwightwatson/rememberable) which adds the `remember` function back into eloquent.
52+
If you would like caching behavior like in Laravel 4 then consider using [dwightwatson/rememberable](https://github.com/dwightwatson/rememberable) which adds the `remember` function back into eloquent.
53+
54+
You can optinally set the expiry time in minutes for the model, by default it is set to `1440` minutes (24 hours).
55+
56+
```php
57+
<?php
58+
59+
namespace App;
60+
61+
use PulkitJalan\Cacheable\Cacheable;
62+
63+
class Model extends \Eloquent
64+
{
65+
use Cacheable;
66+
67+
/**
68+
* Set the cache expiry time.
69+
*
70+
* @var int
71+
*/
72+
public $cacheExpiry = 60;
73+
}
74+
```
75+
76+
Models are cached using the models `table name` as the cache tag and the `id` as the key. There are observers which get registered in the trait to also remove from cache when the `updated`, `saved` or `deleted`.

src/Cacheable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
trait Cacheable
99
{
10+
/**
11+
* Set the cache expiry time.
12+
*
13+
* @var int
14+
*/
15+
public $cacheExpiry = 1440;
16+
1017
/**
1118
* The "booting" method of the model.
1219
*

src/Eloquent/Builder.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class Builder extends IlluminateBuilder
1515
* @param array $columns
1616
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null
1717
*/
18-
public function find($id, $columns = ['*'], $expire = 60)
18+
public function find($id, $columns = ['*'])
1919
{
2020
if (is_array($id)) {
2121
return $this->findMany($id, $columns);
2222
}
2323

24-
return Cache::tags($this->model->getTable())->remember($id, $expire, function () use ($id, $columns) {
24+
return Cache::tags($this->model->getTable())->remember($id, $this->model->cacheExpiry, function () use ($id, $columns) {
2525
return parent::find($id, $columns);
2626
});
2727
}
@@ -33,40 +33,16 @@ public function find($id, $columns = ['*'], $expire = 60)
3333
* @param array $columns
3434
* @return \Illuminate\Database\Eloquent\Collection
3535
*/
36-
public function findMany($ids, $columns = ['*'], $expire = 60)
36+
public function findMany($ids, $columns = ['*'])
3737
{
3838
if (empty($ids)) {
3939
return $this->model->newCollection();
4040
}
4141

4242
return $this->model->newCollection(
43-
Cache::tags($this->model->getTable())->rememberMany($ids, $expire, function ($ids) use ($columns) {
43+
Cache::tags($this->model->getTable())->rememberMany($ids, $this->model->cacheExpiry, function ($ids) use ($columns) {
4444
return parent::findMany($ids, $columns)->all();
4545
})
4646
);
4747
}
48-
49-
/**
50-
* Find a model by its primary key or throw an exception.
51-
*
52-
* @param mixed $id
53-
* @param array $columns
54-
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
55-
*
56-
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
57-
*/
58-
public function findOrFail($id, $columns = ['*'], $expire = 60)
59-
{
60-
$result = $this->find($id, $columns, $expire);
61-
62-
if (is_array($id)) {
63-
if (count($result) == count(array_unique($id))) {
64-
return $result;
65-
}
66-
} elseif (! is_null($result)) {
67-
return $result;
68-
}
69-
70-
throw (new ModelNotFoundException)->setModel(get_class($this->model));
71-
}
7248
}

0 commit comments

Comments
 (0)