Created
August 31, 2017 06:28
-
-
Save leo108/bd7559654c52000cc9774a80b072c629 to your computer and use it in GitHub Desktop.
Psr\SimpleCache\CacheInterface implementation for Laravel
This file contains 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 | |
/** | |
* Created by PhpStorm. | |
* User: leo108 | |
* Date: 2017/8/14 | |
* Time: 15:44 | |
*/ | |
namespace App\Extensions\Cache; | |
use Cache; | |
use Psr\SimpleCache\CacheInterface; | |
class SimpleCacheBridge implements CacheInterface | |
{ | |
public function get($key, $default = null) | |
{ | |
return Cache::get($key, $default); | |
} | |
public function set($key, $value, $ttl = null) | |
{ | |
Cache::put($key, $value, $this->ttl2minutes($ttl)); | |
return true; | |
} | |
public function delete($key) | |
{ | |
return Cache::forget($key); | |
} | |
public function clear() | |
{ | |
return Cache::flush(); | |
} | |
public function getMultiple($keys, $default = null) | |
{ | |
return Cache::many($keys); | |
} | |
public function setMultiple($values, $ttl = null) | |
{ | |
Cache::putMany($values, $this->ttl2minutes($ttl)); | |
return true; | |
} | |
public function deleteMultiple($keys) | |
{ | |
foreach ($keys as $key) { | |
$this->delete($key); | |
} | |
} | |
public function has($key) | |
{ | |
return Cache::has($key); | |
} | |
protected function ttl2minutes($ttl) | |
{ | |
if (is_null($ttl)) { | |
return null; | |
} | |
if ($ttl instanceof \DateInterval) { | |
return $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60; | |
} | |
return $ttl / 60; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this - I have updated it to support PHP 8.2 - Tested on Laravel 10
https://gist.github.com/mrl22/e94ec1ff4f6bc507128bd8d5e7bbaf34