Skip to content

Instantly share code, notes, and snippets.

@Meroje
Created November 17, 2012 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meroje/4096538 to your computer and use it in GitHub Desktop.
Save Meroje/4096538 to your computer and use it in GitHub Desktop.
CacheableTrait (from ShawnMcCool)
<?php
trait CacheableTrait
{
protected $cache = array();
public function request_cache($key, \Closure $closure)
{
// return the cache if it exists
if(!isset($this->cache[$key]))
{
$this->cache[$key] = $closure();
}
return $this->cache[$key];
}
public function session_cache($key, \Closure $closure)
{
$session_variable = 'session_cache_'.$key;
// return the cache if it exists
if(!Session::has($session_variable))
{
Session::put($session_variable, $closure();
}
return Session::get($session_variable);
}
public function cache($key, \Closure $closure)
{
$cache_variable = 'session_cache_'.$key;
// return the cache if it exists
if(!Cache::has($cache_variable))
{
Cache::put($cache_variable, $closure();
}
return Cache::get($cache_variable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment