Module: IdentityCache::WithPrimaryIndex::ClassMethods
- Defined in:
- lib/identity_cache/with_primary_index.rb
Instance Method Summary collapse
-
#cache_index(*fields, unique: false) ⇒ Object
Declares a new index in the cache for the class where IdentityCache was included.
- #cached_primary_index ⇒ Object private
-
#exists_with_identity_cache?(id) ⇒ Boolean
Similar to ActiveRecord::Base#exists? will return true if the id can be found in the cache or in the DB.
-
#expire_primary_key_cache_index(id) ⇒ Object
Invalidates the primary cache index for the associated record.
-
#fetch(id, **options) ⇒ self
Fetch the record by its primary key from the cache or read from the database and fill the cache on a cache miss.
-
#fetch_by_id(id, includes: nil, **cache_fetcher_options) ⇒ self|nil
Fetch the record by its primary key from the cache or read from the database and fill the cache on a cache miss.
-
#fetch_multi(*ids, includes: nil) ⇒ Object
Default fetcher added to the model on inclusion, if behaves like ActiveRecord::Base.find_all_by_id.
- #primary_cache_index_enabled ⇒ Object
Instance Method Details
#cache_index(*fields, unique: false) ⇒ Object
Declares a new index in the cache for the class where IdentityCache was included.
IdentityCache will add a fetch_by_field1_and_field2_and_…field for every index.
Example:
class Product
include IdentityCache
cache_index :name, :vendor
end
Will add Product.fetch_by_name_and_vendor
Parameters
fields Array of symbols or strings representing the fields in the index
Options
-
unique: if the index would only have unique values. Default is false
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/identity_cache/with_primary_index.rb', line 57 def cache_index(*fields, unique: false) attribute_proc = -> { primary_key } cache_attribute_by_alias(attribute_proc, alias_name: :id, by: fields, unique: unique) field_list = fields.join("_and_") arg_list = (0...fields.size).collect { |i| "arg#{i}" }.join(",") if unique instance_eval(" def fetch_by_\#{field_list}(\#{arg_list}, includes: nil)\n id = fetch_id_by_\#{field_list}(\#{arg_list})\n id && fetch_by_id(id, includes: includes)\n end\n\n # exception throwing variant\n def fetch_by_\#{field_list}!(\#{arg_list}, includes: nil)\n fetch_by_\#{field_list}(\#{arg_list}, includes: includes) or raise IdentityCache::RecordNotFound\n end\n CODE\n else\n instance_eval(<<-CODE, __FILE__, __LINE__ + 1)\n def fetch_by_\#{field_list}(\#{arg_list}, includes: nil)\n ids = fetch_id_by_\#{field_list}(\#{arg_list})\n ids.empty? ? ids : fetch_multi(ids, includes: includes)\n end\n CODE\n end\n\n if fields.length == 1\n instance_eval(<<-CODE, __FILE__, __LINE__ + 1)\n def fetch_multi_by_\#{field_list}(index_values, includes: nil)\n ids = fetch_multi_id_by_\#{field_list}(index_values).values.flatten(1)\n return ids if ids.empty?\n fetch_multi(ids, includes: includes)\n end\n CODE\n end\nend\n", __FILE__, __LINE__ + 1) |
#cached_primary_index ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
27 28 29 |
# File 'lib/identity_cache/with_primary_index.rb', line 27 def cached_primary_index @cached_primary_index ||= Cached::PrimaryIndex.new(self) end |
#exists_with_identity_cache?(id) ⇒ Boolean
Similar to ActiveRecord::Base#exists? will return true if the id can be found in the cache or in the DB.
98 99 100 |
# File 'lib/identity_cache/with_primary_index.rb', line 98 def exists_with_identity_cache?(id) !!fetch_by_id(id) end |
#expire_primary_key_cache_index(id) ⇒ Object
Invalidates the primary cache index for the associated record. Will not invalidate cached attributes.
159 160 161 |
# File 'lib/identity_cache/with_primary_index.rb', line 159 def expire_primary_key_cache_index(id) cached_primary_index.expire(id) end |
#fetch(id, **options) ⇒ self
Fetch the record by its primary key from the cache or read from the database and fill the cache on a cache miss. This behaves like ‘readonly.find(id)` being called on the model.
141 142 143 144 145 |
# File 'lib/identity_cache/with_primary_index.rb', line 141 def fetch(id, **) fetch_by_id(id, **) || raise( IdentityCache::RecordNotFound, "Couldn't find #{name} with ID=#{id}" ) end |
#fetch_by_id(id, includes: nil, **cache_fetcher_options) ⇒ self|nil
Fetch the record by its primary key from the cache or read from the database and fill the cache on a cache miss. This behaves like ‘where(id: id).readonly.first` being called on the model.
125 126 127 128 129 130 131 |
# File 'lib/identity_cache/with_primary_index.rb', line 125 def fetch_by_id(id, includes: nil, **) ensure_base_model raise_if_scoped record = cached_primary_index.fetch(id, ) prefetch_associations(includes, [record]) if record && includes record end |
#fetch_multi(*ids, includes: nil) ⇒ Object
Default fetcher added to the model on inclusion, if behaves like ActiveRecord::Base.find_all_by_id
149 150 151 152 153 154 155 156 |
# File 'lib/identity_cache/with_primary_index.rb', line 149 def fetch_multi(*ids, includes: nil) ensure_base_model raise_if_scoped ids.flatten!(1) records = cached_primary_index.fetch_multi(ids) prefetch_associations(includes, records) if includes records end |
#primary_cache_index_enabled ⇒ Object
31 32 33 |
# File 'lib/identity_cache/with_primary_index.rb', line 31 def primary_cache_index_enabled true end |