redis_batch 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d205bf5fdc9bfb375f6666e1017413f9bb2ecbdb8d696912cb9b658f3910ffed
4
- data.tar.gz: 8b7521a379d318634fa2a6c8e5cf42398af416414b2fec89e1a0f9bd40e18087
3
+ metadata.gz: 21c8d0fc6fa834d58afff202ad63597688d3ec0943cba4f06d82e0033201b820
4
+ data.tar.gz: fe2a7c88467d02c85e205d726b53c6aff4f191d0c08aa7d88831c0414bdbcd6a
5
5
  SHA512:
6
- metadata.gz: 357499ea517316f46c7b80dc43d14269656141f345afb8db1cc56a8dab790bcf6fbef8b95121b7480dbdd6fb6162c572182830550ba9637c191d13a99e5ec82d
7
- data.tar.gz: 685246ee83f7a97946efebf7193477cd6aee5c75ace9b92e5f5bb75cf134a4d36968e11519bd3458b8da4b65c622701193e3b622c25615fd19bfb17c747646a4
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
@@ -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
- keys = @client.with { |redis| redis.keys("#{@namespace}_takeout_*") }
23
- client.with do |redis|
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.multi do |transaction|
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
- recover_count = redis.llen(key)
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 './lib/redis_batch/configurable'
3
- require './lib/redis_batch/client'
4
- require './lib/redis_batch/queue'
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.1
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-03 00:00:00.000000000 Z
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: