redis-lua 0.1.0
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 +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +7 -0
- data/lib/redis/lua.rb +57 -0
- data/redis-lua.gemspec +26 -0
- data/spec/lua_spec.rb +106 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/support/scripts/one.lua +1 -0
- data/spec/support/scripts/two +1 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a6d170cfb2288e3cd04b8bb5963f5e6e4c6056a7
|
4
|
+
data.tar.gz: e250f67f25adf641d2734c242db5bbea5fb5e065
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4e94191e274b5f235a6e0a652175eff40a57b1d5e78581be0eda3db7ffecea57c22a60e875c6c8b106a969f503c2e6e877224d864c41f6d95734887344bf5ea
|
7
|
+
data.tar.gz: d9bf3e4805d4c3076373152cada03467d0d4e112b64500226108d265cba36a4857a4871dff79ed40ec703d04dd18aca2ae411d53a4d0f10e061a0e89562991ca
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chris Hanks
|
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,43 @@
|
|
1
|
+
# Redis::Lua
|
2
|
+
|
3
|
+
Adds a few methods to Ruby's Redis class to make handling Lua scripts a bit easier.
|
4
|
+
|
5
|
+
# When your application initializes:
|
6
|
+
$redis = Redis.new
|
7
|
+
$redis.register_script(:my_script, "return 1")
|
8
|
+
|
9
|
+
# Later...
|
10
|
+
$redis.run_script(:my_script) #=> 1
|
11
|
+
|
12
|
+
You can also put scripts in their own files:
|
13
|
+
|
14
|
+
# scripts/my_file_script.lua
|
15
|
+
return 3
|
16
|
+
|
17
|
+
# In your app:
|
18
|
+
$redis.register_script_files('./scripts/*')
|
19
|
+
$redis.run_script(:my_file_script) #=> 3
|
20
|
+
|
21
|
+
This lets you manage your Lua scripts in version control with the rest of your app, so you don't need to worry about loading them manually. If Redis' script cache is lost, they'll be automatically reloaded as needed.
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
gem 'redis-lua'
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install redis-lua
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/redis/lua.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'digest/sha1'
|
3
|
+
|
4
|
+
class Redis
|
5
|
+
module Lua
|
6
|
+
Version = '0.1.0'
|
7
|
+
|
8
|
+
def run_script(name, *args)
|
9
|
+
raise "Nonexistent Lua script!" unless s = scripts.find { |script| script.name == name }
|
10
|
+
|
11
|
+
case @client
|
12
|
+
when Client
|
13
|
+
begin
|
14
|
+
evalsha s.sha, *args
|
15
|
+
rescue CommandError => e
|
16
|
+
raise unless e.message =~ /\ANOSCRIPT/
|
17
|
+
script :load, s.text
|
18
|
+
evalsha s.sha, *args
|
19
|
+
end
|
20
|
+
when Pipeline
|
21
|
+
# When pipelining or in a transaction block, an error will abort the
|
22
|
+
# whole thing and there's no way to check whether the script has
|
23
|
+
# already been loaded. So, to be safe, just eval the script text.
|
24
|
+
eval s.text, *args
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def register_script(name, text)
|
29
|
+
script = Script.new(name, text)
|
30
|
+
scripts << script
|
31
|
+
script
|
32
|
+
end
|
33
|
+
|
34
|
+
def register_script_files(glob)
|
35
|
+
Dir[glob].map do |filename|
|
36
|
+
script_name = filename.split('/').last.split('.').first
|
37
|
+
register_script script_name, File.read(filename)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def scripts
|
42
|
+
@scripts ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
class Script
|
46
|
+
attr_reader :name, :text, :sha
|
47
|
+
|
48
|
+
def initialize(name, text)
|
49
|
+
@name = name.to_sym
|
50
|
+
@text = text.dup.freeze
|
51
|
+
@sha = Digest::SHA1.hexdigest(text).freeze
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
include Lua
|
57
|
+
end
|
data/redis-lua.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 'redis/lua'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'redis-lua'
|
8
|
+
spec.version = Redis::Lua::Version
|
9
|
+
spec.authors = ["Chris Hanks"]
|
10
|
+
spec.email = ['christopher.m.hanks@gmail.com']
|
11
|
+
spec.description = %q{A few more methods for the Redis class to make Lua script usage cleaner.}
|
12
|
+
spec.summary = %q{Cleaner Lua script usage with redis-rb.}
|
13
|
+
spec.homepage = 'https://github.com/chanks/redis-lua'
|
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_dependency 'redis'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'redis-pool'
|
26
|
+
end
|
data/spec/lua_spec.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis do
|
4
|
+
describe "#register_script" do
|
5
|
+
it "should remember a Lua script for execution" do
|
6
|
+
script = $redis.register_script 'my_script', "return 1"
|
7
|
+
script.should be_an_instance_of Redis::Script
|
8
|
+
$redis.scripts.should == [script]
|
9
|
+
|
10
|
+
script.text.should == "return 1"
|
11
|
+
script.text.should be_frozen
|
12
|
+
|
13
|
+
script.name.should == :my_script
|
14
|
+
|
15
|
+
script.sha.should == 'e0e1f9fabfc9d4800c877a703b823ac0578ff8db'
|
16
|
+
script.sha.should be_frozen
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#register_script_files" do
|
21
|
+
it "should register the scripts in a glob of files, using their filenames as script names" do
|
22
|
+
result = $redis.register_script_files './spec/support/scripts/*'
|
23
|
+
result.length.should be 2
|
24
|
+
result.each { |s| s.should be_an_instance_of Redis::Script }
|
25
|
+
result.should == $redis.scripts
|
26
|
+
|
27
|
+
one, two = result.sort_by(&:name)
|
28
|
+
one.name.should == :one
|
29
|
+
one.text.should == "return 1\n"
|
30
|
+
one.sha.should == "48df9b519ca145c867b895f740b37bd891e887af"
|
31
|
+
|
32
|
+
two.name.should == :two
|
33
|
+
two.text.should == "return 2\n"
|
34
|
+
two.sha.should == "b7ac6454df41a85df39e338f3e08cce31719ab72"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#run_script" do
|
39
|
+
before do
|
40
|
+
$redis.register_script 'argless', "return 1"
|
41
|
+
$redis.register_script 'onearg', "return ARGV[1] + 1"
|
42
|
+
$redis.register_script 'twoargs', "return ARGV[1] + ARGV[2]"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should load and execute a Lua script that hasn't been run before" do
|
46
|
+
$redis.run_script(:argless).should == 1
|
47
|
+
$redis.run_script(:onearg, :argv => [3]).should == 4
|
48
|
+
$redis.run_script(:twoargs, :argv => [4, 5]).should == 9
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should execute a Lua script that's been loaded and run before" do
|
52
|
+
sha = $redis.script :load, "return 1"
|
53
|
+
$redis.run_script(:argless).should == 1
|
54
|
+
end
|
55
|
+
|
56
|
+
it "with an unregistered Lua script should raise an error" do
|
57
|
+
proc{$redis.run_script :nonexistent_script}.should raise_error RuntimeError, /Nonexistent Lua script!/
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should gracefully handle a loss of the script cache" do
|
61
|
+
$redis.run_script(:argless).should == 1
|
62
|
+
$redis.script(:flush)
|
63
|
+
$redis.run_script(:argless).should == 1
|
64
|
+
end
|
65
|
+
|
66
|
+
it "in a transaction should execute a Lua script that hasn't been run before without losing the transaction" do
|
67
|
+
result = nil
|
68
|
+
|
69
|
+
$redis.multi do
|
70
|
+
$redis.incr 'key'
|
71
|
+
result = $redis.run_script :argless
|
72
|
+
end
|
73
|
+
|
74
|
+
$redis.get('key').should == '1'
|
75
|
+
result.class.should == Redis::Future
|
76
|
+
result.value.should == 1
|
77
|
+
end
|
78
|
+
|
79
|
+
it "in a pipeline should execute a Lua script that hasn't been run before" do
|
80
|
+
result = nil
|
81
|
+
|
82
|
+
$redis.pipelined do
|
83
|
+
$redis.incr 'key'
|
84
|
+
result = $redis.run_script :argless
|
85
|
+
end
|
86
|
+
|
87
|
+
$redis.get('key').should == '1'
|
88
|
+
result.class.should == Redis::Future
|
89
|
+
result.value.should == 1
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe Redis::Pool do
|
95
|
+
it "should work as expected" do
|
96
|
+
$redis_pool.register_script(:my_script, 'return 1')
|
97
|
+
|
98
|
+
10.times.map do
|
99
|
+
Thread.new do
|
100
|
+
10.times do
|
101
|
+
$redis_pool.run_script(:my_script).should == 1
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end.each(&:join)
|
105
|
+
end
|
106
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'redis/lua'
|
2
|
+
require 'redis/pool'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.before do
|
6
|
+
$redis.flushdb
|
7
|
+
$redis.script :flush
|
8
|
+
$redis.scripts.clear
|
9
|
+
$redis_pool.scripts.clear
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
$redis = Redis.new
|
14
|
+
$redis_pool = Redis::Pool.new(:size => 5)
|
@@ -0,0 +1 @@
|
|
1
|
+
return 1
|
@@ -0,0 +1 @@
|
|
1
|
+
return 2
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-lua
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Hanks
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-21 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: redis-pool
|
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: A few more methods for the Redis class to make Lua script usage cleaner.
|
70
|
+
email:
|
71
|
+
- christopher.m.hanks@gmail.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/redis/lua.rb
|
83
|
+
- redis-lua.gemspec
|
84
|
+
- spec/lua_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/support/scripts/one.lua
|
87
|
+
- spec/support/scripts/two
|
88
|
+
homepage: https://github.com/chanks/redis-lua
|
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.1.9
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Cleaner Lua script usage with redis-rb.
|
112
|
+
test_files:
|
113
|
+
- spec/lua_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/support/scripts/one.lua
|
116
|
+
- spec/support/scripts/two
|