wcache.js 870 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var dtime = '_deadtime';
  2. function put(k, v, t) {
  3. wx.setStorageSync(k, v)
  4. var seconds = parseInt(t);
  5. if (seconds > 0) {
  6. var timestamp = Date.parse(new Date());
  7. timestamp = timestamp / 1000 + seconds;
  8. wx.setStorageSync(k + dtime, timestamp + "")
  9. } else {
  10. wx.removeStorageSync(k + dtime)
  11. }
  12. }
  13. function get(k, def) {
  14. var deadtime = parseInt(wx.getStorageSync(k + dtime))
  15. if (deadtime) {
  16. if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
  17. if (def) {
  18. return def;
  19. } else {
  20. return;
  21. }
  22. }
  23. }
  24. var res = wx.getStorageSync(k);
  25. if (res) {
  26. return res;
  27. } else {
  28. return def;
  29. }
  30. }
  31. function remove(k) {
  32. wx.removeStorageSync(k);
  33. wx.removeStorageSync(k + dtime);
  34. }
  35. function clear() {
  36. wx.clearStorageSync();
  37. }
  38. module.exports = {
  39. put: put,
  40. get: get,
  41. remove: remove,
  42. clear: clear,
  43. }