mongoid_cache_store 0.1.1 → 1.0.0
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 +4 -4
- data/README.md +35 -4
- data/lib/mongoid_cache_store.rb +9 -8
- data/lib/mongoid_cache_store/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c75675a4b3ebb01dc4faea3cdcc77e59620e1014
|
4
|
+
data.tar.gz: 4db06917b8bfc8f706d1df5d0ddbc04f89300755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c934082e5159e27fa6bfe534617d18c198965e83f4b4a47bc4dba158387f60500e0613286cefd676cb7de0ac67785a8df136f4154e97a8721c7d8fecf72822c3
|
7
|
+
data.tar.gz: cc20747f1ccb773fa654c01fd3e89c83d0dbd7b99d2d1872960ddb9e0d88d937924eb0beb2b5474ced8a38774c51ab132247aa1acb552c6bbbe703e433717cd6
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# MongoidCacheStore
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
MongoidCacheStore helps in reducing the number of queries to the database.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -21,8 +19,41 @@ Or install it yourself as:
|
|
21
19
|
$ gem install mongoid_cache_store
|
22
20
|
|
23
21
|
## Usage
|
22
|
+
For ex:
|
23
|
+
```ruby
|
24
|
+
class User
|
25
|
+
include Mongoid::Document
|
26
|
+
belongs_to :image, class_name: UserImage, inverse_of: nil
|
27
|
+
# other user fields goes here
|
28
|
+
end
|
29
|
+
|
30
|
+
class UserImage
|
31
|
+
include Mongoid::Document
|
32
|
+
|
33
|
+
# image field goes here
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Now, in order to display list of users on a listing page, we need data for users including their image url.
|
38
|
+
Accessing image method again & again will increase number of queries.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
cache_store = CacheStore.new.cache_docs(user_ids, User, [{field_name: 'image_id', klass: UserImage}])
|
42
|
+
user = cache_store.document(user_id, User)
|
43
|
+
user_image = cache_store.document(user.image_id, UserImage)
|
44
|
+
```
|
24
45
|
|
25
|
-
|
46
|
+
To fetch all documents of a particular klass cached.
|
47
|
+
```ruby
|
48
|
+
cache_store = CacheStore.new.cache_docs(user_ids, User, [{field_name: 'image_id', klass: UserImage}])
|
49
|
+
users = cache_store.sorted_documents(User)
|
50
|
+
```
|
51
|
+
|
52
|
+
To fetch list of documents for a list of ids ordered as per ids list.
|
53
|
+
```ruby
|
54
|
+
cache_store = CacheStore.new.cache_docs(user_ids, User, [{field_name: 'image_id', klass: UserImage}])
|
55
|
+
users = cache_store.sorted_documents(User, cusrom_user_ids)
|
56
|
+
```
|
26
57
|
|
27
58
|
## Development
|
28
59
|
|
data/lib/mongoid_cache_store.rb
CHANGED
@@ -4,6 +4,7 @@ module MongoidCacheStore
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class CacheStore
|
7
|
+
|
7
8
|
def initialize
|
8
9
|
@doc_ids_hash = {}
|
9
10
|
@related_docs_hash = {}
|
@@ -12,7 +13,7 @@ class CacheStore
|
|
12
13
|
|
13
14
|
# [{field_name: 'image_id', klass: Users::ProfileImage}]
|
14
15
|
def cache_docs(doc_ids, doc_class, related_infos = [])
|
15
|
-
doc_type = doc_class.
|
16
|
+
doc_type = doc_class.to_s.underscore
|
16
17
|
@doc_ids_hash[doc_type] ||= {}
|
17
18
|
[doc_ids].flatten.compact.uniq.each{|doc_id| @doc_ids_hash[doc_type][doc_id.to_s] = nil}
|
18
19
|
@related_docs_hash[doc_type] ||= []
|
@@ -23,22 +24,21 @@ class CacheStore
|
|
23
24
|
|
24
25
|
def document(doc_id, doc_class)
|
25
26
|
return nil if doc_id.blank?
|
26
|
-
doc_type = doc_class.
|
27
|
-
ensure_query(doc_class, doc_id
|
27
|
+
doc_type = doc_class.to_s.underscore
|
28
|
+
ensure_query(doc_class, doc_id)
|
28
29
|
|
29
30
|
@documents[doc_type][doc_id.to_s]
|
30
31
|
end
|
31
32
|
|
32
33
|
def valid_doc_ids(doc_class, doc_ids = nil)
|
33
|
-
doc_type = doc_class.
|
34
|
-
ensure_query(doc_class, doc_ids
|
34
|
+
doc_type = doc_class.to_s.underscore
|
35
|
+
ensure_query(doc_class, doc_ids)
|
35
36
|
|
36
37
|
doc_ids.blank? ? cached_ids(doc_type) : doc_ids.select{|doc_id| @documents[doc_type][doc_id.to_s].present?}
|
37
38
|
end
|
38
39
|
|
39
40
|
def sorted_documents(doc_class, doc_ids = nil)
|
40
|
-
doc_type = doc_class.
|
41
|
-
doc_ids = doc_ids.map(&:to_s) if doc_ids != nil
|
41
|
+
doc_type = doc_class.to_s.underscore
|
42
42
|
ensure_query(doc_class, doc_ids)
|
43
43
|
return [] if @documents[doc_type].blank?
|
44
44
|
|
@@ -56,7 +56,8 @@ class CacheStore
|
|
56
56
|
|
57
57
|
# ensure that there are no missing docs for doc_class
|
58
58
|
def ensure_query(doc_class, doc_ids)
|
59
|
-
|
59
|
+
doc_ids = [doc_ids].flatten.uniq.map(&:to_s)
|
60
|
+
doc_type = doc_class.to_s.underscore
|
60
61
|
|
61
62
|
if doc_ids.present? && (@doc_ids_hash[doc_type].blank? || doc_ids.reject{|doc_id| @doc_ids_hash[doc_type].has_key?(doc_id.to_s)}.present?)
|
62
63
|
cache_docs(doc_ids, doc_class)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_cache_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amit Chaudhary
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
94
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.6.
|
95
|
+
rubygems_version: 2.6.13
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Module to cache documents from belongs_to relationship in a single query
|