hyperf-chat/app/Cache/Repository/HashGroupRedis.php

87 lines
1.9 KiB
PHP
Raw Normal View History

2021-05-21 22:56:42 +08:00
<?php
2021-08-28 16:11:38 +08:00
declare(strict_types=1);
2021-05-21 22:56:42 +08:00
namespace App\Cache\Repository;
class HashGroupRedis extends AbstractRedis
{
2021-05-22 14:47:46 +08:00
protected $prefix = 'rds-hash';
2021-05-21 22:56:42 +08:00
protected $name = 'default';
/**
* @param string $name
* @param string $key
* @param $value
* @return bool|int
*/
public function add(string $name, string $key, $value)
{
return $this->redis()->hSet($this->getCacheKey($name), $key, $value);
}
/**
2021-05-22 14:47:46 +08:00
* @param string $name
* @param string|int $key
2021-05-21 22:56:42 +08:00
* @return false|string
*/
2021-05-22 14:47:46 +08:00
public function get(string $name, string $key)
2021-05-21 22:56:42 +08:00
{
return $this->redis()->hGet($this->getCacheKey($name), $key);
}
/**
* @param string $name
* @return array
*/
2021-08-28 16:11:38 +08:00
public function getAll(string $name): array
2021-05-21 22:56:42 +08:00
{
return $this->redis()->hGetAll($this->getCacheKey($name));
}
/**
* @param string $name
* @param string $key
* @return bool|int
*/
public function rem(string $name, string $key)
{
return $this->redis()->hDel($this->getCacheKey($name), $key);
}
/**
* @param string $name
* @param string $key
* @param int $value
* @return int
*/
2021-08-28 16:11:38 +08:00
public function incr(string $name, string $key, int $value = 1): int
2021-05-21 22:56:42 +08:00
{
return $this->redis()->hIncrBy($this->getCacheKey($name), $key, $value);
}
/**
* @param string $name
* @param string $key
* @return bool
*/
2021-08-28 16:11:38 +08:00
public function isMember(string $name, string $key): bool
2021-05-21 22:56:42 +08:00
{
return $this->redis()->hExists($this->getCacheKey($name), $key);
}
/**
* @param string $name
* @return false|int
*/
public function count(string $name)
{
return $this->redis()->hLen($this->getCacheKey($name));
}
2021-05-22 14:47:46 +08:00
2021-08-28 16:11:38 +08:00
public function delete(string $name): int
2021-05-22 14:47:46 +08:00
{
return $this->redis()->del($this->getCacheKey($name));
}
2021-05-21 22:56:42 +08:00
}