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.6.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configureConfig Also known as: config

Set the configuration options. Best used by passing a block.

Examples:

Set up configuration options.

Mongoid::CachedJson.configure do |config|
  config.cache = Rails.cache
end

Returns:

  • (Config)

    The configuration obejct.


253
254
255
# File 'lib/mongoid-cached-json/cached_json.rb', line 253

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.


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mongoid-cached-json/cached_json.rb', line 177

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.


168
169
170
171
172
173
174
# File 'lib/mongoid-cached-json/cached_json.rb', line 168

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.


225
226
227
# File 'lib/mongoid-cached-json/cached_json.rb', line 225

def as_json(options = {})
  as_json_cached(options)
end

#as_json_cached(options = {}) ⇒ Object

Fetch the partial JSON and materialize all JSON references.


219
220
221
222
# File 'lib/mongoid-cached-json/cached_json.rb', line 219

def as_json_cached(options = {})
  keys, json = as_json_partial(options)
  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.


206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mongoid-cached-json/cached_json.rb', line 206

def as_json_partial(options = {})
  options ||= {}
  if options[:properties] && !all_json_properties.member?(options[:properties])
    fail ArgumentError.new("Unknown properties option: #{options[: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(options), object: self)
  [keys, partial_json]
end

#expire_cached_jsonObject

Expire all JSON entries for this class.


230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mongoid-cached-json/cached_json.rb', line 230

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