StringTrait.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace tw\redis\traits;
  3. use tw\redis\TwRedis;
  4. trait StringTrait
  5. {
  6. public function get($word)
  7. {
  8. return TwRedis::get($this->key($word));
  9. }
  10. public function set($word, $val)
  11. {
  12. return TwRedis::set($this->key($word), $val);
  13. }
  14. /**
  15. * 设置 string 同时设置 TTL
  16. *
  17. * @param int|string $word: 关键字
  18. * @param int|float|string $val: 值
  19. * @param int $expire: TTL in millisecond 1s = 1000 millisecond
  20. */
  21. public function set_with_expire($word, $val, $expire)
  22. {
  23. return TwRedis::setEx($this->key($word), $expire, $val);
  24. }
  25. /**
  26. * key 不存在时设置
  27. */
  28. public function setnx($word, $val)
  29. {
  30. return TwRedis::setNx($this->key($word), $val);
  31. }
  32. public function incr_by($word, $step)
  33. {
  34. return TwRedis::incrBy($this->key($word), $step);
  35. }
  36. public function incr_by_float($word, $f)
  37. {
  38. return TwRedis::incrByFloat($this->key($word), $f);
  39. }
  40. public function decr_by($word, $step)
  41. {
  42. return TwRedis::decrBy($this->key($word), $step);
  43. }
  44. }