BxDolCacheMemcache.php 2.2 KB

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