Class: Volt::RepoCache::Cache
- Inherits:
-
Object
- Object
- Volt::RepoCache::Cache
- Includes:
- Util
- Defined in:
- lib/volt/repo_cache/cache.rb
Instance Attribute Summary collapse
-
#collections ⇒ Object
readonly
Returns the value of attribute collections.
-
#loaded ⇒ Object
readonly
returns a promise.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all caches, circular references, etc when cache no longer required - can’t be used after this.
-
#flush! ⇒ Object
Flush all cached collections and in turn all their models.
-
#initialize(**options) ⇒ Cache
constructor
Create a cache for a given Volt repo (store, page, local_store…) and the given collections within it.
- #method_missing(method, *args, &block) ⇒ Object
- #persistor ⇒ Object
- #query(collection_name, args) ⇒ Object
Methods included from Util
adder, arrify, creator, 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 |
# File 'lib/volt/repo_cache/cache.rb', line 38 def initialize(**) @repo = .delete(:repo) || Volt.current_app.store @collection_options = .delete(:collections) || {} load end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
52 53 54 55 56 |
# File 'lib/volt/repo_cache/cache.rb', line 52 def method_missing(method, *args, &block) collection = @collections[method] super unless collection collection end |
Instance Attribute Details
#collections ⇒ Object (readonly)
Returns the value of attribute collections.
9 10 11 |
# File 'lib/volt/repo_cache/cache.rb', line 9 def collections @collections end |
#loaded ⇒ Object (readonly)
returns a promise
10 11 12 |
# File 'lib/volt/repo_cache/cache.rb', line 10 def loaded @loaded end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/volt/repo_cache/cache.rb', line 9 def @options end |
#repo ⇒ Object (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
#clear ⇒ Object
Clear all caches, circular references, etc when cache no longer required - can’t be used after this.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/volt/repo_cache/cache.rb', line 61 def clear # debug __method__, __LINE__ @collections.each do |name, 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.
79 80 81 82 |
# File 'lib/volt/repo_cache/cache.rb', line 79 def flush! flushes = collections.values.map {|c| c.flush! } Promise.when(*flushes).then { self } end |
#persistor ⇒ Object
44 45 46 |
# File 'lib/volt/repo_cache/cache.rb', line 44 def persistor @repo.persistor end |
#query(collection_name, args) ⇒ Object
48 49 50 |
# File 'lib/volt/repo_cache/cache.rb', line 48 def query(collection_name, args) collections[collection_name].query(args) end |