Class: RedisCopy::Strategy::Classic

Inherits:
Object
  • Object
show all
Includes:
RedisCopy::Strategy
Defined in:
lib/redis-copy/strategy/classic.rb

Instance Method Summary collapse

Methods included from RedisCopy::Strategy

#initialize, load, #to_s

Instance Method Details

#copy(key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/redis-copy/strategy/classic.rb', line 8

def copy(key)
  vtype = @src.type(key)
  ttl = @src.ttl(key)

  case vtype
  when 'string'
    string = @src.get(key)
    @dst.set(key, string)
  when "list"
    list = @src.lrange(key, 0, -1)
    if list.length == 0
      # Empty list special case
      maybe_pipeline(@dst) do |dst|
        dst.lpush(key, '')
        dst.lpop(key)
      end
    else
      maybe_pipeline(@dst) do |dst|
        list.each do |ele|
          dst.rpush(key, ele)
        end
      end
    end
  when "set"
    set = @src.smembers(key)
    if set.length == 0
      # Empty set special case
      maybe_pipeline(@dst) do |dst|
        dst.sadd(key, '')
        dst.srem(key, '')
      end
    else
      maybe_pipeline(@dst) do |dst|
        set.each do |ele|
          dst.sadd(key,ele)
        end
      end
    end
  when 'hash'
    hash = @src.hgetall(key)
    @dst.mapped_hmset(key, hash)
  when 'zset'
    vs_zset = @src.zrange(key, 0, -1, :with_scores => true)
    sv_zset = vs_zset.map(&:reverse)
    @dst.zadd(key, sv_zset)
  else
    return false
  end

  @dst.expire(key, ttl) unless ttl < 0 || vtype == 'none'

  return true
end

#maybe_pipeline(redis, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/redis-copy/strategy/classic.rb', line 62

def maybe_pipeline(redis, &block)
  if pipeline_enabled? && redis.respond_to?(:pipelined)
    redis.pipelined(&block)
  else
    yield(redis)
  end
end

#pipeline_enabled?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/redis-copy/strategy/classic.rb', line 70

def pipeline_enabled?
  @pipeline_enabled ||= (false | @opt[:pipeline])
end