Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/v1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Omranic committed Jun 21, 2016
2 parents 0a6f078 + a4bbc53 commit c211553
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 59 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](CONTRIBUTING.md).


## [v1.0.1] - 2016-06-21
- Update docs, docblocks, and fix homepage link
- Add per repository cache lifetime/driver support (Close #10)

## v1.0.0 - 2016-06-18
- Commit first draft

[v1.0.1]: https://github.com/rinvex/repository/compare/v1.0.0...v1.0.1
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rinvex Repository

**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.
**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.

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

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

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.

> **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.
### Prerequisites
Expand Down Expand Up @@ -236,7 +238,7 @@ return [
### EloquentRepository

The `Rinvex\Repository\Repositories\BaseRepository` is an abstract class with bare minimum implementation that concrete implementations must extend.
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:
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:
```php
use Rinvex\Repository\Repositories\EloquentRepository;

Expand Down Expand Up @@ -516,6 +518,7 @@ Lastly, you can disable cache per single request by passing the following query
> **Notes:**
> - You can control how long repository cache lasts through the `rinvex.repository.cache.lifetime` config option.
> - 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.
> - 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.

## Changelog
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rinvex/repository",
"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.",
"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.",
"keywords": [
"solid",
"rinvex",
Expand All @@ -9,12 +9,13 @@
"interface",
"repository",
"eloquent",
"granular",
"pattern",
"cache",
"trait"
],
"license": "MIT",
"homepage": "https://rinvex.com/repository.html",
"homepage": "https://rinvex.com/marketplace/rinvex-repository/",
"support": {
"email": "help@rinvex.com",
"issues": "https://github.com/rinvex/repository/issues",
Expand Down
46 changes: 30 additions & 16 deletions src/Contracts/RepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface RepositoryContract
public function setContainer(Container $container);

/**
* Return the IoC container instance or any of it's services.
* Get the IoC container instance or any of it's services.
*
* @param string|null $service
*
Expand Down Expand Up @@ -127,13 +127,15 @@ public function orderBy($column, $direction = 'asc');
/**
* Find an entity by its primary key.
*
* @param int $id
* @param array $columns
* @param array $with
* @param int $id
* @param array $columns
* @param array $with
* @param int $lifetime
* @param string $driver
*
* @return object
*/
public function find($id, $columns = ['*'], $with = []);
public function find($id, $columns = ['*'], $with = [], $lifetime = null, $driver = null);

/**
* Find an entity by one of it's attributes.
Expand All @@ -142,20 +144,24 @@ public function find($id, $columns = ['*'], $with = []);
* @param string $value
* @param array $columns
* @param array $with
* @param int $lifetime
* @param string $driver
*
* @return object
*/
public function findBy($attribute, $value, $columns = ['*'], $with = []);
public function findBy($attribute, $value, $columns = ['*'], $with = [], $lifetime = null, $driver = null);

/**
* Find all entities.
*
* @param array $columns
* @param array $with
* @param array $columns
* @param array $with
* @param int $lifetime
* @param string $driver
*
* @return \Illuminate\Support\Collection
*/
public function findAll($columns = ['*'], $with = []);
public function findAll($columns = ['*'], $with = [], $lifetime = null, $driver = null);

/**
* Paginate all entities.
Expand All @@ -164,23 +170,27 @@ public function findAll($columns = ['*'], $with = []);
* @param array $columns
* @param string $pageName
* @param int|null $page
* @param int $lifetime
* @param string $driver
*
* @throws \InvalidArgumentException
*
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $lifetime = null, $driver = null);

/**
* Find all entities matching where conditions.
*
* @param array $where
* @param array $columns
* @param array $with
* @param array $where
* @param array $columns
* @param array $with
* @param int $lifetime
* @param string $driver
*
* @return \Illuminate\Support\Collection
*/
public function findWhere(array $where, $columns = ['*'], $with = []);
public function findWhere(array $where, $columns = ['*'], $with = [], $lifetime = null, $driver = null);

/**
* Find all entities matching whereIn conditions.
Expand All @@ -189,10 +199,12 @@ public function findWhere(array $where, $columns = ['*'], $with = []);
* @param array $values
* @param array $columns
* @param array $with
* @param int $lifetime
* @param string $driver
*
* @return \Illuminate\Support\Collection
*/
public function findWhereIn($attribute, array $values, $columns = ['*'], $with = []);
public function findWhereIn($attribute, array $values, $columns = ['*'], $with = [], $lifetime = null, $driver = null);

/**
* Find all entities matching whereNotIn conditions.
Expand All @@ -201,10 +213,12 @@ public function findWhereIn($attribute, array $values, $columns = ['*'], $with =
* @param array $values
* @param array $columns
* @param array $with
* @param int $lifetime
* @param string $driver
*
* @return \Illuminate\Support\Collection
*/
public function findWhereNotIn($attribute, array $values, $columns = ['*'], $with = []);
public function findWhereNotIn($attribute, array $values, $columns = ['*'], $with = [], $lifetime = null, $driver = null);

/**
* Create a new entity with the given attributes.
Expand Down
28 changes: 18 additions & 10 deletions src/Repositories/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,35 @@ abstract class BaseRepository implements RepositoryContract
* @param string $method
* @param string $hash
* @param \Closure $closure
* @param int $lifetime
* @param string $driver
*
* @return mixed
*/
protected function executeCallback($class, $method, $hash, Closure $closure)
protected function executeCallback($class, $method, $hash, $lifetime = null, $driver = null, Closure $closure)
{
$cacheKey = $class.'@'.$method.'.'.$hash;
$config = $this->getContainer('config')->get('rinvex.repository.cache');
$lifetime = $lifetime ?: $config['lifetime'];

if ($this->isCacheableMethod($config, $method)) {
// Switch cache driver on runtime
if ($driver) {
$this->getContainer('cache')->setDefaultDriver($driver);
}

if ($this->isCacheableMethod($config, $method, $lifetime)) {
if (method_exists($this->getContainer('cache')->getStore(), 'tags')) {
return $config['lifetime'] === -1
return $lifetime === -1
? $this->getContainer('cache')->tags($this->getRepositoryId())->rememberForever($cacheKey, $closure)
: $this->getContainer('cache')->tags($this->getRepositoryId())->remember($cacheKey, $config['lifetime'], $closure);
: $this->getContainer('cache')->tags($this->getRepositoryId())->remember($cacheKey, $lifetime, $closure);
}

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

return $config['lifetime'] === -1
return $lifetime === -1
? $this->getContainer('cache')->rememberForever($cacheKey, $closure)
: $this->getContainer('cache')->remember($cacheKey, $config['lifetime'], $closure);
: $this->getContainer('cache')->remember($cacheKey, $lifetime, $closure);
}

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

/**
* Return the IoC container instance or any of it's services.
* Get the IoC container instance or any of it's services.
*
* @param string|null $service
*
Expand Down Expand Up @@ -359,13 +367,13 @@ protected function getCacheKeys($file)
*
* @param array $config
* @param string $method
* @param int $lifetime
*
* @return bool
*/
protected function isCacheableMethod($config, $method)
protected function isCacheableMethod($config, $method, $lifetime)
{
return $this->cacheEnabled
&& $config['lifetime']
return $this->cacheEnabled && $lifetime
&& in_array($method, $config['methods'])
&& ! $this->getContainer('request')->has($config['skip_uri']);
}
Expand Down
Loading

0 comments on commit c211553

Please sign in to comment.