Skip to content

Commit c211553

Browse files
committed
Merge branch 'release/v1.0.1'
2 parents 0a6f078 + a4bbc53 commit c211553

File tree

6 files changed

+105
-59
lines changed

6 files changed

+105
-59
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ All notable changes to this project will be documented in this file.
55
This project adheres to [Semantic Versioning](CONTRIBUTING.md).
66

77

8+
## [v1.0.1] - 2016-06-21
9+
- Update docs, docblocks, and fix homepage link
10+
- Add per repository cache lifetime/driver support (Close #10)
11+
812
## v1.0.0 - 2016-06-18
913
- Commit first draft
14+
15+
[v1.0.1]: https://github.com/rinvex/repository/compare/v1.0.0...v1.0.1

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Rinvex Repository
22

3-
**Rinvex Repository** is an intuitive, smart, and simple implementation of Repository Pattern used to abstract the data layer, making apps more flexible to maintain adhering to SOLID principles.
3+
**Rinvex Repository** is an intuitive, smart, and simple implementation of Repository Pattern used to abstract the data layer, with extremely flexible & granular caching system, making apps more flexible to maintain.
44

55
[![Packagist](https://img.shields.io/packagist/v/rinvex/repository.svg?label=Packagist&style=flat-square)](https://packagist.org/packages/rinvex/repository)
66
[![License](https://img.shields.io/packagist/l/rinvex/repository.svg?label=License&style=flat-square)](https://github.com/rinvex/repository/blob/develop/LICENSE)
@@ -64,6 +64,8 @@ The best and easiest way to install this package is through [Composer](https://g
6464

6565
This package fully compatible with **Laravel** `5.2.*`.
6666

67+
While this package tends to be framework-agnostic, it embraces Laravel culture and best practices to some extent. It's tested mainly with Laravel but you still can use it with other frameworks or even without any framework if you want.
68+
6769
> **Note:** Global scope features not tested with Laravel 5.1, and probably won't work as it has been drastically changed in 5.2 releases. Checkout Laravel's [Global Scopes](https://laravel.com/docs/5.2/eloquent#global-scopes) documentation for further details.
6870
6971
### Prerequisites
@@ -236,7 +238,7 @@ return [
236238
### EloquentRepository
237239

238240
The `Rinvex\Repository\Repositories\BaseRepository` is an abstract class with bare minimum implementation that concrete implementations must extend.
239-
The `Rinvex\Repository\Repositories\EloquentRepository` is currently the only available repository implementation, it makes it easy to create new instances of a model and to retrieve or override the model during runtime, in addition to performing multiple useful operations on models. To use `EloquentRepository` your repository MUST extend it first:
241+
The `Rinvex\Repository\Repositories\EloquentRepository` is currently the only available repository implementation, it makes it easy to create new eloquent model instances and to retrieve or override the model during runtime, in addition to performing multiple useful operations on models. To use `EloquentRepository` your repository MUST extend it first:
240242
```php
241243
use Rinvex\Repository\Repositories\EloquentRepository;
242244

@@ -516,6 +518,7 @@ Lastly, you can disable cache per single request by passing the following query
516518
> **Notes:**
517519
> - You can control how long repository cache lasts through the `rinvex.repository.cache.lifetime` config option.
518520
> - This package utilizes cache tags in a very smart way, even if your chosen cache driver doesn't support cache tags it will manage virtually on it's own for precise cache management. Behind scenes it uses a json file to store cache keys that you can modify through the `rinvex.repository.cache.keys_file` config option.
521+
> - This package follows the FIG PHP Standards Recommendations compliant with the [PSR-1: Basic Coding Standard](http://www.php-fig.org/psr/psr-1/), [PSR-2: Coding Style Guide](http://www.php-fig.org/psr/psr-2/) and [PSR-4: Autoloader](http://www.php-fig.org/psr/psr-4/) to ensure a high level of interoperability between shared PHP code.
519522
520523

521524
## Changelog

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rinvex/repository",
3-
"description": "Rinvex Repository is an intuitive, smart, and simple implementation of Repository Pattern used to abstract the data layer, making apps more flexible to maintain adhering to SOLID principles.",
3+
"description": "Rinvex Repository is an intuitive, smart, and simple implementation of Repository Pattern used to abstract the data layer, with extremely flexible & granular caching system, making apps more flexible to maintain.",
44
"keywords": [
55
"solid",
66
"rinvex",
@@ -9,12 +9,13 @@
99
"interface",
1010
"repository",
1111
"eloquent",
12+
"granular",
1213
"pattern",
1314
"cache",
1415
"trait"
1516
],
1617
"license": "MIT",
17-
"homepage": "https://rinvex.com/repository.html",
18+
"homepage": "https://rinvex.com/marketplace/rinvex-repository/",
1819
"support": {
1920
"email": "[email protected]",
2021
"issues": "https://github.com/rinvex/repository/issues",

src/Contracts/RepositoryContract.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface RepositoryContract
3030
public function setContainer(Container $container);
3131

3232
/**
33-
* Return the IoC container instance or any of it's services.
33+
* Get the IoC container instance or any of it's services.
3434
*
3535
* @param string|null $service
3636
*
@@ -127,13 +127,15 @@ public function orderBy($column, $direction = 'asc');
127127
/**
128128
* Find an entity by its primary key.
129129
*
130-
* @param int $id
131-
* @param array $columns
132-
* @param array $with
130+
* @param int $id
131+
* @param array $columns
132+
* @param array $with
133+
* @param int $lifetime
134+
* @param string $driver
133135
*
134136
* @return object
135137
*/
136-
public function find($id, $columns = ['*'], $with = []);
138+
public function find($id, $columns = ['*'], $with = [], $lifetime = null, $driver = null);
137139

138140
/**
139141
* Find an entity by one of it's attributes.
@@ -142,20 +144,24 @@ public function find($id, $columns = ['*'], $with = []);
142144
* @param string $value
143145
* @param array $columns
144146
* @param array $with
147+
* @param int $lifetime
148+
* @param string $driver
145149
*
146150
* @return object
147151
*/
148-
public function findBy($attribute, $value, $columns = ['*'], $with = []);
152+
public function findBy($attribute, $value, $columns = ['*'], $with = [], $lifetime = null, $driver = null);
149153

150154
/**
151155
* Find all entities.
152156
*
153-
* @param array $columns
154-
* @param array $with
157+
* @param array $columns
158+
* @param array $with
159+
* @param int $lifetime
160+
* @param string $driver
155161
*
156162
* @return \Illuminate\Support\Collection
157163
*/
158-
public function findAll($columns = ['*'], $with = []);
164+
public function findAll($columns = ['*'], $with = [], $lifetime = null, $driver = null);
159165

160166
/**
161167
* Paginate all entities.
@@ -164,23 +170,27 @@ public function findAll($columns = ['*'], $with = []);
164170
* @param array $columns
165171
* @param string $pageName
166172
* @param int|null $page
173+
* @param int $lifetime
174+
* @param string $driver
167175
*
168176
* @throws \InvalidArgumentException
169177
*
170178
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
171179
*/
172-
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
180+
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $lifetime = null, $driver = null);
173181

174182
/**
175183
* Find all entities matching where conditions.
176184
*
177-
* @param array $where
178-
* @param array $columns
179-
* @param array $with
185+
* @param array $where
186+
* @param array $columns
187+
* @param array $with
188+
* @param int $lifetime
189+
* @param string $driver
180190
*
181191
* @return \Illuminate\Support\Collection
182192
*/
183-
public function findWhere(array $where, $columns = ['*'], $with = []);
193+
public function findWhere(array $where, $columns = ['*'], $with = [], $lifetime = null, $driver = null);
184194

185195
/**
186196
* Find all entities matching whereIn conditions.
@@ -189,10 +199,12 @@ public function findWhere(array $where, $columns = ['*'], $with = []);
189199
* @param array $values
190200
* @param array $columns
191201
* @param array $with
202+
* @param int $lifetime
203+
* @param string $driver
192204
*
193205
* @return \Illuminate\Support\Collection
194206
*/
195-
public function findWhereIn($attribute, array $values, $columns = ['*'], $with = []);
207+
public function findWhereIn($attribute, array $values, $columns = ['*'], $with = [], $lifetime = null, $driver = null);
196208

197209
/**
198210
* Find all entities matching whereNotIn conditions.
@@ -201,10 +213,12 @@ public function findWhereIn($attribute, array $values, $columns = ['*'], $with =
201213
* @param array $values
202214
* @param array $columns
203215
* @param array $with
216+
* @param int $lifetime
217+
* @param string $driver
204218
*
205219
* @return \Illuminate\Support\Collection
206220
*/
207-
public function findWhereNotIn($attribute, array $values, $columns = ['*'], $with = []);
221+
public function findWhereNotIn($attribute, array $values, $columns = ['*'], $with = [], $lifetime = null, $driver = null);
208222

209223
/**
210224
* Create a new entity with the given attributes.

src/Repositories/BaseRepository.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,35 @@ abstract class BaseRepository implements RepositoryContract
6363
* @param string $method
6464
* @param string $hash
6565
* @param \Closure $closure
66+
* @param int $lifetime
67+
* @param string $driver
6668
*
6769
* @return mixed
6870
*/
69-
protected function executeCallback($class, $method, $hash, Closure $closure)
71+
protected function executeCallback($class, $method, $hash, $lifetime = null, $driver = null, Closure $closure)
7072
{
7173
$cacheKey = $class.'@'.$method.'.'.$hash;
7274
$config = $this->getContainer('config')->get('rinvex.repository.cache');
75+
$lifetime = $lifetime ?: $config['lifetime'];
7376

74-
if ($this->isCacheableMethod($config, $method)) {
77+
// Switch cache driver on runtime
78+
if ($driver) {
79+
$this->getContainer('cache')->setDefaultDriver($driver);
80+
}
81+
82+
if ($this->isCacheableMethod($config, $method, $lifetime)) {
7583
if (method_exists($this->getContainer('cache')->getStore(), 'tags')) {
76-
return $config['lifetime'] === -1
84+
return $lifetime === -1
7785
? $this->getContainer('cache')->tags($this->getRepositoryId())->rememberForever($cacheKey, $closure)
78-
: $this->getContainer('cache')->tags($this->getRepositoryId())->remember($cacheKey, $config['lifetime'], $closure);
86+
: $this->getContainer('cache')->tags($this->getRepositoryId())->remember($cacheKey, $lifetime, $closure);
7987
}
8088

8189
// Store cache keys by mimicking cache tags
8290
$this->storeCacheKeys($class, $method, $hash, $config['keys_file']);
8391

84-
return $config['lifetime'] === -1
92+
return $lifetime === -1
8593
? $this->getContainer('cache')->rememberForever($cacheKey, $closure)
86-
: $this->getContainer('cache')->remember($cacheKey, $config['lifetime'], $closure);
94+
: $this->getContainer('cache')->remember($cacheKey, $lifetime, $closure);
8795
}
8896

8997
return call_user_func($closure);
@@ -104,7 +112,7 @@ public function setContainer(Container $container)
104112
}
105113

106114
/**
107-
* Return the IoC container instance or any of it's services.
115+
* Get the IoC container instance or any of it's services.
108116
*
109117
* @param string|null $service
110118
*
@@ -359,13 +367,13 @@ protected function getCacheKeys($file)
359367
*
360368
* @param array $config
361369
* @param string $method
370+
* @param int $lifetime
362371
*
363372
* @return bool
364373
*/
365-
protected function isCacheableMethod($config, $method)
374+
protected function isCacheableMethod($config, $method, $lifetime)
366375
{
367-
return $this->cacheEnabled
368-
&& $config['lifetime']
376+
return $this->cacheEnabled && $lifetime
369377
&& in_array($method, $config['methods'])
370378
&& ! $this->getContainer('request')->has($config['skip_uri']);
371379
}

0 commit comments

Comments
 (0)