storage.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Helper object for interfacing with the LocalStorage. The LocalStorage
  3. * browser API allows for offline storage. That API is very unsophisticated,
  4. * and is essentially a lame key-value store. This object sits on top
  5. * and provides a number of useful utilities, including rudimentarycache
  6. * cache expiration.
  7. *
  8. * @constructor
  9. * @this {LocalStorageWrapper}
  10. * @param {String} namespace - A namespace to use in grouping access to localstorage. This keeps access clean and organized, while also making it possible to have multiple LocalStorage connections.
  11. */
  12. function LocalStorageWrapper(namespace) {
  13. this.namespace = namespace;
  14. }
  15. /**
  16. * A method for adding a key/value pair to LocalStorage.
  17. * Note that both parameters must be strings (JSON.stringify is your friend).
  18. *
  19. * @param {String} key - The name of the key.
  20. * @param {String} value - The value.
  21. */
  22. LocalStorageWrapper.prototype.set = function(key, value) {
  23. localStorage.setItem(this.namespace+"_"+key+"_value", value);
  24. localStorage.setItem(this.namespace+"_"+key+"_timestamp", $.now());
  25. };
  26. /**
  27. * A method for removing a key from LocalStorage.
  28. *
  29. * @param {String} key - The name of the key to remove.
  30. */
  31. LocalStorageWrapper.prototype.remove = function(key) {
  32. localStorage.removeItem(this.namespace+"_"+key+"_value");
  33. localStorage.removeItem(this.namespace+"_"+key+"_timestamp");
  34. };
  35. /**
  36. * A method for retrieving the value associated with the given key.
  37. *
  38. * @param {String} key - The name of the key to retrieve the value for.
  39. */
  40. LocalStorageWrapper.prototype.get = function(key) {
  41. return localStorage.getItem(this.namespace+"_"+key+"_value");
  42. };
  43. /**
  44. * A method for retrieving the value associated with the given key.
  45. * If the key does not exist, then the default value is used instead.
  46. * This default will be set.
  47. *
  48. * @param {String} key - The name of the key to retrieve the value for.
  49. * @param {String} defaultValue - The default value to use. Must be a string.
  50. */
  51. LocalStorageWrapper.prototype.getDefault = function(key, defaultValue) {
  52. if (this.has(key)) {
  53. return this.get(key);
  54. } else {
  55. this.set(key, defaultValue);
  56. return defaultValue;
  57. }
  58. };
  59. /**
  60. * A test for whether the given key is in LocalStorage.
  61. *
  62. * @param {String} key - The key to test existence for.
  63. */
  64. LocalStorageWrapper.prototype.has = function(key) {
  65. return localStorage.getItem(this.namespace+"_"+key+"_value") !== null;
  66. };
  67. /**
  68. * A test for whether the server has the newer version. This function
  69. * assumes that the server trip takes about 5 seconds. This method
  70. * is largely deprecated.
  71. *
  72. * @param {String} key - The key to check.
  73. * @param {Integer} server_time - The server's time as an epoch (in milliseconds)
  74. */
  75. LocalStorageWrapper.prototype.is_new = function(key, server_time) {
  76. var stored_time = localStorage.getItem(this.namespace+"_"+key+"_timestamp");
  77. return (server_time >= stored_time+5000);
  78. };