Advertisement
ukesh

hulk.phy

Feb 27th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2. * pStorage
  3. * --------
  4. * a wrapper
  5. around
  6. PersistJS an
  7. CryptoJS to
  8. allow TTL and
  9. Content
  10. Validation
  11. * by Barry
  12. Shteiman, 2014,
  13. ref:www.
  14. sectorix.com
  15. */
  16. var pStorage =
  17. new function()
  18. {
  19. this.uid =
  20. 'my_
  21. pStorage';
  22. this.salt =
  23. '';
  24. this.
  25. datastore = new
  26. Persist.Store(
  27. this.uid);
  28. this.get =
  29. function get(
  30. key) {
  31. var
  32. entry = JSON.
  33. parse(this.
  34. datastore.get(
  35. key)||0);
  36. if (!
  37. entry) return
  38. null;
  39. if
  40. (entry.hash !==
  41. this.hash(
  42. entry.value +
  43. this.salt)) {
  44. this.datastore
  45. .remove(key);
  46. return null;
  47. }
  48. if
  49. (entry.ttl &amp
  50. ;& entry.
  51. ttl + entry.now
  52. < this.now
  53. ()) {
  54. this.datastore
  55. .remove(key);
  56. return null;
  57. }
  58. return entry.
  59. value;
  60. }
  61. this.set =
  62. function set(
  63. key,value,ttl)
  64. {
  65. this.
  66. datastore.set(
  67. key, JSON.
  68. stringify({
  69. ttl
  70. : ttl || 0,
  71. now
  72. : this.now()
  73. ,
  74. hash : this.
  75. hash(value +
  76. this.salt),
  77. value : value
  78. }) );
  79. }
  80. this.del =
  81. function del(
  82. key) {
  83. var
  84. entry = JSON.
  85. parse(this.
  86. datastore.get(
  87. key)||"0&
  88. quot;);
  89. if (!
  90. entry) return
  91. null;
  92. else {
  93. this.datastore.
  94. remove(key);
  95. return null;
  96. }
  97. }
  98. this.now =
  99. function now ()
  100. {return+new
  101. Date}
  102. this.hash =
  103. function hash(
  104. value) {return
  105. CryptoJS.MD5(
  106. value).toString
  107. ();}
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement