redis-scripting 1.0.0
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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/lib/redis/scripting.rb +7 -0
- data/lib/redis/scripting/module.rb +48 -0
- data/lib/redis/scripting/script.rb +33 -0
- data/lib/redis/scripting/version.rb +5 -0
- data/redis-scripting.gemspec +26 -0
- data/spec/redis/scripting/module_spec.rb +17 -0
- data/spec/redis/scripting/script_spec.rb +38 -0
- data/spec/redis/scripting/scripts/test1.lua +1 -0
- data/spec/spec_helper.rb +2 -0
- metadata +153 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Brian Palmer
|
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,57 @@
|
|
1
|
+
# Redis::Scripting
|
2
|
+
|
3
|
+
Utilities built on redis-rb for using Redis Lua scripting.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'redis-scripting'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install redis-scripting
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Given a directory of lua scripts, you can create an object to run those
|
22
|
+
scripts with:
|
23
|
+
|
24
|
+
> redis = Redis.new()
|
25
|
+
> scripts = Redis::Scripting::Module.new(redis, "/path/to/myscripts")
|
26
|
+
|
27
|
+
Then you can run a script "do_work.lua" with:
|
28
|
+
|
29
|
+
> scripts.run(:do_work, [keys], [argv])
|
30
|
+
|
31
|
+
See http://redis.io/commands/eval for details on the keys and argv
|
32
|
+
params, along with more details on lua in redis. To save bandwidth this
|
33
|
+
will use evalsha to run the script, and fallback to eval if the script
|
34
|
+
is not yet loaded in redis.
|
35
|
+
|
36
|
+
If your scripts directory "myscripts" has a "myscripts/includes"
|
37
|
+
subdirectory, any .lua files in that subdirectory will be prepended to
|
38
|
+
every .lua script in "myscripts" as a primitive form of extracting
|
39
|
+
library code. For instance if your folder structure is:
|
40
|
+
|
41
|
+
- myscripts/
|
42
|
+
- script1.lua
|
43
|
+
- script2.lua
|
44
|
+
- includes/
|
45
|
+
- common.lua
|
46
|
+
- more.lua
|
47
|
+
|
48
|
+
Then script1 and script2 will automatically have common.lua and then
|
49
|
+
more.lua prepended to it.
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "redis"
|
2
|
+
require "redis/scripting/script"
|
3
|
+
|
4
|
+
class Redis
|
5
|
+
module Scripting
|
6
|
+
|
7
|
+
class Module
|
8
|
+
attr_reader :source_dir
|
9
|
+
attr_accessor :redis
|
10
|
+
|
11
|
+
def initialize(redis, source_dir, opts = {})
|
12
|
+
@redis = redis
|
13
|
+
@source_dir = source_dir
|
14
|
+
@scripts = {}
|
15
|
+
load_scripts()
|
16
|
+
end
|
17
|
+
|
18
|
+
def run(script_name, keys, argv)
|
19
|
+
script = @scripts[script_name.to_s]
|
20
|
+
raise(ArgumentError, "unknown script: #{script_name}") unless script
|
21
|
+
|
22
|
+
script.run(redis, keys, argv)
|
23
|
+
end
|
24
|
+
|
25
|
+
def inspect
|
26
|
+
%{<%s: 0x%x @source_dir="%s" @redis=%s>} % [self.class, object_id, source_dir, redis]
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def load_scripts
|
32
|
+
headers = Dir.glob(File.join(source_dir, "includes", "*.lua")).map { |include_name|
|
33
|
+
File.read(include_name)
|
34
|
+
}
|
35
|
+
|
36
|
+
if !headers.empty?
|
37
|
+
header_source = headers.join("\n\n") + "\n\n"
|
38
|
+
end
|
39
|
+
|
40
|
+
Dir.glob(File.join(source_dir, "*.lua")).each do |filename|
|
41
|
+
script = Redis::Scripting::Script.new(filename, script_header: header_source)
|
42
|
+
@scripts[script.name] = script
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
class Redis
|
4
|
+
module Scripting
|
5
|
+
|
6
|
+
class Script
|
7
|
+
attr_reader :source_filename, :source, :name
|
8
|
+
|
9
|
+
def initialize(source_filename, opts = {})
|
10
|
+
@source_filename = source_filename
|
11
|
+
@source = File.read(source_filename)
|
12
|
+
if opts[:script_header]
|
13
|
+
@source = "#{opts[:script_header]}\n\n#{@source}"
|
14
|
+
end
|
15
|
+
@name = File.basename(source_filename, ".lua")
|
16
|
+
end
|
17
|
+
|
18
|
+
def run(redis, keys, argv)
|
19
|
+
begin
|
20
|
+
redis.evalsha(self.sha, keys: keys, argv: argv)
|
21
|
+
rescue Redis::CommandError => err
|
22
|
+
raise unless err.message.start_with?("NOSCRIPT")
|
23
|
+
redis.eval(self.source, keys: keys, argv: argv)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def sha
|
28
|
+
@sha ||= OpenSSL::Digest::SHA1.hexdigest(source)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -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 'redis/scripting/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redis-scripting"
|
8
|
+
spec.version = Redis::Scripting::VERSION
|
9
|
+
spec.authors = ["Brian Palmer"]
|
10
|
+
spec.email = ["brianp@instructure.com"]
|
11
|
+
spec.summary = %q{Utilities built on redis-rb for using Redis Lua scripting.}
|
12
|
+
spec.homepage = "https://github.com/codekitchen/redis-rb-scripting"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "redis", ">= 3.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "guard-rspec"
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Scripting::Module do
|
4
|
+
let(:redis) { double('Redis') }
|
5
|
+
let(:source_dir) { File.dirname(__FILE__) + "/scripts" }
|
6
|
+
let(:subject) { Redis::Scripting::Module.new(redis, source_dir) }
|
7
|
+
|
8
|
+
its(:source_dir) { should == source_dir }
|
9
|
+
its(:redis) { should == redis }
|
10
|
+
|
11
|
+
describe "#run" do
|
12
|
+
it "should raise on unknown" do
|
13
|
+
expect { subject.run(:whut, [], []) }.to raise_error(ArgumentError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Scripting::Script do
|
4
|
+
let(:source_filename) { File.dirname(__FILE__) + "/scripts/test1.lua" }
|
5
|
+
|
6
|
+
context "no header" do
|
7
|
+
let(:subject) { Redis::Scripting::Script.new(source_filename) }
|
8
|
+
|
9
|
+
its(:name) { should == "test1" }
|
10
|
+
its(:source) { should == File.read(source_filename) }
|
11
|
+
its(:sha) { should == OpenSSL::Digest::SHA1.hexdigest(subject.source) }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "header" do
|
15
|
+
let(:header) { "local x = 5" }
|
16
|
+
let(:subject) { Redis::Scripting::Script.new(source_filename, script_header: header) }
|
17
|
+
|
18
|
+
its(:source) { should == "#{header}\n\n#{File.read(source_filename)}" }
|
19
|
+
its(:sha) { should == OpenSSL::Digest::SHA1.hexdigest(subject.source) }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#run" do
|
23
|
+
let(:subject) { Redis::Scripting::Script.new(source_filename) }
|
24
|
+
|
25
|
+
it "should evalsha first" do
|
26
|
+
redis = double('Redis')
|
27
|
+
expect(redis).to receive(:evalsha).with(subject.sha, keys: ['a'], argv: ['b']) { 1 }
|
28
|
+
subject.run(redis, ['a'], ['b']).should == 1
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should eval if evalsha raises NOSCRIPT" do
|
32
|
+
redis = double('Redis')
|
33
|
+
expect(redis).to receive(:evalsha) { raise(Redis::CommandError, "NOSCRIPT doesn't exist") }
|
34
|
+
expect(redis).to receive(:eval).with(subject.source, keys: ['a'], argv: ['b']) { 1 }
|
35
|
+
subject.run(redis, ['a'], ['b']).should == 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
return 1
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-scripting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Palmer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description:
|
95
|
+
email:
|
96
|
+
- brianp@instructure.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- .travis.yml
|
104
|
+
- Gemfile
|
105
|
+
- Guardfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/redis/scripting.rb
|
110
|
+
- lib/redis/scripting/module.rb
|
111
|
+
- lib/redis/scripting/script.rb
|
112
|
+
- lib/redis/scripting/version.rb
|
113
|
+
- redis-scripting.gemspec
|
114
|
+
- spec/redis/scripting/module_spec.rb
|
115
|
+
- spec/redis/scripting/script_spec.rb
|
116
|
+
- spec/redis/scripting/scripts/test1.lua
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
homepage: https://github.com/codekitchen/redis-rb-scripting
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
hash: -614399944501012724
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
segments:
|
141
|
+
- 0
|
142
|
+
hash: -614399944501012724
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 1.8.23
|
146
|
+
signing_key:
|
147
|
+
specification_version: 3
|
148
|
+
summary: Utilities built on redis-rb for using Redis Lua scripting.
|
149
|
+
test_files:
|
150
|
+
- spec/redis/scripting/module_spec.rb
|
151
|
+
- spec/redis/scripting/script_spec.rb
|
152
|
+
- spec/redis/scripting/scripts/test1.lua
|
153
|
+
- spec/spec_helper.rb
|