| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace tw\redis\traits;
- use tw\redis\TwRedis;
- /**
- * 支持 redis hash key 操作的函数, 必须 use 到 Base 的子类中, 因为内部调用了 Base 的虚函数
- *
- */
- trait HashTrait
- {
- /**
- * 获取 hash attr
- *
- * @param int|string $word
- * @param string $attr
- * @return string
- */
- public function get($word, string $attr) : string
- {
- return TwRedis::hGet($this->key($word), $attr);
- }
- public function gets($word, array $attrs) : array
- {
- return TwRedis::hMGet($this->key($word), $attrs);
- }
- /**
- * set hash attr
- */
- public function set($word, string $attr, $value) : bool
- {
- return TwRedis::hSet($this->key($word), $attr, $value);
- }
- /**
- * 设置多个 attr
- *
- * @param array $attrs: ['attr1'=>'value1', 'attr2' => 'value2']
- * @return bool
- */
- public function sets($word, array $attrs) : bool
- {
- return TwRedis::hMSet($this->key($word), $attrs);
- }
- /**
- * setup hash value if the attr not exists
- */
- public function hsetnx($word, $attr, $val) : bool
- {
- return TwRedis::hSetNx($this->key($word), $attr, $val);
- }
-
- /**
- * hash 的 attr 是否存在
- */
- public function hexists($word, string $attr) : bool
- {
- return TwRedis::hExists($this->key($word), $attr);
- }
- public function hincr_by($word, string $attr, int $step) : int
- {
- return TwRedis::hIncrBy($this->key($word), $attr, $step);
- }
- public function hincr_by_float($word, string $attr, float $f) : float
- {
- return TwRedis::hIncrByFloat($this->key($word), $attr, $f);
- }
- public function hkeys($word) : array
- {
- return TwRedis::hKeys($this->key($word));
- }
- public function hvals($word) : array
- {
- return TwRedis::hVals($this->key($word));
- }
- public function dels($word, $attrs) : int
- {
- if (is_string($attrs)) {
- return TwRedis::hDel($this->key($word), $attrs);
- } else if (is_array($attrs)) {
- return TwRedis::hDel($this->key($word), ...$attrs);
- }
- }
- public function getAll($word) : array
- {
- return TwRedis::hGetAll($this->key($word));
- }
- }
|