shapkeep 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +1 -0
- data/lib/shapkeep.rb +60 -0
- data/lib/shapkeep/version.rb +3 -0
- data/lib/shapkeep/wrapper.rb +22 -0
- data/sha_file.yml +5 -0
- data/shapkeep.gemspec +26 -0
- data/spec/fixtures/store.yml +5 -0
- data/spec/fixtures/store_error.yml +7 -0
- data/spec/shapkeep/wrapper_spec.rb +22 -0
- data/spec/shapkeep_spec.rb +57 -0
- data/spec/spec_helper.rb +20 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e5e37117c826802b5ebe29ba7f9fbb0525715c6
|
4
|
+
data.tar.gz: 8b23acf69f30e5ebcb2f9aba40d95d8ba5925f38
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7882ad2314d0e4efcf81838f4eba8465c4b64634e7ec786780c0e9410bf20bc3b937e6ccafcb7cc9ebe2194cc25a8345dbfe7a370e90bd4890d7a372f9a450f4
|
7
|
+
data.tar.gz: 523ae2407eb0fd8d5b4873032b5844f58491636a933cfa41c4139d25aefc8a04fef4c6a5c732d11bb3c4180a609ab4b445a683ca7bc94ac047a11da6dd376292
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 jake
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Shapkeep
|
2
|
+
|
3
|
+
Shapkeep keeps track of your Redis Lua script SHA's so you don't have to.
|
4
|
+
|
5
|
+
Shapkeep will optimize your Redis EVAL calls by always attempting to use
|
6
|
+
EVALSHA.
|
7
|
+
|
8
|
+
If we get a `NOSCRIPT` error, we load the script, and then `retry`.
|
9
|
+
|
10
|
+
Next time, we won't get a `NOSCRIPT`, and so it's Optimized™.
|
11
|
+
|
12
|
+
Shapkeep also gives you a central repo of Lua scripts in the form of a YAML
|
13
|
+
file that you put them all in. The advantages of this are debatable, but for right
|
14
|
+
now I like it.
|
15
|
+
|
16
|
+
It does allow us to eval scripts by name:
|
17
|
+
|
18
|
+
Shapkeep.new('/path/to/store.yml').eval(redis, :name)
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'shapkeep'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install shapkeep
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Shapkeep has two ways of using it, as a separate object and as a wrapper of
|
37
|
+
your Redis object
|
38
|
+
|
39
|
+
As a separate object:
|
40
|
+
|
41
|
+
redis = Redis.new
|
42
|
+
shapkeep = Shapkeep.new('/path/to/store.yml')
|
43
|
+
|
44
|
+
shapkeep.eval(redis, :script__name)
|
45
|
+
|
46
|
+
|
47
|
+
Or as a wrapper (uses SimpleDelegator)
|
48
|
+
|
49
|
+
redis = Shapkeep::Wrapper.new('/path/to/store.yml', redis)
|
50
|
+
|
51
|
+
redis.keys # => []
|
52
|
+
|
53
|
+
redis.eval_script(:one) # => 1
|
54
|
+
|
55
|
+
`Shapkeep#eval` and `Shapkeep::Wrapper#eval_script` both accept the `keys` and
|
56
|
+
`args` arguments as arrays.
|
57
|
+
|
58
|
+
redis.eval_script(:one, ['key1'], ['arg1'])
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/shapkeep.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'shapkeep/version'
|
3
|
+
|
4
|
+
class Shapkeep
|
5
|
+
InvalidScriptNameError = Class.new(StandardError)
|
6
|
+
|
7
|
+
attr_reader :sha_file
|
8
|
+
|
9
|
+
def initialize(sha_file)
|
10
|
+
@sha_file = sha_file
|
11
|
+
end
|
12
|
+
|
13
|
+
def eval(redis, name, keys = [], args = [])
|
14
|
+
begin
|
15
|
+
invalid_script_name!(name) unless script_available?(name)
|
16
|
+
redis.evalsha(sha_for_script(name), keys, args)
|
17
|
+
rescue Redis::CommandError => boom
|
18
|
+
raise boom unless boom.message.include?('NOSCRIPT')
|
19
|
+
|
20
|
+
load_script(redis, name)
|
21
|
+
retry
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def load_scripts!(redis)
|
26
|
+
script_store.keys.each { |name| load_script(redis, name) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def script_store
|
30
|
+
@script_store ||= YAML.load_file(sha_file)
|
31
|
+
end
|
32
|
+
|
33
|
+
def script_available?(name)
|
34
|
+
script_store.has_key?(name)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def sha_for_script(name)
|
40
|
+
script_store[name.to_sym][:sha] ||= sha_script(name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def sha_script(name)
|
44
|
+
Digest::SHA1.hexdigest(script_for_name(name))
|
45
|
+
end
|
46
|
+
|
47
|
+
def script_for_name(name)
|
48
|
+
script_store[name.to_sym][:script]
|
49
|
+
end
|
50
|
+
|
51
|
+
def load_script(redis, name)
|
52
|
+
sha = redis.script(:load, script_for_name(name))
|
53
|
+
script_store[name][:sha] = sha
|
54
|
+
end
|
55
|
+
|
56
|
+
def invalid_script_name!(name)
|
57
|
+
raise InvalidScriptNameError.new("No script with name [ #{name} ].")
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'shapkeep'
|
3
|
+
|
4
|
+
class Shapkeep
|
5
|
+
class Wrapper < SimpleDelegator
|
6
|
+
|
7
|
+
attr_reader :shapkeep
|
8
|
+
|
9
|
+
def initialize(script_file, redis)
|
10
|
+
super(redis)
|
11
|
+
@shapkeep = Shapkeep.new(script_file)
|
12
|
+
end
|
13
|
+
|
14
|
+
def eval_script(script_name, keys = [], args = [])
|
15
|
+
shapkeep.eval(redis, script_name, keys, args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def redis
|
19
|
+
__getobj__
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/sha_file.yml
ADDED
data/shapkeep.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'shapkeep/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "shapkeep"
|
8
|
+
spec.version = Shapkeep::VERSION
|
9
|
+
spec.authors = ['j-wilkins']
|
10
|
+
spec.email = ['jake@jakewilkins.com']
|
11
|
+
spec.description = %q{EVAL Lua scripts in Redis by name}
|
12
|
+
spec.summary = %q{EVAL Lua scripts in Redis by name}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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_runtime_dependency 'redis', '~> 3'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shapkeep::Wrapper do
|
4
|
+
let(:redis) { Redis.new }
|
5
|
+
subject { Shapkeep::Wrapper.new('spec/fixtures/store.yml', redis) }
|
6
|
+
|
7
|
+
describe 'shapkeep' do
|
8
|
+
specify { expect(subject.shapkeep).to be_instance_of(Shapkeep) }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'eval_script' do
|
12
|
+
specify { expect(subject.respond_to?(:eval_script)).to be_true }
|
13
|
+
specify { expect(subject.eval_script(:zero)).to be(0) }
|
14
|
+
specify { expect(subject.eval_script(:one)).to be(1) }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'delegating to redis' do
|
18
|
+
specify { expect(subject.keys).to be_instance_of(Array) }
|
19
|
+
specify { expect(subject.set('asdf', 'yupyupyup')).to eq('OK') }
|
20
|
+
specify { expect(subject.del('asdf')).to be_instance_of(Fixnum) }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shapkeep do
|
4
|
+
let(:redis) { Redis.new }
|
5
|
+
subject { Shapkeep.new('spec/fixtures/store.yml') }
|
6
|
+
|
7
|
+
context 'script store' do
|
8
|
+
specify { expect(subject.script_store.keys).to eq([:zero, :one]) }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'script available' do
|
12
|
+
specify { expect(subject.script_available?(:nope)).to be_false }
|
13
|
+
specify { expect(subject.script_available?(:zero)).to be_true }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'load scripts' do
|
17
|
+
before { redis.script(:flush); subject.load_scripts!(redis) }
|
18
|
+
specify do
|
19
|
+
expect(redis.script(:exists, [subject.send(:sha_for_script, :zero),
|
20
|
+
subject.send(:sha_for_script, :one)])).to be_true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'eval' do
|
25
|
+
context 'invalid script name' do
|
26
|
+
specify do
|
27
|
+
expect(Proc.new { subject.eval(nil, :nope) }).to(
|
28
|
+
raise_error(Shapkeep::InvalidScriptNameError))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'script not loaded' do
|
33
|
+
before { redis.script(:flush) }
|
34
|
+
specify { expect(subject.eval(redis, :zero)).to eq(0) }
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'script loaded' do
|
38
|
+
before do
|
39
|
+
redis.script(:load, subject.script_store[:one][:script])
|
40
|
+
def subject.load_script(*args)
|
41
|
+
raise 'Tried to Load a script #wtf!'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
specify do
|
45
|
+
expect(subject.eval(redis, :one)).to be(1)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'script errors' do
|
50
|
+
subject { Shapkeep.new('spec/fixtures/store_error.yml') }
|
51
|
+
specify do
|
52
|
+
expect(Proc.new { subject.eval(redis, :error) }).to(
|
53
|
+
raise_error(Redis::CommandError))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'shapkeep/wrapper'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shapkeep
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- j-wilkins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: EVAL Lua scripts in Redis by name
|
70
|
+
email:
|
71
|
+
- jake@jakewilkins.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/shapkeep.rb
|
83
|
+
- lib/shapkeep/version.rb
|
84
|
+
- lib/shapkeep/wrapper.rb
|
85
|
+
- sha_file.yml
|
86
|
+
- shapkeep.gemspec
|
87
|
+
- spec/fixtures/store.yml
|
88
|
+
- spec/fixtures/store_error.yml
|
89
|
+
- spec/shapkeep/wrapper_spec.rb
|
90
|
+
- spec/shapkeep_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: ''
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.0.4
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: EVAL Lua scripts in Redis by name
|
116
|
+
test_files:
|
117
|
+
- spec/fixtures/store.yml
|
118
|
+
- spec/fixtures/store_error.yml
|
119
|
+
- spec/shapkeep/wrapper_spec.rb
|
120
|
+
- spec/shapkeep_spec.rb
|
121
|
+
- spec/spec_helper.rb
|