Sha256: db19bb586e2133f17acf6160a93a3fc33d61e9f932ff9d34c672877569bad823

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

#= require modularity/tools/object_tools

# 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: () ->
    modularity.object_length @cache


  # Removes the entry with the given key.
  remove: (key) =>
    delete @cache[key]


  # 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

6 entries across 6 versions & 1 rubygems

Version Path
modularity-rails-0.15.0 vendor/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.14.0 vendor/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.12.3 vendor/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.12.2 vendor/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.12.1 vendor/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.12.0 vendor/assets/javascripts/modularity/data/cache.coffee