redis_batch 0.0.1 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/redis_batch/lua.rb +42 -0
- data/lib/redis_batch/queue.rb +5 -10
- data/lib/redis_batch.rb +4 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21c8d0fc6fa834d58afff202ad63597688d3ec0943cba4f06d82e0033201b820
|
4
|
+
data.tar.gz: fe2a7c88467d02c85e205d726b53c6aff4f191d0c08aa7d88831c0414bdbcd6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db65fe573cc1e50e53e870f620944dedb9d941309019ba06e068b2b49d8c9c6ff7b04b3352f560afeb2d487626fddd27cc26455c678685a021d29ee68d99198a
|
7
|
+
data.tar.gz: 1bbcf4b38252e74cd8e59ff705ee21c79ad9833f80933ca6b05f366da44a99225c0198e368bc691ee9022f85a56df2c847a61f5845a3a2a52cc60ca04a602f33
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module RedisBatch
|
2
|
+
class Lua
|
3
|
+
def self.function_load(redis)
|
4
|
+
redis.call("FUNCTION", "LOAD", "REPLACE", redis_batch_lua)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.redis_batch_lua
|
8
|
+
<<~LUA
|
9
|
+
#!lua name=redis_batch
|
10
|
+
|
11
|
+
local function lmove(keys, args)
|
12
|
+
local count = tonumber(args[1])
|
13
|
+
local source = keys[1]
|
14
|
+
local target = keys[2]
|
15
|
+
local llen = redis.call("LLEN", source)
|
16
|
+
if llen < count then
|
17
|
+
count = llen
|
18
|
+
end
|
19
|
+
local values = {}
|
20
|
+
for i=1,count do
|
21
|
+
values[i] = redis.call("LMOVE", source, target, 'LEFT', 'RIGHT')
|
22
|
+
end
|
23
|
+
return values
|
24
|
+
end
|
25
|
+
|
26
|
+
redis.register_function('rb_lmove', lmove)
|
27
|
+
|
28
|
+
local function lrestore(keys, args)
|
29
|
+
local source = keys[1]
|
30
|
+
local target = keys[2]
|
31
|
+
local llen = redis.call("LLEN", source)
|
32
|
+
for i=1,llen do
|
33
|
+
redis.call("LMOVE", source, target, 'RIGHT', 'LEFT')
|
34
|
+
end
|
35
|
+
return llen
|
36
|
+
end
|
37
|
+
|
38
|
+
redis.register_function('rb_lrestore', lrestore)
|
39
|
+
LUA
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/redis_batch/queue.rb
CHANGED
@@ -3,6 +3,7 @@ module RedisBatch
|
|
3
3
|
def initialize(namespace = self.class.name)
|
4
4
|
@namespace = namespace
|
5
5
|
@client = Client.instance
|
6
|
+
@client.with { |redis| RedisBatch::Lua.function_load(redis) }
|
6
7
|
end
|
7
8
|
|
8
9
|
def add(*items)
|
@@ -19,9 +20,8 @@ module RedisBatch
|
|
19
20
|
|
20
21
|
def abort_all
|
21
22
|
return unless processing?
|
22
|
-
|
23
|
-
|
24
|
-
keys.each do |key|
|
23
|
+
@client.with do |redis|
|
24
|
+
redis.keys("#{@namespace}_takeout_*").each do |key|
|
25
25
|
abort_processing(key, redis)
|
26
26
|
end
|
27
27
|
end
|
@@ -29,9 +29,7 @@ module RedisBatch
|
|
29
29
|
|
30
30
|
def take(count: 1, client: @client)
|
31
31
|
values = client.with do |redis|
|
32
|
-
redis.
|
33
|
-
count.times.map { transaction.lmove(queue_key, take_key, :left, :right) }
|
34
|
-
end
|
32
|
+
redis.call("FCALL", "rb_lmove", 2, queue_key, take_key, count)
|
35
33
|
end
|
36
34
|
yield values
|
37
35
|
client.with { |redis| redis.del(take_key) }
|
@@ -43,10 +41,7 @@ module RedisBatch
|
|
43
41
|
private
|
44
42
|
|
45
43
|
def abort_processing(key, redis)
|
46
|
-
|
47
|
-
redis.multi do |transaction|
|
48
|
-
recover_count.times { transaction.lmove(take_key, queue_key, :right, :left) }
|
49
|
-
end
|
44
|
+
redis.call("FCALL", "rb_lrestore", 2, key, queue_key)
|
50
45
|
end
|
51
46
|
|
52
47
|
def queue_key
|
data/lib/redis_batch.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'redis'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
2
|
+
require 'redis_batch/configurable'
|
3
|
+
require 'redis_batch/lua'
|
4
|
+
require 'redis_batch/client'
|
5
|
+
require 'redis_batch/queue'
|
5
6
|
|
6
7
|
module RedisBatch
|
7
8
|
Error = Class.new(StandardError)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_batch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Avemark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/redis_batch.rb
|
35
35
|
- lib/redis_batch/client.rb
|
36
36
|
- lib/redis_batch/configurable.rb
|
37
|
+
- lib/redis_batch/lua.rb
|
37
38
|
- lib/redis_batch/queue.rb
|
38
39
|
homepage: https://github.com/avemark/redis_batch_rb
|
39
40
|
licenses:
|