BxDolCacheMemcache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolCacheMemcache extends BxDolCache
  10. {
  11. protected $iTTL = 3600;
  12. protected $iStoreFlag = 0;
  13. protected $oMemcache = null;
  14. /**
  15. * constructor
  16. */
  17. function __construct()
  18. {
  19. parent::__construct();
  20. if (class_exists('Memcache')) {
  21. $this->oMemcache = new Memcache();
  22. if (!$this->oMemcache->connect (getParam('sys_cache_memcache_host'), getParam('sys_cache_memcache_port')))
  23. $this->oMemcache = null;
  24. }
  25. }
  26. /**
  27. * Get data from cache server
  28. *
  29. * @param string $sKey - file name
  30. * @param int $iTTL - time to live
  31. * @return the data is got from cache.
  32. */
  33. function getData($sKey, $iTTL = false)
  34. {
  35. $mixedData = $this->oMemcache->get($sKey);
  36. return false === $mixedData ? null : $mixedData;
  37. }
  38. /**
  39. * Save data in cache server
  40. *
  41. * @param string $sKey - file name
  42. * @param mixed $mixedData - the data to be cached in the file
  43. * @param int $iTTL - time to live
  44. * @return boolean result of operation.
  45. */
  46. function setData($sKey, $mixedData, $iTTL = false)
  47. {
  48. return $this->oMemcache->set($sKey, $mixedData, $this->iStoreFlag, false === $iTTL ? $this->iTTL : $iTTL);
  49. }
  50. /**
  51. * Delete cache from cache server
  52. *
  53. * @param string $sKey - file name
  54. * @return result of the operation
  55. */
  56. function delData($sKey)
  57. {
  58. $this->oMemcache->delete($sKey);
  59. return true;
  60. }
  61. /**
  62. * Check if memcache is available
  63. * @return boolean
  64. */
  65. function isAvailable()
  66. {
  67. return $this->oMemcache == null ? false : true;
  68. }
  69. /**
  70. * Check if memcache extension is loaded
  71. * @return boolean
  72. */
  73. function isInstalled()
  74. {
  75. return extension_loaded('memcache');
  76. }
  77. /**
  78. * remove all data from cache by key prefix
  79. * @return true on success
  80. */
  81. function removeAllByPrefix ($s)
  82. {
  83. // not implemented for current cache
  84. return false;
  85. }
  86. }
  87. /** @} */