bulk_cache_fetcher 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46f577da5b738735691d165c968cd243f2fff412
4
- data.tar.gz: 187e82b0dd313cba27e24bb77d3323af8cc2f5b6
3
+ metadata.gz: e95dac1446f4fbeb5d57c0036475a7b8c5ce08b3
4
+ data.tar.gz: bcd85e15b816226daa978a3ee4aad5295fc0f887
5
5
  SHA512:
6
- metadata.gz: fa9a8f78afd53293b374db43a7a8631d4d21d0ec921752da726888ae455a3c33d46236352c5834cbe5b3d4c37806819ac465cb27a1ec9d7a857508bc0333d473
7
- data.tar.gz: 92ec07ef649d39565508bbd98f94bb9c26f1a441e38f747d7a68133ce64bc29721703c9c65a8d19326d847a31d1de2c8f31372604d10faf4361bcd774bfa0f64
6
+ metadata.gz: 3aba42a22a052d4cace6f66895ed775669c0f346c8bfb32aa38804935f827b663610e847edc868f10b64fbe3d7516131c41eb0449420cba2eff881d58ce63f97
7
+ data.tar.gz: 8d3ef1bf3effadf0a2fa67eafe94734b20a7b4bf087dde24ff916a0704528a0b126db7c74c510447c2c136614d2c10eac0eefa923d4613ea8fd9b204d1d4b6eb
@@ -5,7 +5,7 @@
5
5
  # Rails' nested caching while avoiding the n+1 queries problem in the
6
6
  # uncached case.
7
7
  class BulkCacheFetcher
8
- VERSION = '0.0.1'
8
+ VERSION = '0.0.2'
9
9
 
10
10
  # Creates a new bulk cache fetcher, backed by +cache+. Cache must
11
11
  # respond to the standard Rails cache API, described on
@@ -43,10 +43,11 @@ class BulkCacheFetcher
43
43
  cached_objects = []
44
44
  uncached_identifiers = object_identifiers.dup
45
45
 
46
- object_identifiers.each do |identifier|
47
- cache_key = cache_key(identifier)
48
- cached_object = @cache.read(cache_key)
46
+ cache_keys = cache_keys(object_identifiers)
47
+ found_objects = @cache.read_multi(cache_keys)
49
48
 
49
+ cache_keys.each do |cache_key|
50
+ cached_object = found_objects[cache_key]
50
51
  uncached_identifiers.delete(cache_key) if cached_object
51
52
  cached_objects << cached_object
52
53
  end
@@ -92,6 +93,11 @@ class BulkCacheFetcher
92
93
  Array(identifier).first
93
94
  end
94
95
 
96
+ # Returns the cache keys for all of the +identifiers+.
97
+ def cache_keys(identifiers)
98
+ identifiers.map { |identifier| cache_key(identifier) }
99
+ end
100
+
95
101
  # Makes sure we can iterate over identifiers.
96
102
  def normalize(identifiers)
97
103
  identifiers.respond_to?(:each) ? identifiers : Array(identifiers)
@@ -10,6 +10,14 @@ class InMemoryCache
10
10
 
11
11
  def read(key); cache[key]; end
12
12
  def write(key, value, opts = {}); options[key] = opts; cache[key] = value; end
13
+ def read_multi(keys)
14
+ results = {}
15
+ keys.each do |key|
16
+ results[key] = read(key) if read(key)
17
+ end
18
+ results
19
+ end
20
+
13
21
  end
14
22
 
15
23
  class BulkCacheFetcherTest < Minitest::Unit::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulk_cache_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Weiss
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 2.0.6
91
+ rubygems_version: 2.1.5
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Bulk Cache Fetcher allows you to query the cache for a list of objects, and