All Projects → dwightwatson → Rememberable

dwightwatson / Rememberable

Licence: mit
Query caching for Laravel

Projects that are alternatives of or similar to Rememberable

Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (-55.83%)
Mutual labels:  eloquent, caching, laravel
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (-60.94%)
Mutual labels:  eloquent, caching, laravel
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (-1.35%)
Mutual labels:  eloquent, laravel
Laravel
Laravel Model Generator
Stars: ✭ 715 (-25.52%)
Mutual labels:  eloquent, laravel
Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (-98.02%)
Mutual labels:  eloquent, laravel
Laravel Imageup
Auto Image & file upload, resize and crop for Laravel eloquent model using Intervention image
Stars: ✭ 646 (-32.71%)
Mutual labels:  eloquent, laravel
Laravel Friendships
This package gives Eloquent models the ability to manage their friendships.
Stars: ✭ 651 (-32.19%)
Mutual labels:  eloquent, laravel
Laravel Sluggable
An opinionated package to create slugs for Eloquent models
Stars: ✭ 831 (-13.44%)
Mutual labels:  eloquent, laravel
Laravel Schemaless Attributes
Add schemaless attributes to Eloquent models
Stars: ✭ 595 (-38.02%)
Mutual labels:  eloquent, laravel
Eloquent Sortable
Sortable behaviour for Eloquent models
Stars: ✭ 914 (-4.79%)
Mutual labels:  eloquent, laravel
Validating
Automatically validating Eloquent models for Laravel
Stars: ✭ 906 (-5.62%)
Mutual labels:  eloquent, laravel
Eloquent Filter
This simple package helps you filter Eloquent data using query filters.
Stars: ✭ 24 (-97.5%)
Mutual labels:  eloquent, laravel
Laravel Mongodb
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
Stars: ✭ 5,860 (+510.42%)
Mutual labels:  eloquent, laravel
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (-35%)
Mutual labels:  eloquent, laravel
Laravel Repositories
[ABANDONED] 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.
Stars: ✭ 664 (-30.83%)
Mutual labels:  eloquent, laravel
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (-37.92%)
Mutual labels:  eloquent, laravel
Laravel Love
Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
Stars: ✭ 822 (-14.37%)
Mutual labels:  eloquent, laravel
Improved Polymorphic Eloquent Builder
🔨 Improved Polymorphic Eloquent Builder
Stars: ✭ 12 (-98.75%)
Mutual labels:  eloquent, laravel
Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (-40.42%)
Mutual labels:  eloquent, laravel
Laracsv
A Laravel package to easily generate CSV files from Eloquent model
Stars: ✭ 583 (-39.27%)
Mutual labels:  eloquent, laravel

Rememberable, Laravel 5 query cache

Total Downloads Latest Stable Version Latest Unstable Version License

Rememberable is an Eloquent trait for Laravel that adds remember() query methods. This makes it super easy to cache your query results for an adjustable amount of time.

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

// You can also pass the number of seconds if you like (before Laravel 5.8 this will be interpreted as minutes).
User::first()->remember(60 * 60 * 24)->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.

Installation

Install using Composer, just as you would anything else.

composer require watson/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.

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

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

Usage

Using the remember method is super simple. Just pass the number of seconds 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.

// Remember the number of users for an hour.
$users = User::remember(60 * 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.

// Remember the number of users for an hour and tag it with 'user_queries'
User::remember(60 * 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.

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

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

Cache driver

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

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

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

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.

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.

$users = User::where("id", ">", "1")
    ->with(['posts' => function ($q) { $q->remember(60 * 60); }])
    ->remember(60 * 60)
    ->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 seconds 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.

Cache flushing

Based on the architecture of the package it's not possible to delete the cache for a single query. But if you tagged any queries using cache tags, you are able to flush the cache for the tag:

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:

User::flushCache();

Skipping cache

If you need to disable cache for a particular query, you can use the dontRemember method:

User::latest()->dontRemember()->get();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].