Lungo.Data.Cache.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Temporary cache system
  3. *
  4. * @namespace LUNGO.Data
  5. * @class Cache
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.Data.Cache = (function(lng, undefined) {
  11. var _cache = {};
  12. /**
  13. * Sets in the LungoJS cache system a new key/value
  14. *
  15. * @method set
  16. *
  17. * @param {string} Key for the new value
  18. * @param {object} Type of environment: DESKTOP_ENVIRONMENT or MOBILE_ENVIRONMENT
  19. */
  20. var set = function(key, value) {
  21. if (exists(key)) {
  22. _cache[key] = lng.Core.mix(get(key), value);
  23. } else {
  24. _cache[key] = value;
  25. }
  26. };
  27. /**
  28. * Returns the value of a given key.
  29. *
  30. * @method get
  31. *
  32. * @param {string} Key in LungoJS Cache System
  33. * @param {string} [OPTIONAL] Subkey in LungoJS Cache System
  34. * @return {object} Value
  35. */
  36. var get = function(key, value) {
  37. if (arguments.length === 1) {
  38. return _cache[key];
  39. } else {
  40. return _cache[arguments[0]][arguments[1]];
  41. }
  42. };
  43. /**
  44. * Removes the instance in LungoJs Cache System of a given key
  45. *
  46. * @method remove
  47. *
  48. * @param {string} Key in LungoJS Cache System
  49. * @param {string} [OPTIONAL] Subkey in LungoJS Cache System
  50. */
  51. var remove = function(key, value) {
  52. if (arguments.length === 1) {
  53. delete _cache[key];
  54. } else {
  55. delete _cache[arguments[0]][arguments[1]];
  56. }
  57. };
  58. /**
  59. * Returns the existence of a key in LungoJs Cache System
  60. *
  61. * @method exists
  62. *
  63. * @param {String} Key in LungoJS Cache System
  64. * @return {Boolean} true if exists, false if not
  65. */
  66. var exists = function(key) {
  67. return (_cache[key]) ? true : false;
  68. };
  69. return {
  70. set: set,
  71. get: get,
  72. remove: remove,
  73. exists: exists
  74. };
  75. })(LUNGO);