redis_app_join 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 443ea24957f1e3e8ea260db70ac1206aefee6be9
4
+ data.tar.gz: be5d4b45715e15ef856fb3bd3ce0f92fc183baff
5
+ SHA512:
6
+ metadata.gz: 3c649246ab9a2770fd5570877bc5490aafe8b86d7880f192a5bc4a5ddbb7d00d0fcf134e035a1ef16e7e1db6d5d231892ba7272d8b3fd56fbd801bd4b584ac1a
7
+ data.tar.gz: 8c747b2de354770ebb916a23a75e51b5a078b48b56cea9ac866971591b036b34a4c3ec75cd171be00269f08eedca35934d08bde93a4f514d7ccd92402e57f80a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_app_join.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Dmitry Polyakovsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # RedisAppJoin
2
+
3
+ Sometimes we need to implement application level joins. It is easy to query User table and get list of user_ids and then query the child record table for records that belong to those users. But what if we need to combine data attributes from both tables? This can also be a use case when querying mutliple databases or 3rd party APIs.
4
+
5
+ You can use Redis Hashes as a place to cache data needed as you are looping through records. Warning - this is ALPHA quality software, be careful before running it in production.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'redis_app_join'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install redis_app_join
22
+
23
+ ## Usage
24
+
25
+ Create config/initializers/redis_app_join.rb or place this in environment specific config file. You can use a different namespace, DB, driver, etc.
26
+
27
+ ```ruby
28
+ redis_conn = Redis.new(host: 'localhost', port: 6379, db: 0)
29
+ REDIS_APP_JOIN = Redis::Namespace.new(:appjoin, redis: redis_conn)
30
+ ```
31
+
32
+ In the Ruby class where you need to implement application-side join add `include RedisAppJoin`. Here is a sample report generator that will produce a report of comments created since yesterday and include associated article title and name of user who wrote the article.
33
+
34
+ ```ruby
35
+ class ReportGen
36
+ include RedisAppJoin
37
+ def perform
38
+ comments = Comment.gte(created_at: Date.yesterday).only(:body, :article_id)
39
+ cache_records(records: comments)
40
+ comment_ids = comments.pluck(:id)
41
+ # =>
42
+ # => we also could have done comments.pluck(:article_id)
43
+ article_ids = fetch_records_field(record_class: 'Comment', record_ids: comment_ids, field: 'article_id')
44
+ articles = Article.in(id: article_ids).only(:title, :user_id)
45
+ cache_records(records: articles)
46
+ # =>
47
+ user_ids = fetch_records_field(record_class: 'Article', record_ids: article_ids, field: 'user_id')
48
+ users = User.in(id: user_ids).only(:name)
49
+ cache_records(records: users)
50
+ # => instead of using cached comments we just query DB again
51
+ cached_comments = fetch_records(record_class: 'Comment', record_ids: comment_ids)
52
+ cached_comments.each do |comment|
53
+ article = fetch_records(record_class: 'Article', record_ids: [comment.article_id]).first
54
+ user = fetch_records(record_class: 'User', record_ids: [article.user_id]).first
55
+ puts [comment.body, article.title, user.name].join(',')
56
+ end
57
+ delete_records(records: comments + articles + users)
58
+ end
59
+ end
60
+ ```
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
+ Data in Redis will be stored like this:
71
+
72
+ ```ruby
73
+ {"db":0,"key":"appjoin:Comment:id1","ttl":-1,"type":"hash","value":{"body":"body 1","article_id":"id1"},...}
74
+ {"db":0,"key":"appjoin:Comment:id2","ttl":-1,"type":"hash","value":{"body":"body 2","article_id":"id2"},...}
75
+ ...
76
+ {"db":0,"key":"appjoin:Article:id1","ttl":-1,"type":"hash","value":{"title":"title 1","user_id":"id1"},...}
77
+ {"db":0,"key":"appjoin:Article:id2","ttl":-1,"type":"hash","value":{"title":"title 2","user_id":"id2"},...}
78
+ ...
79
+ {"db":0,"key":"appjoin:User:id1","ttl":-1,"type":"hash","value":{"name":"user 1"}, ...}
80
+ {"db":0,"key":"appjoin:User:id2","ttl":-1,"type":"hash","value":{"name":"user 2"}, ...}
81
+ ```
82
+
83
+ Comment, Article and User records will be returned like this.
84
+
85
+ ```ruby
86
+ # comment
87
+ <OpenStruct article_id="id1", body="body 1", id="id1">
88
+ # article
89
+ <OpenStruct user_id="id1", title="title 1", id="id1">
90
+ # user
91
+ <OpenStruct name="user 1", id="id1">
92
+ ```
93
+
94
+ You can do `article.title` and `user = fetch_records(record_class: 'User', record_ids: [article.user_id]).first`.
95
+
96
+ ### TODO:
97
+
98
+ Write tests
99
+
100
+ Support non-string fields. For example, if your DB supports array fields you cannot store those attributes in Redis hash values.
101
+
102
+ Methods to fetch associated records so we can do `article.user.name` from Redis cache.
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dmitrypol/redis_app_join.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redis_app_join"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module RedisAppJoin
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,41 @@
1
+ require "redis_app_join/version"
2
+
3
+ module RedisAppJoin
4
+
5
+ def cache_records(records:)
6
+ records.each do |record|
7
+ key = [record.class.name, record.id.to_s].join(':')
8
+ data = record.attributes.except(:_id, :id)
9
+ REDIS_APP_JOIN.mapped_hmset(key, data)
10
+ end
11
+ end
12
+
13
+ def delete_records(records:)
14
+ records.each do |record|
15
+ key = [record.class.name, record.id.to_s].join(':')
16
+ REDIS_APP_JOIN.del(key)
17
+ end
18
+ end
19
+
20
+ def fetch_records(record_class:, record_ids:)
21
+ output = []
22
+ record_ids.each do |record_id|
23
+ key = [record_class.titleize, record_id.to_s].join(':')
24
+ data = REDIS_APP_JOIN.hgetall(key)
25
+ # => add the key as ID attribute
26
+ output << OpenStruct.new(data.merge(id: record_id.to_s))
27
+ end
28
+ return output
29
+ end
30
+
31
+ def fetch_records_field(record_class:, record_ids:, field:)
32
+ output = []
33
+ record_ids.each do |record_id|
34
+ key = [record_class.titleize, record_id.to_s].join(':')
35
+ data = REDIS_APP_JOIN.hget(key, field)
36
+ output << data
37
+ end
38
+ return output
39
+ end
40
+
41
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_app_join/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_app_join"
8
+ spec.version = RedisAppJoin::VERSION
9
+ spec.authors = ["Dmitry Polyakovsky"]
10
+ spec.email = ["dmitrypol@gmail.com"]
11
+
12
+ spec.summary = %q{Uses Redis to cache data for application-side joins.}
13
+ spec.description = %q{Instead of building your own data structures why not use Redis Hashes.}
14
+ spec.homepage = "https://github.com/dmitrypol/redis_app_join"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.12"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ # =>
34
+ spec.add_development_dependency 'mock_redis', '~> 0.17', '>= 0.17.0'
35
+ # =>
36
+ spec.add_runtime_dependency 'redis', '~> 3.3'
37
+ spec.add_runtime_dependency 'redis-namespace', '~> 1.5'
38
+ spec.add_runtime_dependency 'readthis', '~> 1.3', '>= 1.3.0'
39
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_app_join
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Polyakovsky
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mock_redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.17'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 0.17.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '0.17'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.17.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: redis
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.3'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.3'
89
+ - !ruby/object:Gem::Dependency
90
+ name: redis-namespace
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.5'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.5'
103
+ - !ruby/object:Gem::Dependency
104
+ name: readthis
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.3'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.3.0
113
+ type: :runtime
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '1.3'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 1.3.0
123
+ description: Instead of building your own data structures why not use Redis Hashes.
124
+ email:
125
+ - dmitrypol@gmail.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - ".gitignore"
131
+ - ".rspec"
132
+ - ".travis.yml"
133
+ - Gemfile
134
+ - LICENSE
135
+ - README.md
136
+ - Rakefile
137
+ - bin/console
138
+ - bin/setup
139
+ - lib/redis_app_join.rb
140
+ - lib/redis_app_join/version.rb
141
+ - redis_app_join.gemspec
142
+ homepage: https://github.com/dmitrypol/redis_app_join
143
+ licenses: []
144
+ metadata:
145
+ allowed_push_host: https://rubygems.org
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.5.1
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Uses Redis to cache data for application-side joins.
166
+ test_files: []