Download the PHP package webdeva/query-cache without Composer
On this page you can find all versions of the php package webdeva/query-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package query-cache
Query Cache Package
Description
webdeva/query-cache
is a Laravel package that provides an easy way to cache Eloquent query results automatically. This package leverages Laravel's cache system to enhance performance by reducing database queries.
Installation
Step 1: Install via Composer
Run the following command to include the package in your Laravel project:
Step 2: Add the Trait to Your Models
To enable query caching for a specific model, include the QueryCacheable
trait in your model:
Usage
Caching Queries
Once the trait is applied to a model, you can cache query results by calling the cacheQuery
method on the query builder:
The query result will be cached for 1 hour (3600 seconds) by default.
Flushing Cache
You can manually clear the cache for a specific query or model by calling the flushQueryCache
method:
How It Works
- The
QueryCacheable
trait adds a global scope to cache query results. - The cache key is generated using the query SQL and its bindings, ensuring unique caching for every query.
- The default cache duration is set to 3600 seconds (1 hour). You can modify this by overriding the
$cacheFor
property in your model:
Requirements
- PHP >= 7.4
- Laravel >= 10.x
Advanced Example
Query with Filters
The caching mechanism works seamlessly with filtered queries:
Dynamic Cache Tags
You can assign specific tags for cache management:
Limitations
- The package currently uses Laravel's default cache store. Ensure that your caching system (e.g., Redis, Memcached) is configured correctly.
- Be cautious with large datasets, as caching them can increase memory usage.
License
This package is open-source software licensed under the MIT license.
Authors
Developed and maintained by Mohammad Khan ([email protected]).