国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
5.1KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 缓存小助手,支持文件和memcache
  5. *
  6. * @version $Id: cache.helper.php 1 10:46 2011-3-2 tianya $
  7. * @package DedeBIZ.Helpers
  8. * @copyright Copyright (c) 2022, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. /**
  13. * 读缓存
  14. *
  15. * @access public
  16. * @param string $prefix 前缀
  17. * @param string $key 键
  18. * @param string $is_memcache 是否为memcache缓存
  19. * @return string
  20. */
  21. if (!function_exists('GetCache')) {
  22. function GetCache($prefix, $key, $is_memcache = TRUE)
  23. {
  24. global $cache_helper_config;
  25. $key = md5($key);
  26. /* 如果启用MC缓存 */
  27. if ($is_memcache === TRUE && !empty($cache_helper_config['memcache']) && $cache_helper_config['memcache']['is_mc_enable'] === 'Y') {
  28. $mc_path = empty($cache_helper_config['memcache']['mc'][substr($key, 0, 1)]) ? $cache_helper_config['memcache']['mc']['default'] : $cache_helper_config['memcache']['mc'][substr($key, 0, 1)];
  29. $mc_path = parse_url($mc_path);
  30. $key = ltrim($mc_path['path'], '/').'_'.$prefix.'_'.$key;
  31. if (empty($GLOBALS['mc_'.$mc_path['host']])) {
  32. $GLOBALS['mc_'.$mc_path['host']] = new Memcache();
  33. $GLOBALS['mc_'.$mc_path['host']]->connect($mc_path['host'], $mc_path['port']);
  34. }
  35. return $GLOBALS['mc_'.$mc_path['host']]->get($key);
  36. }
  37. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  38. $result = @file_get_contents(DEDEDATA."/cache/$prefix/$key.php");
  39. if ($result === false) {
  40. return false;
  41. }
  42. $result = str_replace("<?php exit('dedebiz');?>\n\r", "", $result);
  43. $result = @unserialize($result);
  44. if ($result['timeout'] != 0 && $result['timeout'] < time()) {
  45. return false;
  46. }
  47. return $result['data'];
  48. }
  49. }
  50. /**
  51. * 写缓存
  52. *
  53. * @access public
  54. * @param string $prefix 前缀
  55. * @param string $key 键
  56. * @param string $value 值
  57. * @param string $timeout 缓存时间
  58. * @return int
  59. */
  60. if (!function_exists('SetCache')) {
  61. function SetCache($prefix, $key, $value, $timeout = 3600, $is_memcache = TRUE)
  62. {
  63. global $cache_helper_config;
  64. $key = md5($key);
  65. /* 如果启用MC缓存 */
  66. if (!empty($cache_helper_config['memcache']) && $cache_helper_config['memcache']['is_mc_enable'] === 'Y' && $is_memcache === TRUE) {
  67. $mc_path = empty($cache_helper_config['memcache']['mc'][substr($key, 0, 1)]) ? $cache_helper_config['memcache']['mc']['default'] : $cache_helper_config['memcache']['mc'][substr($key, 0, 1)];
  68. $mc_path = parse_url($mc_path);
  69. $key = ltrim($mc_path['path'], '/').'_'.$prefix.'_'.$key;
  70. if (empty($GLOBALS['mc_'.$mc_path['host']])) {
  71. $GLOBALS['mc_'.$mc_path['host']] = new Memcache();
  72. $GLOBALS['mc_'.$mc_path['host']]->connect($mc_path['host'], $mc_path['port']);
  73. //设置数据压缩门槛
  74. //$GLOBALS ['mc_'.$mc_path ['host']]->setCompressThreshold(2048, 0.2);
  75. }
  76. $result = $GLOBALS['mc_'.$mc_path['host']]->set($key, $value, MEMCACHE_COMPRESSED, $timeout);
  77. return $result;
  78. }
  79. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  80. $tmp['data'] = $value;
  81. $tmp['timeout'] = $timeout != 0 ? time() + (int) $timeout : 0;
  82. $cache_data = "<?php exit('dedebiz');?>\n\r".@serialize($tmp);
  83. return @PutFile(DEDEDATA."/cache/$prefix/$key.php", $cache_data);
  84. }
  85. }
  86. /**
  87. * 删除缓存
  88. *
  89. * @access public
  90. * @param string $prefix 前缀
  91. * @param string $key 键
  92. * @param string $is_memcache 是否为memcache缓存
  93. * @return string
  94. */
  95. if (!function_exists('DelCache')) {
  96. /* 删缓存 */
  97. function DelCache($prefix, $key, $is_memcache = TRUE)
  98. {
  99. global $cache_helper_config;
  100. $key = md5($key);
  101. /* 如果启用MC缓存 */
  102. if (!empty($cache_helper_config['memcache']) && $cache_helper_config['memcache']['is_mc_enable'] === TRUE && $is_memcache === TRUE) {
  103. $mc_path = empty($cache_helper_config['memcache']['mc'][substr($key, 0, 1)]) ? $cache_helper_config['memcache']['mc']['default'] : $cache_helper_config['memcache']['mc'][substr($key, 0, 1)];
  104. $mc_path = parse_url($mc_path);
  105. $key = ltrim($mc_path['path'], '/').'_'.$prefix.'_'.$key;
  106. if (empty($GLOBALS['mc_'.$mc_path['host']])) {
  107. $GLOBALS['mc_'.$mc_path['host']] = new Memcache();
  108. $GLOBALS['mc_'.$mc_path['host']]->connect($mc_path['host'], $mc_path['port']);
  109. }
  110. return $GLOBALS['mc_'.$mc_path['host']]->delete($key);
  111. }
  112. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  113. return @unlink(DEDEDATA."/cache/$prefix/$key.php");
  114. }
  115. }