memory_driver_yac.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: memory_driver_yac.php 27635 2017-02-02 17:02:46Z NaiXiaoxIN $
  7. */
  8. if (!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class memory_driver_yac {
  12. public $cacheName = 'Yac';
  13. private $object = null;
  14. public $enable;
  15. public function env() {
  16. return extension_loaded('Yac');
  17. }
  18. public function init($config) {
  19. $this->enable = $this->env();
  20. if ($this->enable) {
  21. $this->object = new yac();
  22. }
  23. }
  24. public function get($key) {
  25. return $this->object->get($key);
  26. }
  27. public function getMulti($keys) {
  28. $result = $this->object->get($keys);
  29. foreach ($result as $key => $value) {
  30. if ($value === false) {
  31. unset($result[$key]);
  32. }
  33. }
  34. return $result;
  35. }
  36. public function set($key, $value, $ttl = 0) {
  37. return $this->object->set($key, $value, $ttl);
  38. }
  39. public function rm($key) {
  40. return $this->object->delete($key);
  41. }
  42. public function clear() {
  43. return $this->object->flush();
  44. }
  45. public function inc($key, $step = 1) {
  46. $old = $this->get($key);
  47. if (!$old) {
  48. return false;
  49. }
  50. return $this->set($key, $old + $step);
  51. }
  52. public function dec($key, $step = 1) {
  53. $old = $this->get($key);
  54. if (!$old) {
  55. return false;
  56. }
  57. return $this->set($key, $old - $step);
  58. }
  59. }