国内流行的内容管理系统(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.

105 lines
3.4KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * 缓存助手,支持文件和dedebiz cache
  5. *
  6. * @version $id:cache.helper.php 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. * @return string
  19. */
  20. if (!function_exists('GetCache')) {
  21. function GetCache($prefix, $key)
  22. {
  23. global $cfg_bizcore_appid, $cfg_bizcore_key;
  24. $key = md5($key);
  25. //商业组件缓存
  26. if (!empty($cfg_bizcore_appid) && !empty($cfg_bizcore_key)) {
  27. $client = new DedeBizClient();
  28. $key = trim($prefix.'_'.$key);
  29. $data = $client->CacheGet($key);
  30. $result = unserialize($data->data);
  31. $client->Close();
  32. return $result;
  33. }
  34. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  35. $result = @file_get_contents(DEDEDATA."/cache/$prefix/$key.php");
  36. if ($result === false) {
  37. return false;
  38. }
  39. $result = str_replace("<?php exit('dedebiz');?>\n\r", "", $result);
  40. $result = @unserialize($result);
  41. if ($result['timeout'] != 0 && $result['timeout'] < time()) {
  42. return false;
  43. }
  44. return $result['data'];
  45. }
  46. }
  47. /**
  48. * 写缓存
  49. *
  50. * @access public
  51. * @param string $prefix 前缀
  52. * @param string $key 键
  53. * @param string $value 值
  54. * @param string $timeout 缓存时间
  55. * @return int
  56. */
  57. if (!function_exists('SetCache')) {
  58. function SetCache($prefix, $key, $value, $timeout = 3600)
  59. {
  60. global $cfg_bizcore_appid, $cfg_bizcore_key;
  61. $key = md5($key);
  62. //商业组件缓存
  63. if (!empty($cfg_bizcore_appid) && !empty($cfg_bizcore_key)) {
  64. $client = new DedeBizClient();
  65. $key = trim($prefix.'_'.$key);
  66. $data = $client->CacheSet($key,serialize($value),$timeout);
  67. $result = unserialize($data->data);
  68. $client->Close();
  69. return $result;
  70. }
  71. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  72. $tmp['data'] = $value;
  73. $tmp['timeout'] = $timeout != 0 ? time() + (int) $timeout : 0;
  74. $cache_data = "<?php exit('dedebiz');?>\n\r".@serialize($tmp);
  75. return @PutFile(DEDEDATA."/cache/$prefix/$key.php", $cache_data);
  76. }
  77. }
  78. /**
  79. * 删除缓存
  80. *
  81. * @access public
  82. * @param string $prefix 前缀
  83. * @param string $key 键
  84. * @return string
  85. */
  86. if (!function_exists('DelCache')) {
  87. //删缓存
  88. function DelCache($prefix, $key)
  89. {
  90. global $cfg_bizcore_appid, $cfg_bizcore_key;
  91. $key = md5($key);
  92. //商业组件缓存
  93. if (!empty($cfg_bizcore_appid) && !empty($cfg_bizcore_key)) {
  94. $client = new DedeBizClient();
  95. $key = trim($prefix.'_'.$key);
  96. $data = $client->CacheDel($key);
  97. $client->Close();
  98. return true;
  99. }
  100. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  101. return @unlink(DEDEDATA."/cache/$prefix/$key.php");
  102. }
  103. }
  104. ?>