PHP code example of davidmpeace / squirrel

1. Go to this page and download the library: Download davidmpeace/squirrel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

davidmpeace / squirrel example snippets



namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent\Cache\Squirrel;

class MyAppSuperModel extends Model
{
    use Squirrel;
}


namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent\Cache\Squirrel;

class User extends Model
{
    use Squirrel;
    
    /**
     * Implement this method, to establish additional unique keys on your table.  Doing this gives Squirrel more power
     * in establishing more cacheable queries.  Return an array of string column names, or nested arrays for 
     * compound unique keys.
     */
    public function getUniqueKeys()
    {
        $primaryKey = $this->getKeyName();
        return [$primaryKey, 'uuid', ['account_id', 'email']];
    }
    
    /**
     * Implement this method to cacheing on or off for this model specifically.  Returning false on this method
     * does not affect other models also using Squirrel.
     */
    public function isCacheActive()
    {
        return true; 
    }
    
    /**
     * Implement this method to change the expiration minutes timeout when cacheing this model.
     */
    public function cacheExpirationMinutes()
    {
        return (60 * 24); 
    }
}

use Eloquent\Cache\SquirrelCache;

SquirrelCache::setGlobalCacheActive(false);      // Turn Squirrel ON or OFF globally
SquirrelCache::isGlobalCacheActive();            // Returns the config value if Squirrel is active or not globally.
SquirrelCache::setCacheKeyPrefix("Squirrel::");  // Prefix used for all stored Cache Keys
SquirrelCache::getCacheKeyPrefix();              // Returns the cache key prefix
SquirrelCache::setLoggingActive(true);           // Turns on internally logging for cache hits\misses, and DB queries.
SquirrelCache::isLoggingActive();                // Returns true if logging is enabled.
SquirrelCache::getLogs();                        // Returns the array of logs that were generated so far from Squirrel.
SquirrelCache::getLogSummary();                  // Returns a simple log summary of hits, misses, and DB queries.
SquirrelCache::flushAll();                       // Flushes all cached models from Squirrel

$model->remember();                  // Will store the object in Cache
$model->forget();                    // Will remove the object from Cache
$model->isCached();                  // Returns true if the current object is stored in cache.
$model->isCacheing();                // Returns true if Cacheing is on for this model
$model->cachedData();                // Returns the data that is stored in cache for this object.
$model->cacheKeys();                 // Will return an array of all the Cache keys used to store the object
$model->primaryCacheKey();           // Will return the primary cache key for the object.
$model->cacheExpirationMinutes();    // Returns the number of minutes cache records stay available.
$model->countCachedWithSameClass();  // Returns the number of cached models with the same class.
$model->forgetAllWithSameClass();    // Forgets all cached models with the same class.

// Simple ID Queries
Model::find(1);
Model::whereId(1)->get();

// This works because we return a compound unique key on the model
Model::whereAccountId(12)->whereEmail('[email protected]')->get();  

// Also works, because it will try to find all the individual records
Model::whereIn('id', [1,2,3,4,5])->get(); 

// Works if 'uuid' is returned as a unique key on the model
Model::whereUuid('12345-12346-123456-12356')->first(); 

// THESE QUERIES DO NOT WORK WITH CACHEING, AND WILL QUERY THE DB

// WON'T CACHE because the "=" equals sign, and "in", are the only supported operators.
Model::where('id', '>', 50)->get();

// WON'T CACHE because the field is not defined as a unique key on the model
Model::wherePlanId(23)->first();