2017 © Pedro Peláez
 

library rememberable

Query caching for Laravel 5

image

creativelittledots/rememberable

Query caching for Laravel 5

  • Monday, February 12, 2018
  • by terence1990
  • Repository
  • 1 Watchers
  • 0 Stars
  • 89 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 55 Forks
  • 0 Open issues
  • 18 Versions
  • 2 % Grown

The README.md

Rememberable, Laravel 5 query cache

Total Downloads Latest Stable Version Latest Unstable Version License, (*1)

Rememberable is an Eloquent trait for Laravel 5.0+ that brings back the remember() query functions from Laravel 4.2. This makes it super easy to cache your query results for an adjustable amount of time., (*2)

// Get a the first user's posts and remember them for a day.
User::first()->remember(1440)->posts()->get();

It works by simply remembering the SQL query that was used and storing the result. If the same query is attempted while the cache is persisted it will be retrieved from the store instead of hitting your database again., (*3)

Installation

Install using Composer, just as you would anything else., (*4)

composer require creativelittledots/rememberable

The easiest way to get started with Eloquent is to create an abstract App\Model which you can extend your application models from. In this base model you can import the rememberable trait which will extend the same caching functionality to any queries you build off your model., (*5)

<?php
namespace App;

use Watson\Rememberable\Rememberable;
use Illuminate\Database\Eloquent\Model as Eloquent;

abstract class Model extends Eloquent
{
    use Rememberable;
}

Now, just ensure that your application models from this new App\Model instead of Eloquent., (*6)

<?php
namespace App;

class Post extends Model
{
    //
}

Alternatively, you can simply apply the trait to each and every model you wish to use remember() on., (*7)

Usage

Using the remember method is super simple. Just pass the number of minutes you want to store the result of that query in the cache for, and whenever the same query is called within that time frame the result will be pulled from the cache, rather than from the database again., (*8)

// Remember the number of users for an hour.
$users = User::remember(60)->count();

Cache tags

If you want to tag certain queries you can add cacheTags('tag_name') to your query. Please notice that cache tags are not supported by all cache drivers., (*9)

// Remember the number of users for an hour and tag it with 'user_queries'
User::remember(60)->cacheTags('user_queries')->count();

Cache prefix

If you want a unique prefix added to the cache key for each of your queries (say, if your cache doesn't support tagging), you can add prefix('prefix') to your query., (*10)

// Remember the number of users for an hour and prefix the key with 'users'
User::remember(60)->prefix('users')->count();

Alternatively, you can add the $rememberCachePrefix property to your model to always use that cache prefix., (*11)

Cache driver

If you want to use a custom cache driver (defined in config/cache.php) you can add cacheDriver('cacheDriver') to your query., (*12)

// Remember the number of users for an hour using redis as cache driver
User::remember(60)->cacheDriver('redis')->count();

Alternatively, you can add the $rememberCacheDriver property to your model to always use that cache driver., (*13)

Model wide cache tag

You can set a cache tag for all queries of a model by setting the $rememberCacheTag property with an unique string that should be used to tag the queries., (*14)

Relationships

Validating works by caching queries on a query-by-query basis. This means that when you perform eager-loading those additional queries will not be cached as well unless explicitly specified. You can do that by using a callback with your eager-loads., (*15)

$users = User::where("id", ">", "1")
    ->with(['posts' => function ($q) { $q->remember(10); }])
    ->remember(10)
    ->take(5)
    ->get();

Always enable

You can opt-in to cache all queries of a model by setting the $rememberFor property with the number of minutes you want to cache results for. Use this feature with caution as it could lead to unexpected behaviour and stale data in your app if you're not familiar with how it works., (*16)

Cache flushing

You can flush a single query by simple using:, (*17)

User::flushCache();

This works for all queries:, (*18)

User::where('active', 1)->flushCache();

Based on the architecture of the package it is possible to delete the cache for a both a single query and any tagged queries using cache tags, you are able to flush the cache for the tag:, (*19)

User::flushCache('user_queries');

If you used the $rememberCacheTag property you can use the method without a parameter and the caches for the tag set by $rememberCacheTag are flushed:, (*20)

User::flushCache();

The Versions

12/02 2018

dev-master

9999999-dev

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

12/02 2018

2.0.8

2.0.8.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

06/02 2018

2.0.7

2.0.7.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

09/01 2018

2.0.6

2.0.6.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

01/11 2017

dev-pull

dev-pull

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

01/11 2017

2.0.5

2.0.5.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

24/10 2017

2.0.4

2.0.4.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent caching remember

24/08 2017

2.0.3

2.0.3.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

15/05 2017

2.0.2

2.0.2.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

30/04 2017

2.0.1

2.0.1.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

23/08 2016

1.0.x-dev

1.0.9999999.9999999-dev

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

23/08 2016

2.0.0

2.0.0.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

22/08 2016

1.0.5

1.0.5.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

26/05 2016

1.0.4

1.0.4.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

20/05 2016

1.0.3

1.0.3.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

06/04 2016

1.0.2

1.0.2.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

15/10 2015

1.0.1

1.0.1.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember

21/04 2015

1.0.0

1.0.0.0

Query caching for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dwight Watson

laravel eloquent caching remember