Skip to content

A Laravel's package that automatically cache and invalidate Eloquent queries.

Notifications You must be signed in to change notification settings

telcolab/cacher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Cacher - Caching Eloquent Query

Latest Stable Version Latest Unstable Version Total Downloads License

Cacher is a package that automatically cache Eloquent query. With Cacher it's super easy to cache all eloquent query including relationships.

This package works by checking if the query hasn't been executed & cached before and if it doesn't, it will execute the SQL query generated by Eloquent and cache the results. If the query has been cached previously it'll just retrieve it from cache store instead of database.

Requirements

  • PHP >= 7
  • Laravel >= 5.1
  • Taggable Cacher Driver - Redis/Memcached

Installation

Install using Composer, just as you would anything else.

composer require telcolab/cacher

After the installation is completed just open up your Model and use TelcoLAB\Cacher\Traits\Cacheable trait. Let's say you have a model name App\Post.

<?php
namespace App;

use TelcoLAB\Cacher\Traits\Cacheable;
use Illuminate\Database\Eloquent\Model;
  
class Post extends Model
{
    use Cacheable;
    ...
}

Usage

After that you just call use Eloquent normally you use and Cacher will automatically cache subsequent query. For example in your controller,

<?php

namespace App\Http\Controllers;

use App\Post;

class PostController extends Controller
{
    public function index()
    {
         /**
         * This query will be cache to your cache driver.
         */
        $posts = Post::where('published', 1)->latest()->get();

        return view('news.index', [
            'news' => $posts,
        ]);
    }
    ...
}
Cache lifetime

By default Cacher will cache all the result 24 hours, you may alter this by changing the property (in minutes).

/**
* Cache will expire after x minutes.
* @var int
*/
protected $cacheExpireAfer = 1440;
Cache lifetime per query.

You also may change how long specific query will be cached.

/**
* The query will be cached for 360 minutes.
*/
$hotPosts = App\Post::where([
  ['views', '>=', '360']
])->remember(360)->get();

/**
* The query will not be cached at all.
*/
$hotPosts = App\Post::where([
  ['views', '>=', '360']
])->dontRemember()->get();

/**
* The query will cached forever until the Post model is updated or flushed manually.
*/
$hotPosts = App\Post::where([
  ['views', '>=', '360']
])->rememberForever()->get();
Cache flushing

You may flush the cache manully by calling the flush method on the model or let Cacher flush automatically when you insert/update/delete the model.

/**
* Flush cache for Post model manually.
*/
$flushCache = App\Post::flush();

/**
* Flush cache automatically by updating the model.
*/
$updatePostAndClearCache = App\Post::first()->update([
    'title' => 'Hello World!'
]);

Contributing

Thank you for considering contributing to the Cacher package! You may contribute by fork and make a pull request to this repository.

Security Vulnerabilities

If you discover a security vulnerability within Cacher, please send an e-mail to Haries at haries@telcolab.my . All security vulnerabilities will be promptly addressed.

License

The Laravel Standalone Package Creator is open-sourced software licensed under the MIT license.

Jobs

Our office is located in Seri Kembangan, Selangor. Our working time is flexible. Incase you're interested to join our team, you may shoot us an email at career@telcolab.my. Knowing Laravel and its best practices is a plus.

About

A Laravel's package that automatically cache and invalidate Eloquent queries.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages