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

111 lines
3.9KB

  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, $cfg_bizcore_hostname, $cfg_bizcore_port;
  24. $key = md5($key);
  25. //商业组件缓存
  26. if (!empty($cfg_bizcore_appid) && !empty($cfg_bizcore_key)) {
  27. $client = new DedeBizClient($cfg_bizcore_hostname, $cfg_bizcore_port);
  28. $client->appid = $cfg_bizcore_appid;
  29. $client->key = $cfg_bizcore_key;
  30. $key = trim($prefix.'_'.$key);
  31. $data = $client->CacheGet($key);
  32. $result = unserialize($data->data);
  33. $client->Close();
  34. return $result;
  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)
  61. {
  62. global $cfg_bizcore_appid, $cfg_bizcore_key, $cfg_bizcore_hostname, $cfg_bizcore_port;
  63. $key = md5($key);
  64. //商业组件缓存
  65. if (!empty($cfg_bizcore_appid) && !empty($cfg_bizcore_key)) {
  66. $client = new DedeBizClient($cfg_bizcore_hostname, $cfg_bizcore_port);
  67. $client->appid = $cfg_bizcore_appid;
  68. $client->key = $cfg_bizcore_key;
  69. $key = trim($prefix.'_'.$key);
  70. $data = $client->CacheSet($key,serialize($value),$timeout);
  71. $result = unserialize($data->data);
  72. $client->Close();
  73. return $result;
  74. }
  75. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  76. $tmp['data'] = $value;
  77. $tmp['timeout'] = $timeout != 0 ? time() + (int) $timeout : 0;
  78. $cache_data = "<?php exit('dedebiz');?>\n\r".@serialize($tmp);
  79. return @PutFile(DEDEDATA."/cache/$prefix/$key.php", $cache_data);
  80. }
  81. }
  82. /**
  83. * 删除缓存
  84. *
  85. * @access public
  86. * @param string $prefix 前缀
  87. * @param string $key 键
  88. * @return string
  89. */
  90. if (!function_exists('DelCache')) {
  91. //删缓存
  92. function DelCache($prefix, $key)
  93. {
  94. global $cfg_bizcore_appid, $cfg_bizcore_key, $cfg_bizcore_hostname, $cfg_bizcore_port;
  95. $key = md5($key);
  96. //商业组件缓存
  97. if (!empty($cfg_bizcore_appid) && !empty($cfg_bizcore_key)) {
  98. $client = new DedeBizClient($cfg_bizcore_hostname, $cfg_bizcore_port);
  99. $client->appid = $cfg_bizcore_appid;
  100. $client->key = $cfg_bizcore_key;
  101. $key = trim($prefix.'_'.$key);
  102. $data = $client->CacheDel($key);
  103. $client->Close();
  104. return true;
  105. }
  106. $key = substr($key, 0, 2).'/'.substr($key, 2, 2).'/'.substr($key, 4, 2).'/'.$key;
  107. return @unlink(DEDEDATA."/cache/$prefix/$key.php");
  108. }
  109. }
  110. ?>