国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

120 строки
5.1KB

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