mongoid-cached-json 1.4.2 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -157,21 +157,31 @@ module Mongoid
157
157
  # Check whether the cache supports :read_multi and prefetch the data if it does.
158
158
  def self.materialize_json_references_with_read_multi(key_refs, partial_json)
159
159
  unfrozen_keys = key_refs.keys.to_a.map(&:dup) if key_refs # see https://github.com/mperham/dalli/pull/320
160
- local_cache = unfrozen_keys && Mongoid::CachedJson.config.cache.respond_to?(:read_multi) ? Mongoid::CachedJson.config.cache.read_multi(unfrozen_keys) : {}
161
- Mongoid::CachedJson.materialize_json_references(key_refs, local_cache) if key_refs
160
+ read_multi = unfrozen_keys && Mongoid::CachedJson.config.cache.respond_to?(:read_multi)
161
+ local_cache = read_multi ? Mongoid::CachedJson.config.cache.read_multi(unfrozen_keys) : {}
162
+ Mongoid::CachedJson.materialize_json_references(key_refs, local_cache, read_multi) if key_refs
162
163
  partial_json
163
164
  end
164
165
 
165
166
  # Materialize all the JSON references in place.
166
- def self.materialize_json_references(key_refs, local_cache = {})
167
+ def self.materialize_json_references(key_refs, local_cache = {}, read_multi = false)
167
168
  key_refs.each_pair do |key, refs|
168
169
  refs.each do |ref|
169
170
  _ref = ref.delete(:_ref)
170
171
  key = _ref[:_key]
171
172
  fetched_json = local_cache[key] if local_cache.has_key?(key)
172
- fetched_json ||= (local_cache[key] = Mongoid::CachedJson.config.cache.fetch(key, { :force => !! Mongoid::CachedJson.config.disable_caching }) do
173
- _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json])
174
- end)
173
+ if ! fetched_json
174
+ if read_multi
175
+ # no value in cache, materialize and write
176
+ fetched_json = (local_cache[key] = _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json]))
177
+ Mongoid::CachedJson.config.cache.write(key, fetched_json) unless !! Mongoid::CachedJson.config.disable_caching
178
+ else
179
+ # fetch/write from cache
180
+ fetched_json = (local_cache[key] = Mongoid::CachedJson.config.cache.fetch(key, { :force => !! Mongoid::CachedJson.config.disable_caching }) do
181
+ _ref[:_clazz].materialize_cached_json(* _ref[:_materialize_cached_json])
182
+ end)
183
+ end
184
+ end
175
185
  if fetched_json
176
186
  ref.merge! fetched_json
177
187
  elsif _ref[:_parent]
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  module CachedJson
3
- VERSION = '1.4.2'
3
+ VERSION = '1.4.3'
4
4
  end
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-cached-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-24 00:00:00.000000000 Z
14
+ date: 2013-01-25 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -174,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  segments:
176
176
  - 0
177
- hash: -3841515240372741329
177
+ hash: -3669552343151646894
178
178
  required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  none: false
180
180
  requirements: