redis_app_join 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +6 -10
- data/lib/redis_app_join.rb +27 -3
- data/lib/redis_app_join/version.rb +1 -1
- data/redis_app_join.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0254b1ea1caa6281771b11bb2457c7c27e0e1ca
|
4
|
+
data.tar.gz: f51c7597abee8e79fec7034a9fce12f5f1e1782d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf7863cac7483b98c5bf03b8aa20794d98b7d907a3764e23264ec0e5f578ddc471901a48c045fe5cd3b6121f4b6b71b1af1ce739d3d3398c3eab383861bdc75f
|
7
|
+
data.tar.gz: 35b9feb9220403d25edb0be5402430a15998705ad127c64a6c385562e55fdd62c820ab3e37829cdc6bbff041cd2818ce6bc83af94f204a966f2da0be2fd82e80
|
data/README.md
CHANGED
@@ -33,7 +33,7 @@ In the Ruby class where you need to implement application-side join add `include
|
|
33
33
|
|
34
34
|
```ruby
|
35
35
|
class ReportGen
|
36
|
-
include RedisAppJoin
|
36
|
+
include RedisAppJoin
|
37
37
|
def perform
|
38
38
|
comments = Comment.gte(created_at: Date.yesterday).only(:body, :article_id)
|
39
39
|
cache_records(records: comments)
|
@@ -47,7 +47,7 @@ include RedisAppJoin
|
|
47
47
|
user_ids = fetch_records_field(record_class: 'Article', record_ids: article_ids, field: 'user_id')
|
48
48
|
users = User.in(id: user_ids).only(:name)
|
49
49
|
cache_records(records: users)
|
50
|
-
# => instead of using cached comments we
|
50
|
+
# => instead of using cached comments we could query DB again
|
51
51
|
cached_comments = fetch_records(record_class: 'Comment', record_ids: comment_ids)
|
52
52
|
cached_comments.each do |comment|
|
53
53
|
article = fetch_records(record_class: 'Article', record_ids: [comment.article_id]).first
|
@@ -59,14 +59,6 @@ include RedisAppJoin
|
|
59
59
|
end
|
60
60
|
```
|
61
61
|
|
62
|
-
`cache_records` expects an array of [ActiveModels](http://api.rubyonrails.org/classes/ActiveModel/Model.html). It will loop through them creating keys using combination of class and ID. Hash value will be record's attributes.
|
63
|
-
|
64
|
-
`delete_records` expects an array ActiveModels. You can pass different types of records (users and articles) in the same method call.
|
65
|
-
|
66
|
-
`fetch_records` expects class name and array of IDs. It will return an array of objects and include the original record ID as one of the attributes for each object.
|
67
|
-
|
68
|
-
`fetch_records_field` expects class name, array of IDs and the field name you want. It will return an array of values for that feild.
|
69
|
-
|
70
62
|
Data in Redis will be stored like this:
|
71
63
|
|
72
64
|
```ruby
|
@@ -97,6 +89,10 @@ You can do `article.title` and `user = fetch_records(record_class: 'User', recor
|
|
97
89
|
|
98
90
|
Write tests
|
99
91
|
|
92
|
+
Default TTL of 1.week
|
93
|
+
|
94
|
+
Support JSON structures in caching (getting data from API), not just ActiveModels
|
95
|
+
|
100
96
|
Support non-string fields. For example, if your DB supports array fields you cannot store those attributes in Redis hash values.
|
101
97
|
|
102
98
|
Methods to fetch associated records so we can do `article.user.name` from Redis cache.
|
data/lib/redis_app_join.rb
CHANGED
@@ -2,6 +2,12 @@ require "redis_app_join/version"
|
|
2
2
|
|
3
3
|
module RedisAppJoin
|
4
4
|
|
5
|
+
# will loop through records creating keys using combination of class and ID.
|
6
|
+
# can combine different record types (Users and Articles) in the same method call
|
7
|
+
# record's attributes will be hash fields
|
8
|
+
#
|
9
|
+
# @see https://github.com/dmitrypol/redis_app_join
|
10
|
+
# @param records [Array] ActiveModels to cache
|
5
11
|
def cache_records(records:)
|
6
12
|
records.each do |record|
|
7
13
|
key = [record.class.name, record.id.to_s].join(':')
|
@@ -10,6 +16,10 @@ module RedisAppJoin
|
|
10
16
|
end
|
11
17
|
end
|
12
18
|
|
19
|
+
# used to delete cached records after the process is done
|
20
|
+
# can combine different record types (Users and Articles) in the same method call
|
21
|
+
#
|
22
|
+
# @param records [Array] ActiveModels to delete
|
13
23
|
def delete_records(records:)
|
14
24
|
records.each do |record|
|
15
25
|
key = [record.class.name, record.id.to_s].join(':')
|
@@ -17,10 +27,16 @@ module RedisAppJoin
|
|
17
27
|
end
|
18
28
|
end
|
19
29
|
|
30
|
+
# fetch recors from cache,
|
31
|
+
# cannot combine different record types (Users and Articles) in the same method call
|
32
|
+
#
|
33
|
+
# @param record_class [String] - name of class, used in lookup
|
34
|
+
# @param record_ids [Array] array of IDs to lookup
|
35
|
+
# @return [Array] array of objects and include the original record ID as one of the attributes for each object.
|
20
36
|
def fetch_records(record_class:, record_ids:)
|
21
37
|
output = []
|
22
38
|
record_ids.each do |record_id|
|
23
|
-
key = [record_class
|
39
|
+
key = [record_class, record_id.to_s].join(':')
|
24
40
|
data = REDIS_APP_JOIN.hgetall(key)
|
25
41
|
# => add the key as ID attribute
|
26
42
|
output << OpenStruct.new(data.merge(id: record_id.to_s))
|
@@ -28,14 +44,22 @@ module RedisAppJoin
|
|
28
44
|
return output
|
29
45
|
end
|
30
46
|
|
47
|
+
# retrieves specific field for an array or records (all user_ids for articles)
|
48
|
+
# only returns the field if it's present
|
49
|
+
# cannot combine different record types in the same method call
|
50
|
+
#
|
51
|
+
# @param record_class [String] - name of class, used in lookup
|
52
|
+
# @param record_ids [Array] array of IDs to lookup
|
53
|
+
# @param field [String] name of field/attribute to retrieve
|
54
|
+
# @return [Array] array of unique strings
|
31
55
|
def fetch_records_field(record_class:, record_ids:, field:)
|
32
56
|
output = []
|
33
57
|
record_ids.each do |record_id|
|
34
|
-
key = [record_class
|
58
|
+
key = [record_class, record_id.to_s].join(':')
|
35
59
|
data = REDIS_APP_JOIN.hget(key, field)
|
36
60
|
output << data
|
37
61
|
end
|
38
|
-
return output
|
62
|
+
return output.uniq
|
39
63
|
end
|
40
64
|
|
41
65
|
end
|
data/redis_app_join.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["dmitrypol@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Uses Redis to cache data for application-side joins.}
|
13
|
-
spec.description = %q{
|
13
|
+
spec.description = %q{Uses Redis hashes to cache data for application-side joins instead of building internal own data structures.}
|
14
14
|
spec.homepage = "https://github.com/dmitrypol/redis_app_join"
|
15
15
|
|
16
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_app_join
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Polyakovsky
|
@@ -120,7 +120,8 @@ dependencies:
|
|
120
120
|
- - ">="
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: 1.3.0
|
123
|
-
description:
|
123
|
+
description: Uses Redis hashes to cache data for application-side joins instead of
|
124
|
+
building internal own data structures.
|
124
125
|
email:
|
125
126
|
- dmitrypol@gmail.com
|
126
127
|
executables: []
|