Download the PHP package mahmoud217tr/cacheable without Composer
On this page you can find all versions of the php package mahmoud217tr/cacheable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mahmoud217tr/cacheable
More information about mahmoud217tr/cacheable
Files in mahmoud217tr/cacheable
Package cacheable
Short Description Laravel package for easing and enhance caching for models and cacheable classes
License MIT
Homepage https://github.com/mahmoud217tr/cacheable
Informations about the package cacheable
Cacheable for Laravel
Effortless and Enhanced Caching for Models and Classes
Laravel package that provides a streamlined and powerful solution for implementing caching within your application. This package simplifies the process of caching Eloquent models and other classes, ensuring improved performance and scalability for your Laravel application.
Table of Contents
- Installation
- Usage
- Cacheable Models
- Easy Caching for Model Records and Collections
- Auto-Caching Model Records
- Cached Route Model Binding
- Cacheable Interface & Trait
- Cacheable Facade
- Helper Functions
- Cacheable Models
- Publishing
- Testing
- Changelog
- Security Vulnerabilities
- Credits
- License
Installation
To install the package to your Laravel project via Composer:
And once the installation is complete, the package will be ready up and ready for usage.
Usage
There is a lot of features and usecases we will summerize them with the following:
1. Cacheable Models
You can make the model cacheable by making it implement CacheableModel
interface and use the CacheableModelTrait
trait as the following example:
Upon doing that your model will be using the
Cacheable Collection
as a collection which extends theEloquent Collection
, preserving the same logic and providing the model with caching features.
Making a model cacheable will provide you with caching features:
1. Easy Caching for Model Records and Collections
You can easily cache individual model records or collections of records using the cache
method as follows:
2. Auto-Caching Model Records
Models can be auto-cached, meaning all model records will be cached and synchronized upon creation, updating, or deletion.
CAUTION: This behavior may be unsuitable for large models or models with frequent changes. Use it wisely based on your use case.
To enable auto-caching, override the isAutoCacheSyncEnabled
method in your model to return true
:
Now, the auto-caching feature will be enabled, and all model records will be cached and updated automatically.
IMPORTANT: If you have modular Laravel application or you've changed your models default director, you'll need to do an extra step.
You can manage the cached models as follows:
You can also control the auto-cached collection by overriding the getDataForCaching
method as it's default behaviour is to return the all
method result:
And now only the published posts will cached by auto-caching feature and in order of the latest.
You can also control the TTL
of the cached data by overriding the getCacheTTL
method:
3. Cached Route Model Binding
You can also utilize the cached records to be used in route model binding by simply be using the CachedRouteBinding
trait:
Note: Modifying the auto-cached data by overriding the
getDataForCaching
method may result in404 Not Found
for non-cached model records. Solutions for this scenario will be discussed further.
You can change the cached data that is used for route model binding by overriding 2 methods:
- You should override the
shouldUseDifferentDataForBinding
method to returntrue
(which by default it returnsfalse
). - And you ou should also override the
getBindingData
method which represents the data collection to be cached (by default it returnsnull
).
And now the data used for route model binding will be cached with a different key and have different values and will be synconized automatically.
When the usage of a different data for binding is enabled, you can control the TTL
of the route binding cache by overriding the getBindingCacheTTL
method:
You can also allow the model to use an alternative route binding method by overriding the shouldUseAlternativeRouteBinding
method to return true
:
The default alternative route binding resolver is the resolveRouteBinding
method, which can be customized by overriding the alternativeRouteBinding
method:
2. Cacheable Interface & Trait
You can easily make any thing cacheable by making it implements the Cacheable
interface and use the CacahableTrait
trait:
And you can use the cache function to store the object as follows:
3. Cacheable Facade
You can also utilize the Cacheable
facade for some caching features, which has some drived features from the Cache
facade:
4. Helper Functions
The package will give you a new helper functions:
- The
is_cacheable_model
helper function which checks if a mode or an instance of it implements theCacheableModel
interface. - The
is_cacheable_class
helper function which checks if a class or an instance of it implements theCacheable
interface. - The
cached
helper function which retrieves cached data of a given key, but if not found, it will cache the given value by the given key with a given time to live if provided.
Publishing
To customize the package caching behavior you can publish the configuration file by running the following command in your console to copy the config file to your application's config directory:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Mahmoud Mahmoud
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of cacheable with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^9.0|^10.0|^11.0
illuminate/support Version ^9.0|^10.0|^11.0
illuminate/cache Version ^9.0|^10.0|^11.0