Module: Redis::Strings

Defined in:
lib/redis/strings.rb

Overview

Note: Redis does not have a dedicated integer type. Integer operations will convert values to integers.

Instance Method Summary collapse

Instance Method Details

#redis_APPEND(key, value) ⇒ Object



14
15
16
17
18
# File 'lib/redis/strings.rb', line 14

def redis_APPEND key, value
  new_value = redis_GET(key).to_s + value
  @database[key] = new_value
  new_value.size
end

#redis_DECRBY(key, value) ⇒ Object



137
138
139
# File 'lib/redis/strings.rb', line 137

def redis_DECRBY key, value
  @database[key] = redis_i(@database[key] || 0) - redis_i(value)
end

#redis_GET(key) ⇒ Object



8
9
10
11
12
# File 'lib/redis/strings.rb', line 8

def redis_GET key
  value = @database[key]
  redis_t NilClass, String, Numeric, value
  value
end

#redis_GETBIT(key, offset) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redis/strings.rb', line 41

def redis_GETBIT key, offset
  data = redis_GET key
  return 0 unless data
  data = data.to_s
  offset = redis_pos_i offset
  byte = offset / 8
  bit = 0x80 >> offset % 8
  return 0 if data.size <= byte
  original_byte = data[byte].ord
  original_bit = original_byte & bit
  original_bit != 0
end

#redis_GETRANGE(key, first, last) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/redis/strings.rb', line 33

def redis_GETRANGE key, first, last
  first = redis_i first
  last = redis_i last
  value = redis_GET(key) || ''
  first = 0 if first < -value.size
  value[first..last]
end

#redis_GETSET(key, value) ⇒ Object



88
89
90
91
92
# File 'lib/redis/strings.rb', line 88

def redis_GETSET key, value
  old = redis_GET key
  @database[key] = value
  old
end

#redis_INCR(key) ⇒ Object



129
130
131
# File 'lib/redis/strings.rb', line 129

def redis_INCR key
  @database[key] = redis_i(@database[key] || 0) + 1
end

#redis_INCRBY(key, value) ⇒ Object



133
134
135
# File 'lib/redis/strings.rb', line 133

def redis_INCRBY key, value
  @database[key] = redis_i(@database[key] || 0) + redis_i(value)
end

#redis_MGET(*keys) ⇒ Object



81
82
83
84
85
86
# File 'lib/redis/strings.rb', line 81

def redis_MGET *keys
  keys.collect do |key|
    value = @database[key]
    (String === value) ? value : nil
  end
end

#redis_MSET(*args) ⇒ Object



105
106
107
108
109
110
# File 'lib/redis/strings.rb', line 105

def redis_MSET *args
  Hash[*args].each do |key, value|
    @database[key] = value
  end
  :'+OK'
end

#redis_MSETNX(*args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/redis/strings.rb', line 112

def redis_MSETNX *args
  hash = Hash[*args]
  hash.each do |key, value|
    return false if @database.has_key? key
  end
  hash.each do |key, value|
    @database[key] = value
  end
  true
end

#redis_SET(key, value) ⇒ Object



94
95
96
97
# File 'lib/redis/strings.rb', line 94

def redis_SET key, value
  @database[key] = value
  :'+OK'
end

#redis_SETBIT(key, offset, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/redis/strings.rb', line 54

def redis_SETBIT key, offset, value
  data = (redis_GET(key)||'').to_s
  offset = redis_pos_i offset, 'out of range'
  raise 'out of range' if offset >= 4*1024*1024*1024
  byte = offset / 8
  bit = 0x80 >> offset % 8
  if data.size <= byte
    data += 0.chr * (byte - data.size + 1)
  end
  original_byte = data[byte].ord
  original_bit = original_byte & bit
  if value == '0'
    data[byte] = (original_byte & ~bit).chr
  elsif value == '1'
    data[byte] = (original_byte | bit).chr
  else
    raise 'out of range'
  end
  @database[key] = data
  original_bit != 0
end

#redis_SETEX(key, seconds, value) ⇒ Object



99
100
101
102
103
# File 'lib/redis/strings.rb', line 99

def redis_SETEX key, seconds, value
  @database[key] = value
  @database.expire key, redis_pos_i(seconds, 'invalid expire time in SETEX')
  :'+OK'
end

#redis_SETNX(key, value) ⇒ Object



123
124
125
126
127
# File 'lib/redis/strings.rb', line 123

def redis_SETNX key, value
  return false if @database.has_key? key
  @database[key] = value
  true
end

#redis_SETRANGE(key, offset, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redis/strings.rb', line 20

def redis_SETRANGE key, offset, value
  data = (redis_GET(key)||'').to_s
  return data.size if value.empty?
  offset = redis_pos_i offset, 'out of range'
  raise 'maximum allowed size' if offset + value.size > 512*1024*1024
  if data.size <= offset
    data.concat 0.chr * (offset - data.size + value.size)
  end
  data[offset,value.size] = value
  @database[key] = data
  data.size
end

#redis_STRLEN(key) ⇒ Object



76
77
78
79
# File 'lib/redis/strings.rb', line 76

def redis_STRLEN key
  value = redis_GET key
  value ? value.size : 0
end