redis_collection 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07e47b0f3e2daa373237c88cd56023e9d9ce2406
4
+ data.tar.gz: 1e744dadcd3429875218d9c7cf3a4fcf2fe0e560
5
+ SHA512:
6
+ metadata.gz: ad0579ff02defb74a9bb335e352ab5cafbd085b8ac3fc2c232c125d914b8476846f8ae1dffb9358c61f48dd814a53b75fcc3d446addad155f6079fc43624c571
7
+ data.tar.gz: d07d7822a1a1b22c4a6c75f9f734855515ba96753ae7d53830cda12b77eb5b583987a6ebfbd18fa329a7e4ddf7f470554b4109b2cb4f325f411f10f3cd5013bd
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.2
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,46 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to creating a positive environment include:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+ * Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
20
+ * Trolling, insulting/derogatory comments, and personal or political attacks
21
+ * Public or private harassment
22
+ * Publishing others' private information, such as a physical or electronic address, without explicit permission
23
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Our Responsibilities
26
+
27
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28
+
29
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ ## Scope
32
+
33
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34
+
35
+ ## Enforcement
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at max@crossfield.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38
+
39
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40
+
41
+ ## Attribution
42
+
43
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44
+
45
+ [homepage]: http://contributor-covenant.org
46
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_collection.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Maxim Chernyak
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # RedisCollection
2
+
3
+ Easily sync an iterable ruby object with a redis namespace.
4
+
5
+ This tool DOES NOT:
6
+
7
+ - integrate with anything
8
+ - help retrieve data back from redis
9
+ - handle redis connection issues
10
+ - make birds in 5 mile radius attack you
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'redis_collection'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install redis_collection
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ books = [
32
+ { 'id' => 1, 'title' => 'Programming Elixir' },
33
+ { 'id' => 2, 'title' => 'Programming Phoenix' }
34
+ ]
35
+
36
+ redis = Redis.new(url: ENV['REDIS_URL'])
37
+ redis_collection = RedisCollection.new(redis, namespace: 'books_')
38
+ redis_collection.sync(books)
39
+ redis.mget('books_1', 'books_2')
40
+ # => ["\x04\b{\aI\"\aid\x06:\x06ETi\x06I\"\ntitle\x06;\x00TI\"\x17Programming Elixir\x06;\x00T", "\x04\b{\aI\"\aid\x06:\x06ETi\aI\"\ntitle\x06;\x00TI\"\x18Programming Phoenix\x06;\x00T"]
41
+ ```
42
+
43
+ By default each object in the collection is serialized via `Marshal.dump` and loaded via `Marshal.load`. Identity of each object is determined by calling `['id']` by default. To change these defaults you can provide procs to `RedisCollection.new`.
44
+
45
+ ```ruby
46
+ redis_collection = RedisCollection.new(redis,
47
+ namespace: 'books_',
48
+ load: -> string { JSON.parse(string) },
49
+ dump: -> object { object.to_json },
50
+ make_key: -> object { object.special_id }
51
+ )
52
+ redis_collection.sync(books)
53
+ ```
54
+
55
+ In the above example objects are converted to/from JSON and must respond to `special_id` to be identified.
56
+
57
+ Method `sync` always overwrites keys that already exist in Redis, and deletes keys that are in Redis but not in collection.
58
+
59
+ ## Benchmark
60
+
61
+ Method `sync` returns a hash of stats containing 3 pieces of information:
62
+
63
+ 1. `:mset_cnt` - how many strings were [mset](http://redis.io/commands/mset)
64
+ 2. `:del_cnt` - how many strings were [del](http://redis.io/commands/del)
65
+ 3. `:time` - the result of `Benchmark.measure {}` on the redis call
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ 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).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/crossfield/redis_collection. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redis_collection"
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
@@ -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,98 @@
1
+ require 'redis_collection/version'
2
+ require 'set'
3
+ require 'benchmark'
4
+
5
+ class RedisCollection
6
+ def initialize(redis,
7
+ namespace: '',
8
+ load: -> string { Marshal.load(string) },
9
+ dump: -> object { Marshal.dump(object) },
10
+ make_key: -> object { object['id'] }
11
+ )
12
+
13
+ @redis = redis
14
+ @namespace = namespace
15
+ @load_proc = load
16
+ @dump_proc = dump
17
+ @make_key_proc = make_key
18
+
19
+ clear_args
20
+ end
21
+
22
+ attr_reader :redis, :namespace, :load_proc, :dump_proc, :make_key_proc
23
+
24
+ ##
25
+ # Sync values under a redis namespace with the given collection by
26
+ #
27
+ # - overwriting existing values
28
+ # - deleting what's not in collection
29
+ #
30
+ def sync(collection)
31
+ clear_args
32
+ updated_namespaced_keys = Set.new
33
+
34
+ each_with_key(collection) do |object, key|
35
+ updated_namespaced_keys << "#{namespace}#{key}"
36
+ add_mset "#{namespace}#{key}", dump(object)
37
+ end
38
+
39
+ redis.scan_each(match: "#{namespace}*") do |namespaced_key|
40
+ unless updated_namespaced_keys.include?(namespaced_key)
41
+ add_del namespaced_key
42
+ end
43
+ end
44
+
45
+ exec!
46
+ end
47
+
48
+ private
49
+
50
+ def exec!
51
+ stats = {
52
+ mset_cnt: @mset_args.size / 2,
53
+ del_cnt: @del_args.size
54
+ }
55
+
56
+ stats[:time] = Benchmark.measure {
57
+ redis.multi do |multi|
58
+ multi.mset(*@mset_args) unless @mset_args.empty?
59
+ multi.del(*@del_args) unless @del_args.empty?
60
+ end
61
+ }
62
+
63
+ stats
64
+ end
65
+
66
+ def each_with_key(collection)
67
+ collection.each do |object|
68
+ yield object, make_key(object)
69
+ end
70
+ end
71
+
72
+ def add_mset(key, value)
73
+ @mset_args << key << value
74
+ end
75
+
76
+ def add_del(key)
77
+ @del_args << key
78
+ end
79
+
80
+ def load(string)
81
+ load_proc.(string)
82
+ end
83
+
84
+ def dump(object)
85
+ dump_proc.(object)
86
+ end
87
+
88
+ def make_key(object)
89
+ make_key_proc.(object)
90
+ end
91
+
92
+ private
93
+
94
+ def clear_args
95
+ @mset_args = []
96
+ @del_args = []
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ class RedisCollection
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_collection/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_collection"
8
+ spec.version = RedisCollection::VERSION
9
+ spec.authors = ["Maxim Chernyak"]
10
+ spec.email = ["max@crossfield.com"]
11
+
12
+ spec.summary = %q{Sync an iterable ruby object with a redis namespace.}
13
+ spec.homepage = "https://github.com/crossfield/redis_collection"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "mock_redis", "~> 0.17"
27
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_collection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Chernyak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-23 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.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
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.17'
69
+ description:
70
+ email:
71
+ - max@crossfield.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/redis_collection.rb
86
+ - lib/redis_collection/version.rb
87
+ - redis_collection.gemspec
88
+ homepage: https://github.com/crossfield/redis_collection
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Sync an iterable ruby object with a redis namespace.
112
+ test_files: []
113
+ has_rdoc: