Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24928965
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/Engines/CacheRedis.php b/src/Engines/CacheRedis.php
index 7101285..b409f69 100644
--- a/src/Engines/CacheRedis.php
+++ b/src/Engines/CacheRedis.php
@@ -1,163 +1,164 @@
<?php
namespace Keruald\Cache\Engines;
use Keruald\Cache\Cache;
use Keruald\Cache\Exceptions\CacheException;
use DateInterval;
use DateTimeImmutable;
use Redis;
use RedisException;
class CacheRedis extends Cache {
///
/// Constants - default value
///
const DEFAULT_SERVER = "localhost";
const DEFAULT_PORT = 6379;
///
/// Properties
///
private Redis $redis;
///
/// Constructors
///
public function __construct (Redis $client) {
$this->redis = $client;
}
public static function load (array $config) : Cache {
//Checks extension is okay
if (!extension_loaded("redis")) {
throw new CacheException("Can't initialize Redis cache engine: PHP extension redis not loaded.");
}
$client = new Redis();
try {
$client->connect(
$config["server"] ?? self::DEFAULT_SERVER,
$config["port"] ?? self::DEFAULT_PORT,
);
if (array_key_exists("database", $config)) {
$client->select($config["database"]);
}
} catch (RedisException $ex) {
throw new CacheException(
"Can't initialize Redis cache engine: Can't connect to Redis server",
0,
$ex
);
}
return new self($client);
}
///
/// Cache operations
///
public function get (string $key, mixed $default = null) : mixed {
try {
$value = $this->redis->get($key);
} catch (RedisException $ex) {
throw new CacheException("Can't get item", 0, $ex);
}
return match ($value) {
false => $default,
default => unserialize($value),
};
}
function set (
string $key,
mixed $value,
null|int|DateInterval $ttl = null
) : bool {
try {
+ $value = serialize($value);
if ($ttl === null) {
- $this->redis->set($key, serialize($value));
+ $this->redis->set($key, $value);
} else {
$this->redis->setex($key, self::parse_interval($ttl), $value);
}
} catch (RedisException $ex) {
throw new CacheException("Can't set item", 0, $ex);
}
return true;
}
public function delete (string $key) : bool {
try {
$countDeleted = $this->redis->del($key);
} catch (RedisException $e) {
throw new CacheException("Can't delete item", 0, $ex);
}
return $countDeleted === 1;
}
public function clear () : bool {
try {
$this->redis->flushDB();
} catch (RedisException $e) {
throw new CacheException("Can't clear cache", 0, $ex);
}
return true;
}
public function has (string $key) : bool {
try {
$count = $this->redis->exists($key);
} catch (RedisException $e) {
throw new CacheException("Can't check item", 0, $ex);
}
return $count === 1;
}
///
/// Overrides for multiple operations
///
public function deleteMultiple (iterable $keys) : bool {
$keys = [...$keys];
$expectedCount = count($keys);
try {
$countDeleted = $this->redis->del($keys);
} catch (RedisException $e) {
throw new CacheException("Can't delete items", 0, $ex);
}
return $countDeleted === $expectedCount;
}
///
/// Helper methods
///
private static function parse_interval (DateInterval|int $ttl) : int {
if (is_integer($ttl)) {
return $ttl;
}
$start = new DateTimeImmutable;
$end = $start->add($ttl);
return $end->getTimestamp() - $start->getTimestamp();
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Mar 21, 07:04 (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3546604
Default Alt Text
(4 KB)
Attached To
Mode
rKC Keruald Cache
Attached
Detach File
Event Timeline
Log In to Comment