25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeyValue.php 1.3 KiB

3 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * 键-值对的 MQ
  4. *
  5. * - 队列存放于Key-Value的缓存中
  6. *
  7. * @author dogstar <chanzonghuang@gmail.com> 20160430
  8. */
  9. class Task_MQ_KeyValue implements Task_MQ {
  10. /**
  11. * @var PhalApi_Cache_Memcached/PhalApi_Cache_Memcache/PhalApi_Cache_File $kvCache 缓存实例
  12. */
  13. protected $kvCache;
  14. public function __construct(PhalApi_Cache $kvCache) {
  15. $this->kvCache = $kvCache;
  16. }
  17. public function add($service, $params = array()) {
  18. $list = $this->kvCache->get($service);
  19. if (empty($list)) {
  20. $list = array();
  21. }
  22. $list[] = $params;
  23. $this->kvCache->set($service, $list, $this->getExpireTime());
  24. $list = $this->kvCache->get($service);
  25. return true;
  26. }
  27. public function pop($service, $num = 1) {
  28. $rs = array();
  29. if ($num <= 0) {
  30. return $rs;
  31. }
  32. $list = $this->kvCache->get($service);
  33. if (empty($list)) {
  34. $list = array();
  35. }
  36. $rs = array_splice($list, 0, $num);
  37. $this->kvCache->set($service, $list, $this->getExpireTime());
  38. return $rs;
  39. }
  40. /**
  41. * 最大缓存时间,一年
  42. */
  43. protected function getExpireTime() {
  44. return 31536000;
  45. }
  46. }