Skip to content

Instantly share code, notes, and snippets.

@ferdousulhaque
Created February 16, 2025 02:13
Show Gist options
  • Save ferdousulhaque/02770c5303e093b7c57d7346b53a1240 to your computer and use it in GitHub Desktop.
Save ferdousulhaque/02770c5303e093b7c57d7346b53a1240 to your computer and use it in GitHub Desktop.
<?php
namespace Src\Platform\CommonLibrary\PHPRedis;
use Exception;
class PHPRedisCache
{
/**
* A string that should be prepended to keys
*
* @var string
*/
protected $prefix;
/**
* Redis connector
*
* @var object
*/
private $redisConnector;
/**
* Constructor method
*
* @param array $config
*/
public function __construct(array $config)
{
$this->redisConnector = new RedisConnector($config);
$this->setPrefix($config['prefix']);
}
/**
* Retrieve an item from the redis
*
* @param $key
* @return mixed|null
* @throws \Exception
*/
public function get($key)
{
$connection = $this->redisConnector->getConnection('slave');
$value = $connection->get($this->prefix . $key);
return !is_null($value) ? $value : null;
}
/**
* Run redis pipeline
*
* @param array $operations
* @return void
*/
public function pipeline(array $operations)
{
$connection = $this->redisConnector->getConnection('master');
$pipeline = $connection->multi();
foreach ($operations as $operation) {
call_user_func_array([$pipeline, $operation['method']], $operation['args']);
}
return $pipeline->exec();
}
/**
* Run redis pipeline get operations
*
* @param array $operations
* @return array
*/
public function pipelineGetOperations(array $operations)
{
$connection = $this->redisConnector->getConnection('slave');
$pipeline = $connection->multi($connection::PIPELINE);
foreach ($operations as $item) {
if(isset($item[METHOD_KEY]) && isset($item['key'])){
$pipeline->{$item[METHOD_KEY]}($this->prefix . $item['key']);
}
}
return $pipeline->exec();
}
/**
* Run redis pipeline set operations
*
* @param array $operations
* @return array
*/
public function pipelineSetOperations(array $operations)
{
$connection = $this->redisConnector->getConnection('master');
$pipeline = $connection->multi($connection::PIPELINE);
foreach($operations as $item) {
// Set the prefix with Key
$key = $this->prefix . $item['key'];
if(isset($item['method']) && $item['method'] === 'expire'){
$pipeline->expire($key, $item['ttl']);
} else if (isset($item['ttl']) || isset($item['value'])) {
$pipeline->{$item[METHOD_KEY]}($key, $item['value']);
$pipeline->expire($key, $item['ttl']);
} else {
isset($item['hash_keys']) ? $pipeline->{$item[METHOD_KEY]}($key, $item['hash_keys']) : $pipeline->{$item[METHOD_KEY]}($key);
}
}
return $pipeline->exec();
}
/**
* Retrieve multiple items from the redis
*
* @param array $keys
* @return array
* @throws \Exception
*/
public function many(array $keys)
{
$connection = $this->redisConnector->getConnection('slave');
$results = [];
$values = $connection->mget(array_map(function ($key) {
return $this->prefix . $key;
}, $keys));
foreach ($values as $index => $value) {
$results[$keys[$index]] = !is_null($value) ? $value : null;
}
return $results;
}
/**
* Store an item in the redis for given number of seconds
*
* @param $key
* @param $value
* @return bool
* @throws \Exception
*/
public function put($key, $value)
{
$connection = $this->redisConnector->getConnection('master');
return (bool)$connection->set(
$this->prefix . $key,
$value
);
}
/**
* Store an item in the redis for given number of seconds
*
* @param $key
* @param $value
* @return bool
* @throws \Exception
*/
public function set($key, $value)
{
return $this->put($key, $value);
}
/**
* Store multiple items in the redis for a given number of seconds.
*
* @param array $values
* @return bool
* @throws \Exception
*/
public function putMany(array $values)
{
$connection = $this->redisConnector->getConnection('master');
$connection->multi();
$manyResult = null;
foreach ($values as $key => $value) {
$result = $this->put($key, $value);
$manyResult = is_null($manyResult) ? $result : $result && $manyResult;
}
$connection->exec();
return $manyResult ?: false;
}
/**
* Setting a value in redis if there is no value found store the result
*
* @param $key
* @param null $seconds
* @param callable $callback
* @return mixed|null
* @throws \Exception
*/
public function remember($key, $seconds, callable $callback)
{
if (($value = $this->get($key)) !== null && ($value = $this->get($key)) !== false) {
return $value;
}
$this->put($key, $value = $callback());
$this->expire($key, $seconds);
return $value;
}
/**
* Increment the value of an item in the redis.
*
* @param string $key
* @param mixed $value
* @return int
*/
public function increment($key, $value = 1)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->incrby($this->prefix . $key, $value);
}
/**
* Decrement the value of an item in redis.
*
* @param string $key
* @param mixed $value
* @return int
* @throws \Exception
*/
public function decrement($key, $value = 1)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->decrby($this->prefix . $key, $value);
}
/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
* @throws \Exception
*/
public function forget($key)
{
$connection = $this->redisConnector->getConnection('master');
return (bool)$connection->del($this->prefix . $key);
}
/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
* @throws \Exception
*/
public function del($key)
{
return $this->forget($key);
}
/**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Determine if a key exists
*
* @param string $key
* @return bool|int
* @throws \Exception
*/
public function exists(string $key)
{
$connection = $this->redisConnector->getConnection('slave');
return $connection->exists($key);
}
/**
* Set the cache key prefix.
*
* @param string $prefix
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Determine if redis is connected.
*
* @param string $connectionType
* @return boolean
*/
public function isConnected($connectionType = 'master')
{
$connection = $this->redisConnector->getConnection($connectionType);
return $connection && $connection->isConnected();
}
public function expire($key, $ttl): bool
{
$connection = $this->redisConnector->getConnection('master');
return (bool)$connection->expire($this->prefix . $key, $ttl);
}
/**
* Get the hash.
*
* @param string $key
* @return array
*/
public function hgetall(string $key)
{
$connection = $this->redisConnector->getConnection('slave');
return $connection->hGetAll($this->prefix . $key);
}
/**
* Set a hash with key/value pair.
*
* @param string $key
* @param array $data
* @return boolean
*/
public function hmset(string $key, array $data)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->hMSet($this->prefix . $key, $data);
}
/**
* Add values to head of a list.
*
* @param string $key
* @param array $data
* @return boolean
*/
public function lpush(string $key, array $data)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->lPush($this->prefix . $key, $data);
}
/**
* Get and remove first element of a list.
*
* @param string $key
* @return mixed
*/
public function lpop(string $key)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->lPop($this->prefix . $key);
}
/**
* Get an element from specific range.
*
* @param string $key
* @param integer $from
* @param integer $to
* @return mixed
*/
public function lrange(string $key, $from, $to)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->lRange($this->prefix . $key, $from, $to);
}
/**
* Add values to tail of a list.
*
* @param string $key
* @param array $data
* @return boolean
*/
public function rpush(string $key, $data): bool
{
$connection = $this->redisConnector->getConnection('master');
return $connection->rPush($this->prefix . $key, $data);
}
/**
* Get and remove first element of a list.
*
* @param string $key
* @return mixed
*/
public function rpop(string $key)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->rPop($this->prefix . $key);
}
/**
* @param $key
* @param $start
* @param $stop
* @return mixed
*/
public function ltrim($key, $start, $stop)
{
$connection = $this->redisConnector->getConnection('master');
return $connection->ltrim($this->prefix . $key, $start, $stop);
}
/**
* @param $key
* @param $value
* @return bool
*/
public function srem($key, $value)
{
$connection = $this->redisConnector->getConnection('master');
return (bool)$connection->sRem(
$this->prefix . $key,
$value
);
}
/**
* @param $key
* @param $value
* @return bool
*/
public function sadd($key, $value)
{
$connection = $this->redisConnector->getConnection('master');
return (bool)$connection->sAdd(
$this->prefix . $key,
$value
);
}
/**
* @param $key
* @param $value
* @return bool
*/
public function sismember($key, $value)
{
$connection = $this->redisConnector->getConnection('master');
return (bool)$connection->sIsMember(
$this->prefix . $key,
$value
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment