Module: RedisCluster::Function::String

Included in:
RedisCluster::Function
Defined in:
lib/redis_cluster/function/string.rb

Overview

String implement redis strings commands. There will be some adjustment for cluster. see redis.io/commands#string. Most of the code are copied from github.com/redis/redis-rb/blob/master/lib/redis.rb.

SETTER = [:getset, :append, :setbit, :setrange, :set, :setex, :psetex, :setnx, :incr,

:incrby, :incrbyfloat, :decr, :decrby]

GETTER = [:strlen, :bitpos, :bitcount, :getbit, :getrange, :get]

Instance Method Summary collapse

Instance Method Details

#append(key, value) ⇒ Fixnum

Append a value to a key.

Parameters:

Returns:

  • (Fixnum)

    length of the string after appending



185
186
187
# File 'lib/redis_cluster/function/string.rb', line 185

def append(key, value)
  call(key, [:append, key, value])
end

#bitcount(key, start = 0, stop = -1)) ⇒ Fixnum

Count the number of set bits in a range of the string value stored at key.

Parameters:

  • key (String)
  • start (Fixnum) (defaults to: 0)

    start index

  • stop (Fixnum) (defaults to: -1))

    stop index

Returns:

  • (Fixnum)

    the number of bits set to 1



195
196
197
# File 'lib/redis_cluster/function/string.rb', line 195

def bitcount(key, start = 0, stop = -1)
  call(key, [:bitcount, key, start, stop], read: true)
end

#bitpos(key, bit, start = nil, stop = nil) ⇒ Fixnum

Return the position of the first bit set to 1 or 0 in a string.

Parameters:

  • key (String)
  • bit (Fixnum)

    whether to look for the first 1 or 0 bit

  • start (Fixnum) (defaults to: nil)

    start index

  • stop (Fixnum) (defaults to: nil)

    stop index

Returns:

  • (Fixnum)

    the position of the first 1/0 bit. -1 if looking for 1 and it is not found or start and stop are given.



207
208
209
210
211
212
213
# File 'lib/redis_cluster/function/string.rb', line 207

def bitpos(key, bit, start = nil, stop = nil)
  command = [:bitpos, key, bit]
  command << start if start
  command << stop if start && stop

  call(key, command, read: true)
end

#decr(key) ⇒ Fixnum

Decrement the integer value of a key by one.

Examples:

redis.decr("value")
  # => 4

Parameters:

Returns:

  • (Fixnum)

    value after decrementing it



25
26
27
# File 'lib/redis_cluster/function/string.rb', line 25

def decr(key)
  call(key, [:decr, key])
end

#decrby(key, decrement) ⇒ Fixnum

Decrement the integer value of a key by the given number.

Examples:

redis.decrby("value", 5)
  # => 0

Parameters:

  • key (String)
  • decrement (Fixnum)

Returns:

  • (Fixnum)

    value after decrementing it



38
39
40
# File 'lib/redis_cluster/function/string.rb', line 38

def decrby(key, decrement)
  call(key, [:decrby, key, decrement])
end

#get(key) ⇒ String

Get the value of a key.

Parameters:

Returns:



136
137
138
# File 'lib/redis_cluster/function/string.rb', line 136

def get(key)
  call(key, [:get, key], read: true)
end

#getbit(key, offset) ⇒ Fixnum

Returns the bit value at offset in the string value stored at key.

Parameters:

  • key (String)
  • offset (Fixnum)

    bit offset

Returns:

  • (Fixnum)

    0 or 1



176
177
178
# File 'lib/redis_cluster/function/string.rb', line 176

def getbit(key, offset)
  call(key, [:getbit, key, offset], read: true)
end

#getrange(key, start, stop) ⇒ Fixnum

Get a substring of the string stored at a key.

Parameters:

  • key (String)
  • start (Fixnum)

    zero-based start offset

  • stop (Fixnum)

    zero-based end offset. Use -1 for representing the end of the string

Returns:

  • (Fixnum)

    0 or 1



157
158
159
# File 'lib/redis_cluster/function/string.rb', line 157

def getrange(key, start, stop)
  call(key, [:getrange, key, start, stop], read: true)
end

#getset(key, value) ⇒ String

Set the string value of a key and return its old value.

Parameters:

  • key (String)
  • value (String)

    value to replace the current value with

Returns:

  • (String)

    the old value stored in the key, or nil if the key did not exist



221
222
223
# File 'lib/redis_cluster/function/string.rb', line 221

def getset(key, value)
  call(key, [:getset, key, value.to_s])
end

#incr(key) ⇒ Fixnum

Increment the integer value of a key by one.

Examples:

redis.incr("value")
  # => 6

Parameters:

Returns:

  • (Fixnum)

    value after incrementing it



50
51
52
# File 'lib/redis_cluster/function/string.rb', line 50

def incr(key)
  call(key, [:incr, key])
end

#incrby(key, increment) ⇒ Fixnum

Increment the integer value of a key by the given integer number.

Examples:

redis.incrby("value", 5)
  # => 10

Parameters:

  • key (String)
  • increment (Fixnum)

Returns:

  • (Fixnum)

    value after incrementing it



63
64
65
# File 'lib/redis_cluster/function/string.rb', line 63

def incrby(key, increment)
  call(key, [:incrby, key, increment])
end

#incrbyfloat(key, increment) ⇒ Float

Increment the numeric value of a key by the given float number.

Examples:

redis.incrbyfloat("value", 1.23)
  # => 1.23

Parameters:

  • key (String)
  • increment (Float)

Returns:

  • (Float)

    value after incrementing it



76
77
78
# File 'lib/redis_cluster/function/string.rb', line 76

def incrbyfloat(key, increment)
  call(key, [:incrbyfloat, key, increment], transform: Redis::Floatify)
end

#psetex(key, ttl, value) ⇒ String

Set the time to live in milliseconds of a key.

Parameters:

Returns:



119
120
121
# File 'lib/redis_cluster/function/string.rb', line 119

def psetex(key, ttl, value)
  call(key, [:psetex, key, ttl, value.to_s])
end

#set(key, value, options = {}) ⇒ String, Boolean

Set the string value of a key.

Parameters:

  • key (String)
  • value (String)
  • options (Hash) (defaults to: {})
    • ‘:ex => Fixnum`: Set the specified expire time, in seconds.

    • ‘:px => Fixnum`: Set the specified expire time, in milliseconds.

    • ‘:nx => true`: Only set the key if it does not already exist.

    • ‘:xx => true`: Only set the key if it already exist.

Returns:

  • (String, Boolean)

    ‘“OK”` or true, false if `:nx => true` or `:xx => true`



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/redis_cluster/function/string.rb', line 90

def set(key, value, options = {})
  ex = options[:ex]
  px = options[:px]
  args = [:set, key, value.to_s]

  args.concat(['EX', ex]) if ex
  args.concat(['PX', px]) if px
  args.concat(['NX']) if options[:nx]
  args.concat(['XX']) if options[:xx]

  call(key, args, transform: Redis::BoolifySet)
end

#setbit(key, offset, value) ⇒ Fixnum

Sets or clears the bit at offset in the string value stored at key.

Parameters:

  • key (String)
  • offset (Fixnum)

    bit offset

  • value (Fixnum)

    bit value 0 or 1

Returns:

  • (Fixnum)

    the original bit value stored at offset



167
168
169
# File 'lib/redis_cluster/function/string.rb', line 167

def setbit(key, offset, value)
  call(key, [:setbit, key, offset, value])
end

#setex(key, ttl, value) ⇒ String

Set the time to live in seconds of a key.

Parameters:

Returns:



109
110
111
# File 'lib/redis_cluster/function/string.rb', line 109

def setex(key, ttl, value)
  call(key, [:setex, key, ttl, value.to_s])
end

#setnx(key, value) ⇒ Boolean

Set the value of a key, only if the key does not exist.

Parameters:

Returns:

  • (Boolean)

    whether the key was set or not



128
129
130
# File 'lib/redis_cluster/function/string.rb', line 128

def setnx(key, value)
  call(key, [:setnx, key, value.to_s], transform: Redis::Boolify)
end

#setrange(key, offset, value) ⇒ Fixnum

Overwrite part of a string at key starting at the specified offset.

Parameters:

  • key (String)
  • offset (Fixnum)

    byte offset

  • value (String)

Returns:

  • (Fixnum)

    length of the string after it was modified



146
147
148
# File 'lib/redis_cluster/function/string.rb', line 146

def setrange(key, offset, value)
  call(key, [:setrange, key, offset, value.to_s])
end

#strlen(key) ⇒ Fixnum

Get the length of the value stored in a key.

Parameters:

Returns:

  • (Fixnum)

    the length of the value stored in the key, or 0 if the key does not exist



230
231
232
# File 'lib/redis_cluster/function/string.rb', line 230

def strlen(key)
  call(key, [:strlen, key], read: true)
end