国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

130 rindas
5.5KB

  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 DedeCMS.Helpers
  7. * @copyright Copyright (c) 2020, 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. {
  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. {
  29. $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 )];
  30. $mc_path = parse_url ( $mc_path );
  31. $key = ltrim ( $mc_path ['path'], '/' ) . '_' . $prefix . '_' . $key;
  32. if (empty ( $GLOBALS ['mc_' . $mc_path ['host']] ))
  33. {
  34. $GLOBALS ['mc_' . $mc_path ['host']] = new Memcache ( );
  35. $GLOBALS ['mc_' . $mc_path ['host']]->connect ( $mc_path ['host'], $mc_path ['port'] );
  36. }
  37. return $GLOBALS ['mc_' . $mc_path ['host']]->get ( $key );
  38. }
  39. $key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
  40. $result = @file_get_contents ( DEDEDATA . "/cache/$prefix/$key.php" );
  41. if ($result === false)
  42. {
  43. return false;
  44. }
  45. $result = str_replace("<?php exit('dedecms');?>\n\r", "", $result);
  46. $result = @unserialize ( $result );
  47. if($result ['timeout'] != 0 && $result ['timeout'] < time ())
  48. {
  49. return false;
  50. }
  51. return $result ['data'];
  52. }
  53. }
  54. /**
  55. * 写缓存
  56. *
  57. * @access public
  58. * @param string $prefix 前缀
  59. * @param string $key 键
  60. * @param string $value 值
  61. * @param string $timeout 缓存时间
  62. * @return int
  63. */
  64. if ( ! function_exists('SetCache'))
  65. {
  66. function SetCache($prefix, $key, $value, $timeout = 3600, $is_memcache = TRUE)
  67. {
  68. global $cache_helper_config;
  69. $key = md5 ( $key );
  70. /* 如果启用MC缓存 */
  71. if (! empty ( $cache_helper_config['memcache'] ) && $cache_helper_config['memcache'] ['is_mc_enable'] === 'Y' && $is_memcache === TRUE)
  72. {
  73. $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 )];
  74. $mc_path = parse_url ( $mc_path );
  75. $key = ltrim ( $mc_path ['path'], '/' ) . '_' . $prefix . '_' . $key;
  76. if (empty ( $GLOBALS ['mc_' . $mc_path ['host']] ))
  77. {
  78. $GLOBALS ['mc_' . $mc_path ['host']] = new Memcache ( );
  79. $GLOBALS ['mc_' . $mc_path ['host']]->connect ( $mc_path ['host'], $mc_path ['port'] );
  80. //设置数据压缩门槛
  81. //$GLOBALS ['mc_' . $mc_path ['host']]->setCompressThreshold(2048, 0.2);
  82. }
  83. $result = $GLOBALS ['mc_' . $mc_path ['host']]->set ( $key, $value, MEMCACHE_COMPRESSED, $timeout );
  84. return $result;
  85. }
  86. $key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
  87. $tmp ['data'] = $value;
  88. $tmp ['timeout'] = $timeout != 0 ? time () + ( int ) $timeout : 0;
  89. $cache_data = "<?php exit('dedecms');?>\n\r".@serialize ( $tmp );
  90. return @PutFile ( DEDEDATA . "/cache/$prefix/$key.php", $cache_data);
  91. }
  92. }
  93. /**
  94. * 删除缓存
  95. *
  96. * @access public
  97. * @param string $prefix 前缀
  98. * @param string $key 键
  99. * @param string $is_memcache 是否为memcache缓存
  100. * @return string
  101. */
  102. if ( ! function_exists('DelCache'))
  103. {
  104. /* 删缓存 */
  105. function DelCache($prefix, $key, $is_memcache = TRUE)
  106. {
  107. global $cache_helper_config;
  108. $key = md5 ( $key );
  109. /* 如果启用MC缓存 */
  110. if (! empty ( $cache_helper_config['memcache'] ) && $cache_helper_config['memcache'] ['is_mc_enable'] === TRUE && $is_memcache === TRUE)
  111. {
  112. $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 )];
  113. $mc_path = parse_url ( $mc_path );
  114. $key = ltrim ( $mc_path ['path'], '/' ) . '_' . $prefix . '_' . $key;
  115. if (empty ( $GLOBALS ['mc_' . $mc_path ['host']] ))
  116. {
  117. $GLOBALS ['mc_' . $mc_path ['host']] = new Memcache ( );
  118. $GLOBALS ['mc_' . $mc_path ['host']]->connect ( $mc_path ['host'], $mc_path ['port'] );
  119. }
  120. return $GLOBALS ['mc_' . $mc_path ['host']]->delete ( $key );
  121. }
  122. $key = substr ( $key, 0, 2 ) . '/' . substr ( $key, 2, 2 ) . '/' . substr ( $key, 4, 2 ) . '/' . $key;
  123. return @unlink ( DEDEDATA . "/cache/$prefix/$key.php" );
  124. }
  125. }