Skip to content

Instantly share code, notes, and snippets.

@Omranic
Last active September 22, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Omranic/36e383535f95fbad1ec1bfc7e0610dc8 to your computer and use it in GitHub Desktop.
Save Omranic/36e383535f95fbad1ec1bfc7e0610dc8 to your computer and use it in GitHub Desktop.
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain. https://github.com/rinvex/repository
<?php
namespace App\Http\Controllers;
use App\Repositories\UserRepository;
class UsersController
{
public function baz(UserRepository $users)
{
// Find entity by primary key
$users->find(1);
// Find all entities
$users->findAll();
// Cache this individual query with redis (regardless of default cache driver)
$users->setCacheDriver('redis')->whereIn('id', [1, 2, 5, 8]);
// Cache this individual query for 123 minutes (regardless of default cache lifetime)
$users->setCacheLifetime(123)->whereNotIn('id', [1, 2, 5, 8]);
// Find user with username equal tester, but don't cache the result
$users->setCacheLifetime(0)->findBy('username', 'tester');
// Execute complex query logix, and cache results granularly with flexibility
$users->with('roles') // Retrieve roles relation as well
->where('username', '!=', 'test') // Find users with username != test
->where('first_name', '%like%', 'Test') // Find users with first name like Test
->whereNotIn('id', [1, 2, 5, 8]) // Find users with id not in 1, 2, 5, 8
->offset(5) // Skip first 5 results
->limit(9) // Limit results to 9 records
->orderBy('id', 'asc') // Order results ascending by id
->setCacheDriver('redis') // Cache results using redis
->setCacheLifetime(123); // Cache results for 123 minutes
// Create a new entity with name attribute
$users->create(['name' => 'Example']);
}
}
@ebisbe
Copy link

ebisbe commented Sep 19, 2016

I don't understand why in these examples you don't use ->get()

This is not working for me
$product->featured()->limit(2)->get();
Featured() is a scope and I'm using https://github.com/jenssegers/laravel-mongodb with get() I don't see any caching at all. And without like your examples it's not working for me.

Can you help me?

@ebisbe
Copy link

ebisbe commented Sep 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment