Class: Volt::RepoCache::Cache

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/volt/repo_cache/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

adder, arrify, creator, debug, friend?, friends_only, not_yet_implemented, prefix_method, remover, setter, subclass_responsibility, time, unsupported

Constructor Details

#initialize(**options) ⇒ Cache

Create a cache for a given Volt repo (store, page, local_store…) and the given collections within it.

If no repo given then Volt.current_app.store will be used.

Collections is an array of hashes each with a collection name as key, and a hash of options as value. Options are: :where => query (as for store._collection.where(query)) … The cache should be used until the ‘loaded’ promise resolves.

For example, to cache all users:

RepoCache.new(collections: [:_users]).loaded.then do |cache|
  puts "cache contains #{cache._users.size} users"
end

Call #flush! to flush all changes to the repo. Call #clear when finished with the cache to help garbage collection.

TODO: read_only should be inherited by has_… targets



38
39
40
41
42
43
# File 'lib/volt/repo_cache/cache.rb', line 38

def initialize(**options)
  # debug __method__, __LINE__, "@options = #{@options}"
  @repo = options.delete(:repo) || Volt.current_app.store
  @collection_options = options.delete(:collections) || {}
  load
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



53
54
55
56
57
# File 'lib/volt/repo_cache/cache.rb', line 53

def method_missing(method, *args, &block)
  collection = @collections[method]
  super unless collection
  collection
end

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



9
10
11
# File 'lib/volt/repo_cache/cache.rb', line 9

def collections
  @collections
end

#loadedObject (readonly)

returns a promise



10
11
12
# File 'lib/volt/repo_cache/cache.rb', line 10

def loaded
  @loaded
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/volt/repo_cache/cache.rb', line 9

def options
  @options
end

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/volt/repo_cache/cache.rb', line 9

def repo
  @repo
end

Instance Method Details

#clearObject

Clear all caches, circular references, etc when cache no longer required - can’t be used after this.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/volt/repo_cache/cache.rb', line 62

def clear
  # debug __method__, __LINE__
  @collections.each do |name, collection|
    # debug __method__, __LINE__, "name=#{name} collection=#{collection}"
    collection.send(:uncache)
  end
  @collections = {}

  # TODO: this is not nice, but bad things happen if we don't clear the repo persistor's identity map
  # -> ensure we don't leave patched models lying around
  # debug __method__, __LINE__, "calling @repo.persistor.clear_identity_map "
  # @repo.persistor.clear_identity_map # otherwise error if new customer add via repo_cache
end

#flush!Object

Flush all cached collections and in turn all their models. Flushing performs inserts, updates or destroys as required. Returns a promise with this cache as value or error(s) if any occurred.



81
82
83
84
# File 'lib/volt/repo_cache/cache.rb', line 81

def flush!
  flushes = collections.values.map {|c| c.flush! }
  Promise.when(*flushes).then { self }
end

#persistorObject



45
46
47
# File 'lib/volt/repo_cache/cache.rb', line 45

def persistor
  @repo.persistor
end

#query(collection_name, args) ⇒ Object



49
50
51
# File 'lib/volt/repo_cache/cache.rb', line 49

def query(collection_name, args)
  collections[collection_name].query(args)
end