Sha256: 949ac53ea21987a896efb3ebf6bdd59607f82fdb6da1976719c67e1fe293701a

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# A generic cache.
# Stores key-value pairs.
class window.modularity.Cache

  constructor: ->
    @cache = {}


  # Adds the given entry to the cache.
  # Overwrites existing entries.
  add: (key, value) ->
    @cache[key] = value


  # Returns the entry with the given key from the cache, or NULL if no entry exists.
  get: (key) ->
    @cache[key]


  # Looks up several entries at once.
  # Returns a hash of found entries, and a list of missing entries.
  get_many: (keys) ->
    result = { found: {}, missing: [] }
    for key in keys
      do (key) =>
        value = @cache[key]
        if value
          result.found[key] = value
        else
          result.missing.push key
    result
  getMany: Cache::get_many


  # Returns the number of cached objects.
  length: () ->
    # NOTE(KG): This doesn't work in IE8.
    Object.keys(@cache).length


  # Replaces the cache with the given data.
  # When 'key' is given, treats 'data' as an array of objects, and indexes each element by the given key.
  # When 'key' is not given, treats 'data' as an already indexed hash object.
  replace_all: (data, key) ->
    if key
      # Key given --> index the data array.
      @add(entry[key], entry) for entry in data
    else
      # Key not given --> use data as the new cache.
      @cache = data
  replaceAll: Cache::replace_all

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
modularity-rails-0.11.1 vendor/assets/javascripts/modularity/tools/cache.coffee