2020-09-29 19:42:32 +00:00
|
|
|
# refer : https://redis.io/commands
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
module Redis::Alfred
|
2020-07-07 18:44:07 +00:00
|
|
|
include Redis::RedisKeys
|
2020-07-04 06:12:47 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class << self
|
2020-07-07 18:44:07 +00:00
|
|
|
# key operations
|
|
|
|
|
2020-09-29 19:42:32 +00:00
|
|
|
# set a value in redis
|
2020-07-07 18:44:07 +00:00
|
|
|
def set(key, value)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.set(key, value) }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-09-29 19:42:32 +00:00
|
|
|
# set a key with expiry period
|
2020-07-07 18:44:07 +00:00
|
|
|
def setex(key, value, expiry = 1.day)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.setex(key, expiry, value) }
|
2020-07-07 18:44:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(key)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.get(key) }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.del(key) }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-09-29 19:42:32 +00:00
|
|
|
# increment a key by 1. throws error if key value is incompatible
|
|
|
|
# sets key to 0 before operation if key doesn't exist
|
|
|
|
def incr(key)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.incr(key) }
|
2020-09-29 19:42:32 +00:00
|
|
|
end
|
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
# list operations
|
|
|
|
|
|
|
|
def llen(key)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.llen(key) }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
def lrange(key, start_index = 0, end_index = -1)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.lrange(key, start_index, end_index) }
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
def rpop(key)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.rpop(key) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
def lpush(key, values)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.lpush(key, values) }
|
2020-07-07 18:44:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rpoplpush(source, destination)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.rpoplpush(source, destination) }
|
2020-07-07 18:44:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def lrem(key, value, count = 0)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.lrem(key, count, value) }
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
2020-07-04 06:12:47 +00:00
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
# hash operations
|
2020-07-04 06:12:47 +00:00
|
|
|
|
|
|
|
# add a key value to redis hash
|
|
|
|
def hset(key, field, value)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.hset(key, field, value) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get value from redis hash
|
|
|
|
def hget(key, field)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.hget(key, field) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get values of multiple keys from redis hash
|
|
|
|
def hmget(key, fields)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.hmget(key, *fields) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
# sorted set operations
|
2020-07-04 06:12:47 +00:00
|
|
|
|
|
|
|
# add score and value for a key
|
|
|
|
def zadd(key, score, value)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.zadd(key, score, value) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get score of a value for key
|
|
|
|
def zscore(key, value)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.zscore(key, value) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get values by score
|
|
|
|
def zrangebyscore(key, range_start, range_end)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.zrangebyscore(key, range_start, range_end) }
|
2020-07-04 06:12:47 +00:00
|
|
|
end
|
2022-08-05 10:51:39 +00:00
|
|
|
|
|
|
|
# remove values by score
|
|
|
|
# exclusive score is specified by prefixing (
|
|
|
|
def zremrangebyscore(key, range_start, range_end)
|
2022-11-03 00:31:20 +00:00
|
|
|
$alfred.with { |conn| conn.zremrangebyscore(key, range_start, range_end) }
|
2022-08-05 10:51:39 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|