Sha256: 3ca89d4ef4c9124da9b0d11bc373f6b7f718ec864b16807e01ca9c30e521568c

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

#= require modularity/tools/object_tools

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

  constructor: ->

    # Container for the cached objects.
    @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]


  # Removes all entries with the given keys.
  remove_many: (keys) ->
    @remove(key) for key in keys


  # 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

20 entries across 20 versions & 1 rubygems

Version Path
modularity-rails-0.28.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.27.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.26.4 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.26.3 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.26.2 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.26.1 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.26.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.25.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.24.1 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.24.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.23.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.22.1 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.22.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.21.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.20.3 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.20.2 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.20.1 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.20.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.19.0 app/assets/javascripts/modularity/data/cache.coffee
modularity-rails-0.17.0 app/assets/javascripts/modularity/data/cache.coffee