bluesik/laravel-cacheable

Cacheable trait for Laravel models

dev-master 2018-07-29 19:17 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:44:07 UTC


README

Description

This trait allows you to easily cache your model's data through a variety of handful methods.

NOTE:

  • Cache is automatically cleared when a given model is saved/deleted

Installation

composer require bluesik/laravel-cacheable

Usage

Add at the top of your model file:

<?php

use Bluesik\LaravelCacheable\Cacheable;

Then within a class:

use Cacheable;

Available properties

protected $cacheExpiry;
  • Integer $cacheExpiry
    • A number of minutes till the cache expires.
    • Defaults to 24 hours
protected static $fullModelCaching
  • Boolean $fullModelCaching
    • Indicates whether models should be cached directly or converted to an array
    • Defaults to true
protected static $bustCacheOnSaved
  • Boolean $bustCacheOnSaved
    • Should cache be busted upon saving a model
    • Defaults to true
protected static $bustCacheOnDeleted
  • Boolean $bustCacheOnDeleted
    • Should cache be busted upon deleting a model
    • Defaults to true

Available methods

Get latest records

Model::getLatest($limit, $with, $orderBy);
  • Integer $limit

    • How many records to getAll
    • Defaults to: 3
  • String $orderBy

    • $orderBy - What column to use when sorting the data
    • Defaults to: created_at
  • Array $with

    • $with - List of relationships to eager load
    • Defaults to: An empty array

Returns a Collection or an array

Get a single record by its id

Model::getById($id);
  • Integer $id
    • Id of a record you want to get

Returns a Model, an array or null

Get records where

Model::getWhere($column, $value, $with);
  • String $column
    • Column name
    • Defaults to: id
  • Mixed $value
    • Value to look for
    • Defaults to: an empty string
  • Array $with
    • An array of relationships to eager load
    • Defaults to: an empty array

Returns a Collection or an array

Get all records

Model::getAll($with);
  • Array $with
    • An array of relationships to eager load
    • Defaults to: an empty array

Returns a Collection or an array

Clear cache for a given model

Model::clearCache(); 

Enable full model caching

Model::enableFullModelCaching(); 

Disable full model caching (use arrays instead)

Model::disableFullModelCaching(); 

License

MIT