-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from rasteiner/main
- Loading branch information
Showing
9 changed files
with
107 additions
and
23 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace tobimori; | ||
|
||
use DateInterval; | ||
use Kirby\Cache\Cache; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
class KirbyCacheAdapter implements CacheInterface { | ||
protected Cache $cache; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->cache = kirby()->cache($name); | ||
} | ||
|
||
public function get(string $key, mixed $default = null): mixed { | ||
return $this->cache->get($key) ?? $default; | ||
} | ||
|
||
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { | ||
if($ttl instanceof DateInterval) { | ||
$ttl = ceil($ttl->s / 60); | ||
} elseif($ttl === null) { | ||
$ttl = 0; | ||
} elseif($ttl < 0) { | ||
$ttl = 0; | ||
} elseif(is_int($ttl)) { | ||
$ttl = ceil($ttl / 60); | ||
} | ||
|
||
return $this->cache->set($key, $value, $ttl); | ||
} | ||
|
||
public function delete(string $key): bool { | ||
return $this->cache->remove($key); | ||
} | ||
|
||
public function clear(): bool { | ||
return $this->cache->flush(); | ||
} | ||
|
||
public function getMultiple(iterable $keys, mixed $default = null): iterable { | ||
$values = []; | ||
foreach($keys as $key) { | ||
$values[$key] = $this->cache->get($key) ?? $default; | ||
} | ||
return $values; | ||
} | ||
|
||
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { | ||
foreach($values as $key => $value) { | ||
$this->set($key, $value, $ttl); | ||
} | ||
return true; | ||
} | ||
|
||
public function deleteMultiple(iterable $keys): bool { | ||
foreach($keys as $key) { | ||
$this->delete($key); | ||
} | ||
return true; | ||
} | ||
|
||
public function has(string $key): bool { | ||
return $this->cache->exists($key); | ||
} | ||
} |
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
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
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
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
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
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
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