Created
November 20, 2017 16:27
-
-
Save benbristow/4753a49d4226e4a18858e8a318dfda5a to your computer and use it in GitHub Desktop.
Implementation of 'Rails.cache.fetch' using Stash PHP Library
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 | |
use Stash\Driver\Redis; | |
use Stash\Pool; | |
class Cache { | |
public static function fetch(String $namespace, String $key, $expiresAfter, $callback) { | |
$pool = new Pool(new Redis()); | |
$item = $pool->getItem($namespace . '/' . hash('sha256', $key)); | |
$data = $item->get(); | |
if($item->isMiss()) { | |
$item->lock(); | |
$data = $callback(); | |
$item->set($data); | |
$item->expiresAt($expiresAfter); | |
$pool->save($item); | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment