Module: Mongoid::CachedJson
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid-cached-json/config.rb,
lib/mongoid-cached-json/version.rb,
lib/mongoid-cached-json/cached_json.rb,
lib/mongoid-cached-json/key_references.rb
Defined Under Namespace
Modules: ClassMethods, Config Classes: KeyReferences
Constant Summary collapse
- VERSION =
'1.5.2'
Class Method Summary collapse
-
.configure ⇒ Config
(also: config)
Set the configuration options.
-
.materialize_json_references(key_refs, local_cache = {}, read_multi = false) ⇒ Object
Materialize all the JSON references in place.
-
.materialize_json_references_with_read_multi(key_refs, partial_json) ⇒ Object
Check whether the cache supports :read_multi and prefetch the data if it does.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Return the JSON representation of the object.
-
#as_json_cached(options = {}) ⇒ Object
Fetch the partial JSON and materialize all JSON references.
-
#as_json_partial(options = {}) ⇒ Object
Return a partial JSON without resolved references and all the keys.
-
#expire_cached_json ⇒ Object
Expire all JSON entries for this class.
Class Method Details
.configure ⇒ Config Also known as: config
Set the configuration options. Best used by passing a block.
245 246 247 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 245 def configure block_given? ? yield(Mongoid::CachedJson::Config) : Mongoid::CachedJson::Config end |
.materialize_json_references(key_refs, local_cache = {}, read_multi = false) ⇒ Object
Materialize all the JSON references in place.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 169 def self.materialize_json_references(key_refs, local_cache = {}, read_multi = false) key_refs.each_pair do |key, refs| refs.each do |ref| _ref = ref.delete(:_ref) key = _ref[:_key] fetched_json = local_cache[key] if local_cache.key?(key) unless fetched_json if read_multi # no value in cache, materialize and write fetched_json = (local_cache[key] = _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json])) Mongoid::CachedJson.config.cache.write(key, fetched_json) unless Mongoid::CachedJson.config.disable_caching else # fetch/write from cache fetched_json = (local_cache[key] = Mongoid::CachedJson.config.cache.fetch(key, force: !!Mongoid::CachedJson.config.disable_caching) do _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json]) end) end end if fetched_json ref.merge! fetched_json elsif _ref[:_parent] # a single _ref that resolved to a nil _ref[:_parent][_ref[:_field]] = nil end end end end |
.materialize_json_references_with_read_multi(key_refs, partial_json) ⇒ Object
Check whether the cache supports :read_multi and prefetch the data if it does.
160 161 162 163 164 165 166 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 160 def self.materialize_json_references_with_read_multi(key_refs, partial_json) unfrozen_keys = key_refs.keys.to_a.map(&:dup) if key_refs # see https://github.com/mperham/dalli/pull/320 read_multi = unfrozen_keys && Mongoid::CachedJson.config.cache.respond_to?(:read_multi) local_cache = read_multi ? Mongoid::CachedJson.config.cache.read_multi(*unfrozen_keys) : {} Mongoid::CachedJson.materialize_json_references(key_refs, local_cache, read_multi) if key_refs partial_json end |
Instance Method Details
#as_json(options = {}) ⇒ Object
Return the JSON representation of the object.
217 218 219 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 217 def as_json( = {}) as_json_cached() end |
#as_json_cached(options = {}) ⇒ Object
Fetch the partial JSON and materialize all JSON references.
211 212 213 214 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 211 def as_json_cached( = {}) keys, json = as_json_partial() Mongoid::CachedJson.materialize_json_references_with_read_multi(keys, json) end |
#as_json_partial(options = {}) ⇒ Object
Return a partial JSON without resolved references and all the keys.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 198 def as_json_partial( = {}) ||= {} if [:properties] && !all_json_properties.member?([:properties]) fail ArgumentError.new("Unknown properties option: #{[:properties]}") end # partial, unmaterialized JSON keys, partial_json = self.class.materialize_json({ properties: :short, is_top_level_json: true, version: Mongoid::CachedJson.config.default_version }.merge(), object: self) [keys, partial_json] end |
#expire_cached_json ⇒ Object
Expire all JSON entries for this class.
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 222 def expire_cached_json all_json_properties.each do |properties| [true, false].each do |is_top_level_json| all_json_versions.each do |version| Mongoid::CachedJson.config.cache.delete(self.class.cached_json_key({ properties: properties, is_top_level_json: is_top_level_json, version: version }, self.class, id)) end end end end |