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.1'
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.
242 243 244 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 242 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.
167 168 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 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 167 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.has_key?(key) if ! 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.
158 159 160 161 162 163 164 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 158 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.
215 216 217 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 215 def as_json( = {}) as_json_cached() end |
#as_json_cached(options = {}) ⇒ Object
Fetch the partial JSON and materialize all JSON references.
209 210 211 212 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 209 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.
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 196 def as_json_partial( = {}) ||= {} if [:properties] and ! self.all_json_properties.member?([:properties]) raise 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.
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/mongoid-cached-json/cached_json.rb', line 220 def expire_cached_json self.all_json_properties.each do |properties| [true, false].each do |is_top_level_json| self.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, self.id)) end end end end |