redikey 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1669a8286c4332c77e88855c63d9b2f6d2c328bc
4
+ data.tar.gz: e3fa7013b47fcac467fe908d4d8498dee616411d
5
+ SHA512:
6
+ metadata.gz: 3f44fae1a34958cc3a04606c031dff64c3b4007172c8796e66cce13bd19725c6aaaf55e84924480045c925abf4ee6d7d854a5d3c713015d0e7dbed54581066d2
7
+ data.tar.gz: ca63fe1a5855fdc953d6ea14c5250127068f2d4c7dda9fd0ae55e7140199d96611e82e00c3aab085979fea20cd41460995fb84f4c75cfd19e8ae676d3cd42ed7
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redikey.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Carl Furrow
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,91 @@
1
+ # Redikey
2
+
3
+ Redikey encapsulates the Redis hash key pattern [defined here](http://instagram-engineering.tumblr.com/post/12202313862/storing-hundreds-of-millions-of-simple-key-value).
4
+
5
+ The most common usage was to group values into common key prefixes ('milestones' or 'str_report', etc) and then taking the user_id of the user we want to store or retrieve data in redis for (e.g. 5000000) and then making the hash key by taking the user_id and dividing by 512, which would become: `milestones:9765`.
6
+
7
+ Inside that hash key, we'd create a field based again on the user's user_id, but this time, instead of dividing, we mod: `320`
8
+
9
+ So the redis operation to set a value for user 5000000 would be:
10
+
11
+ ```
12
+ redis.hset('milestones:9765', '320', some_value)
13
+ ```
14
+
15
+ If the user's user_id was 5000001, the redis operation would look like this:
16
+
17
+ ```
18
+ redis.hset('milestones:9765', '321', some_other_value)
19
+ ```
20
+
21
+ Redikey simplifies this math, and creates a simple api to retrieve those keys for you.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'redikey'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install redikey
38
+
39
+ ## Usage
40
+
41
+ To initialize the Redikey KeyHelper:
42
+
43
+ `Redikey::KeyHelper.new(resource_id, separator: ':', prefixes: [])`
44
+
45
+ - **Required** The resource_id (e.g. a user_id)
46
+ - *Optional* separator: A separator to use between prefixes or parts of the key (defaults to ':')
47
+ - *Optional* prefixes: An array of strings that will become prefixes for your key. (defaults to [])
48
+
49
+
50
+ ```
51
+ redikey = Redikey::KeyHelper.new(1, separator: '/', prefixes: [:prefix1, :prefix2, :prefixn])
52
+
53
+ redikey.key #=> 'prefix1/prefix2/prefixn/0'
54
+ redikey.field_key => '1'
55
+ ```
56
+
57
+ Without any prefixes
58
+ ```
59
+ redikey = Redikey::KeyHelper.new(1)
60
+ redikey.key #=> '0'
61
+ redikey.field_key #=> '1'
62
+ ```
63
+
64
+ You can also add an additional prefix that will be added to the end of any existing prefixes:
65
+ ```
66
+ redikey.key(:foo) #=> 'foo:0'
67
+
68
+ redikey = Redikey::KeyHelper.new(1, prefixes: [:prefix1, :prefix2, :prefixn])
69
+ redikey.key(:foo) #=> 'prefix1:prefix2:prefixn:foo:0'
70
+ ```
71
+
72
+ Now that you have the keys you need for the hash, and the field, you can use Redis' `hset` command:
73
+
74
+ ```
75
+ redis.hset(redikey.key, redikey.field_key, some_value)
76
+ ```
77
+
78
+ And to read from the hash:
79
+
80
+ ```
81
+ redis.hget(redikey.key, redikey.field_key)
82
+ ```
83
+
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it ( https://github.com/[my-github-username]/redikey/fork )
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,5 @@
1
+ require "redikey/version"
2
+ require 'redikey/key_helper'
3
+
4
+ module Redikey
5
+ end
@@ -0,0 +1,26 @@
1
+ module Redikey
2
+ class KeyHelper
3
+ DEFAULT_HASH_SIZE = 512
4
+ attr_reader :resource_id, :prefixes, :separator
5
+
6
+ def initialize(resource_id, separator: ':', prefixes: [])
7
+ @resource_id = resource_id
8
+ @prefixes = prefixes
9
+ @separator = separator
10
+ end
11
+
12
+ def key(appended_prefix = nil)
13
+ (prefixes + [appended_prefix, sharded_key]).compact.join(separator)
14
+ end
15
+
16
+ def field_key
17
+ resource_id % DEFAULT_HASH_SIZE
18
+ end
19
+
20
+ private
21
+
22
+ def sharded_key
23
+ (resource_id / DEFAULT_HASH_SIZE).to_i
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Redikey
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redikey/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redikey"
8
+ spec.version = Redikey::VERSION
9
+ spec.authors = ["Carl Furrow"]
10
+ spec.email = ["carl.furrow@gmail.com"]
11
+ spec.summary = %q{Simplify creation of storage-efficient redis hash keys}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ module Redikey
4
+ describe KeyHelper do
5
+ let(:user_id) { 999 }
6
+ let(:prefixes) { [] }
7
+ let(:redikey) { described_class.new(user_id, prefixes: prefixes) }
8
+
9
+ describe '#key' do
10
+
11
+ subject { redikey.key }
12
+
13
+ context 'without a prefix' do
14
+ it 'returns the sharded key' do
15
+ expect(subject).to eq( sharded_key )
16
+ end
17
+ end
18
+
19
+ context 'with a prefix' do
20
+ let(:prefixes) { [:foo, :bar] }
21
+
22
+ it 'returns the sharded key with a prefix' do
23
+ expect(subject).to eq( "foo:bar:#{sharded_key}" )
24
+ end
25
+
26
+ context 'with additional prefix' do
27
+ subject { redikey.key('baz') }
28
+
29
+ it 'appends the additional prefix to the key' do
30
+ expect(subject).to eq("foo:bar:baz:#{sharded_key}")
31
+ end
32
+ end
33
+ end
34
+
35
+ context 'with additional prefix' do
36
+ subject { redikey.key('baz') }
37
+
38
+ it 'appends the additional prefix to the key' do
39
+ expect(subject).to eq("baz:#{sharded_key}")
40
+ end
41
+ end
42
+
43
+ context 'when separator is /' do
44
+ let(:redikey) { described_class.new(user_id, separator: '/', prefixes: prefixes) }
45
+ let(:prefixes) { [:foo, :bar] }
46
+
47
+ it 'uses the given separator' do
48
+ expect(subject).to eq("foo/bar/#{sharded_key}")
49
+ end
50
+ end
51
+ end
52
+
53
+ describe '#field_key' do
54
+ subject { redikey.field_key }
55
+
56
+ it 'returns the resource key, mod the default hash size' do
57
+ expect(subject).to eq( sharded_field_key )
58
+ end
59
+ end
60
+
61
+ def sharded_key
62
+ (user_id / described_class::DEFAULT_HASH_SIZE).to_s
63
+ end
64
+
65
+ def sharded_field_key
66
+ user_id % described_class::DEFAULT_HASH_SIZE
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'redikey'
5
+
6
+ RSpec.configure do |config|
7
+ config.color = true
8
+ config.tty = true
9
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redikey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carl Furrow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-17 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: ''
56
+ email:
57
+ - carl.furrow@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/redikey.rb
68
+ - lib/redikey/key_helper.rb
69
+ - lib/redikey/version.rb
70
+ - redikey.gemspec
71
+ - spec/redikey/key_helper_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Simplify creation of storage-efficient redis hash keys
97
+ test_files:
98
+ - spec/redikey/key_helper_spec.rb
99
+ - spec/spec_helper.rb
100
+ has_rdoc: