ApcuCache.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace ACES\Common\cache;
  3. use ACES\TDEClient;
  4. class ApcuCache extends iCache
  5. {
  6. private $isEnable = false;
  7. function __construct()
  8. {
  9. if (extension_loaded("apcu") && ini_get('apc.enabled')==1){
  10. $this->isEnable = true;
  11. }
  12. }
  13. public function get($key)
  14. {
  15. if($this->isEnable){
  16. $key = md5($key);
  17. return apcu_fetch(self::$CACHE_PREFIX . $key);
  18. }
  19. }
  20. /**
  21. * @param $key
  22. * @param $var TDEClient
  23. */
  24. public function set($key,$var)
  25. {
  26. if($this->isEnable){
  27. if ($var && !$var->checkNullParam()) {
  28. $key = md5($key);
  29. apcu_store(self::$CACHE_PREFIX . $key, $var, self::$CACHE_TTL);
  30. }
  31. }
  32. }
  33. public function delete($key)
  34. {
  35. if($this->isEnable){
  36. $key = md5($key);
  37. apcu_delete(self::$CACHE_PREFIX . $key);
  38. }
  39. }
  40. public function cacheable()
  41. {
  42. return $this->isEnable;
  43. }
  44. }