Lungo.Data.Cache.coffee 1.5 KB

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