cache.mysql.func.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. $sql = 'DELETE FROM ' . tablename('core_cache') . ' WHERE `key`=:key';
  53. $params = array();
  54. $params[':key'] = $key;
  55. $result = pdo_query($sql, $params);
  56. return $result;
  57. }
  58. function cache_clean($prefix = '') {
  59. global $_W;
  60. if (empty($prefix)) {
  61. $sql = 'DELETE FROM ' . tablename('core_cache');
  62. $result = pdo_query($sql);
  63. if ($result) {
  64. unset($_W['cache']);
  65. }
  66. } else {
  67. $sql = 'DELETE FROM ' . tablename('core_cache') . ' WHERE `key` LIKE :key';
  68. $params = array();
  69. $params[':key'] = "{$prefix}:%";
  70. $result = pdo_query($sql, $params);
  71. }
  72. return $result;
  73. }