Skip to content

Instantly share code, notes, and snippets.

@hazeim254
Created February 11, 2016 07:03
Show Gist options
  • Save hazeim254/658e07cef757c323346c to your computer and use it in GitHub Desktop.
Save hazeim254/658e07cef757c323346c to your computer and use it in GitHub Desktop.
Russian Caching inspired by Jeffry Way's class with an addition for local environmen
<?php
namespace App;
use Cache;
class RussianCaching
{
/**
* A list of model cache keys.
*
* @param array $keys
*/
protected static $keys = [];
/**
* Setup our caching mechanism.
*
* @param mixed $model
*/
public static function setUp($model)
{
if (app()->environment() === 'local') {
return '';
}
static::$keys[] = $key = $model->getCacheKey();
ob_start();
return Cache::tags('views')->has($key);
}
/**
* Teardown our cache setup.
*/
public static function tearDown()
{
if (app()->environment() === 'local') {
return '';
}
$key = array_pop(static::$keys);
$html = ob_get_clean();
return Cache::tags('views')
->rememberForever($key, function () use ($html) {
return $html;
});
}
}
@jaouadk
Copy link

jaouadk commented Feb 11, 2016

👍 Nice.

public static function setUp($model)
{
   if (static::shouldCache()) { 
        static::$keys[] = $key = $model->getCacheKey();
        ob_start();
        return Cache::tags('views')->has($key);
    }
}

public static function shouldCache()
{
   return ! app()->environment('local');
}

Same for teadDown method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment