Module: RedisLike::Lists

Includes:
Helpers::Lists, Keys
Included in:
Moneta::Defaults
Defined in:
lib/redislike/lists.rb

Overview

Mixin containing redis-like list operations for Moneta stores.

Instance Method Summary collapse

Methods included from Helpers::Lists

#block_while_empty, #dequeue, #enqueue, #remove_all, #remove_count

Methods included from Keys

#exists

Instance Method Details

#blpop(list) ⇒ Object



27
28
29
30
# File 'lib/redislike/lists.rb', line 27

def blpop(list)
  block_while_empty list
  lpop list
end

#brpop(list) ⇒ Object



40
41
42
43
# File 'lib/redislike/lists.rb', line 40

def brpop(list)
  block_while_empty list
  rpop list
end

#brpoplpush(source, destination) ⇒ Object



51
52
53
54
# File 'lib/redislike/lists.rb', line 51

def brpoplpush(source, destination)
  block_while_empty source
  brpoplpush source, destination
end

#lindex(list, item) ⇒ Object



56
57
58
# File 'lib/redislike/lists.rb', line 56

def lindex(list, item)
  fetch(list, []).at item
end

#linsert(list, location, pivot, item) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/redislike/lists.rb', line 60

def linsert(list, location, pivot, item)
  return 0 unless exists list
  return -1 unless lrange(list, 0, -1).include? pivot
  items = fetch list, []
  index = items.index pivot
  index += 1 if location == :before
  items.insert(index, item)
  store list, items
  llen list
end

#llen(list) ⇒ Object



71
72
73
# File 'lib/redislike/lists.rb', line 71

def llen(list)
  fetch(list, []).length
end

#lpop(list) ⇒ Object



32
33
34
# File 'lib/redislike/lists.rb', line 32

def lpop(list)
  dequeue list, from: :head
end

#lpush(list, item) ⇒ Object



11
12
13
# File 'lib/redislike/lists.rb', line 11

def lpush(list, item)
  enqueue list, item, to: :head
end

#lpushx(list, item) ⇒ Object



15
16
17
# File 'lib/redislike/lists.rb', line 15

def lpushx(list, item)
  enqueue list, item, to: :head, allow_create: false
end

#lrange(list, start, stop) ⇒ Object



75
76
77
# File 'lib/redislike/lists.rb', line 75

def lrange(list, start, stop)
  fetch(list, [])[(start..stop)] || []
end

#lrem(list, count, item) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/redislike/lists.rb', line 79

def lrem(list, count, item)
  case
  when count > 0
    remove_count list, count, item, from: :head
  when count < 0
    remove_count list, count.abs, item, from: :tail
  when count == 0
    remove_all list, item
  end
end

#lset(list, index, item) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/redislike/lists.rb', line 90

def lset(list, index, item)
  fail KeyError, 'ERR no such key' unless exists(list)
  fail IndexError, 'ERR index out of range' unless lindex(list, index)
  items = fetch list, []
  items[index] = item
  'OK' if store list, items
end

#ltrim(list, start, stop) ⇒ Object



98
99
100
# File 'lib/redislike/lists.rb', line 98

def ltrim(list, start, stop)
  'OK' if store list, lrange(list, start, stop)
end

#rpop(list) ⇒ Object



36
37
38
# File 'lib/redislike/lists.rb', line 36

def rpop(list)
  dequeue list, from: :tail
end

#rpoplpush(source, destination) ⇒ Object



45
46
47
48
49
# File 'lib/redislike/lists.rb', line 45

def rpoplpush(source, destination)
  item = rpop source
  lpush destination, item
  item
end

#rpush(list, item) ⇒ Object



19
20
21
# File 'lib/redislike/lists.rb', line 19

def rpush(list, item)
  enqueue list, item, to: :tail
end

#rpushx(list, item) ⇒ Object



23
24
25
# File 'lib/redislike/lists.rb', line 23

def rpushx(list, item)
  enqueue list, item, to: :tail, allow_create: false
end