cache.mysql.func.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function cache_read($key) {
  8. $cachedata = pdo_getcolumn('core_cache', array('key' => $key), 'value');
  9. if (empty($cachedata)) {
  10. return '';
  11. }
  12. $cachedata = iunserializer($cachedata);
  13. if (is_array($cachedata) && !empty($cachedata['expire']) && !empty($cachedata['data'])) {
  14. if ($cachedata['expire'] > TIMESTAMP) {
  15. return $cachedata['data'];
  16. } else {
  17. return '';
  18. }
  19. } else {
  20. return $cachedata;
  21. }
  22. }
  23. function cache_search($prefix) {
  24. $sql = 'SELECT * FROM ' . tablename('core_cache') . ' WHERE `key` LIKE :key';
  25. $params = array();
  26. $params[':key'] = "{$prefix}%";
  27. $rs = pdo_fetchall($sql, $params);
  28. $result = array();
  29. foreach ((array)$rs as $v) {
  30. $result[$v['key']] = iunserializer($v['value']);
  31. }
  32. return $result;
  33. }
  34. function cache_write($key, $data, $expire = 0) {
  35. if (empty($key) || !isset($data)) {
  36. return false;
  37. }
  38. $record = array();
  39. $record['key'] = $key;
  40. if (!empty($expire)) {
  41. $cache_data = array(
  42. 'expire' => TIMESTAMP + $expire,
  43. 'data' => $data
  44. );
  45. } else {
  46. $cache_data = $data;
  47. }
  48. $record['value'] = iserializer($cache_data);
  49. return pdo_insert('core_cache', $record, true);
  50. }
  51. function cache_delete($key) {
  52. $cache_relation_keys = cache_relation_keys($key);
  53. if (is_error($cache_relation_keys)) {
  54. return $cache_relation_keys;
  55. }
  56. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  57. foreach ($cache_relation_keys as $key) {
  58. $cache_info = cache_load($key);
  59. if (!empty($cache_info)) {
  60. $sql = 'DELETE FROM ' . tablename('core_cache') . ' WHERE `key`=:key';
  61. $params = array();
  62. $params[':key'] = $key;
  63. $result = pdo_query($sql, $params);
  64. if (!$result) {
  65. return error(1, '缓存:' . $key . ' 删除失败!');
  66. }
  67. }
  68. }
  69. return true;
  70. }
  71. }
  72. function cache_clean($prefix = '') {
  73. global $_W;
  74. if (empty($prefix)) {
  75. $sql = 'DELETE FROM ' . tablename('core_cache');
  76. $result = pdo_query($sql);
  77. if ($result) {
  78. unset($_W['cache']);
  79. }
  80. } else {
  81. $cache_relation_keys = cache_relation_keys($prefix);
  82. if (is_error($cache_relation_keys)) {
  83. return $cache_relation_keys;
  84. }
  85. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  86. foreach ($cache_relation_keys as $key) {
  87. preg_match_all('/\:([a-zA-Z0-9\-\_]+)/', $key, $matches);
  88. $sql = "DELETE FROM " . tablename('core_cache') . ' WHERE `key` LIKE :key';
  89. $params = array();
  90. $params[':key'] = "we7:{$matches[1][0]}%";
  91. $result = pdo_query($sql, $params);
  92. if (!$result) {
  93. return error(-1, '缓存 ' . $key . '删除失败!');
  94. }
  95. }
  96. }
  97. }
  98. return true;
  99. }