Created
November 17, 2012 14:51
-
-
Save Meroje/4096538 to your computer and use it in GitHub Desktop.
CacheableTrait (from ShawnMcCool)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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