Download the PHP package davidmpeace/squirrel without Composer
On this page you can find all versions of the php package davidmpeace/squirrel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package squirrel
Squirrel
Squirrel is an Eloquent cacheing solution that handles the complexities of 'remembers' and 'forgets' for you. This package is intended to be used with Laravel. Squirrel automatically caches and retrieves models when querying records using Eloquent ORM. When Squirrel is used, you can expect to see orders of magnitude fewer queries to your database, with the confidence you will never be retrieving stale data from Cache.
License
Squirrel is open-sourced software licensed under the MIT license
Installation
To get started with Squirrel, add to your composer.json
file as a dependency:
composer require davidmpeace/squirrel
Basic Usage
To use the Squirrel library, you simply need to use the Squirrel trait for any model you want to implement cacheing for. Typically, you would want to implement the trait in your super-class so that all your sub-classes will automatically inherit the functionality.
That's it! You will now automatically inherit all the magic of Squirrel.
Default Behavior
Without any customization, Squirrel behaves in the following default manner:
1) Each model will be cached using the key returned from the $model->getKeyName() method, as defined in the base Eloquent Model class. 2) The Cache is "active" for all models that use the Squirrel trait. 3) Each model is cached for 24 hours before expiring.
Configuration
Sometimes you'll need custom configuration on a per-model basis. Here are some examples of methods you can implement to override default behavior.
Global Configuration & Methods
Use the following global configuration methods to update settings for all models that implement Squirrel cache.
Squirrel Model Instance Methods
These methods are available to any Object using the Squirrel Trait
Queries Supported
Squirrel is meant to support multiple unique keys, as well as compound unique keys, so any query that is attempting to bring back a single row based on a unique key will work. However, you may also perform an "In" query, as long as that's the only part of the query. See below:
Under the Hood
The way Squirrel works is by extending the default \Illuminate\Database\Query\Builder
Class, which is responsible for executing queries for your models.
By default, Models inherit a method called newBaseQueryBuilder()
which is responsible for returning the Builder object. We overload this method so we can return the SquirrelQueryBuilder
object instead.
The SquirrelQueryBuilder->get()
method does the actual querying. However, before we query the data, we first check to see if our model is cached via any unique keys, if so, we return it, otherwise, we do the query. Finally, after the query is executed, we save the retrieved data in cache so it doesn't get hit again until the data expires.
Cache keys are stored in the following format:
SquirrelCache::$cacheKeyPrefix . "::" . get_class($model) . "::" . serialize(uniquekey);
Example for User model where id=1:
Squirrel::App\\User::a:1:{s:2:"id";s:1:"1";}
Example for User model where account_id=27 and [email protected]:
Squirrel::App\\User::a:2:{s:10:"account_id";s:2:"27";s:5:"email";s:11:"[email protected]";}