Skip to content

Instantly share code, notes, and snippets.

@leo108
Created August 31, 2017 06:28
Show Gist options
  • Save leo108/bd7559654c52000cc9774a80b072c629 to your computer and use it in GitHub Desktop.
Save leo108/bd7559654c52000cc9774a80b072c629 to your computer and use it in GitHub Desktop.
Psr\SimpleCache\CacheInterface implementation for Laravel
<?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;
}
}
@mrl22
Copy link

mrl22 commented Jan 22, 2024

Thank you for this - I have updated it to support PHP 8.2 - Tested on Laravel 10
https://gist.github.com/mrl22/e94ec1ff4f6bc507128bd8d5e7bbaf34

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