Module: Redis::Keys

Defined in:
lib/redis/keys.rb

Instance Method Summary collapse

Instance Method Details

#redis_DEL(*keys) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/redis/keys.rb', line 91

def redis_DEL *keys
  count = 0
  keys.each do |key|
    count += 1  if @database.has_key? key
    @database.delete key
  end
  count
end

#redis_EXISTS(key) ⇒ Object



118
119
120
# File 'lib/redis/keys.rb', line 118

def redis_EXISTS key
  @database.has_key? key
end

#redis_EXPIRE(key, seconds) ⇒ Object



122
123
124
# File 'lib/redis/keys.rb', line 122

def redis_EXPIRE key, seconds
  @database.expire key, redis_pos_i(seconds)
end

#redis_EXPIREAT(key, timestamp) ⇒ Object



126
127
128
# File 'lib/redis/keys.rb', line 126

def redis_EXPIREAT key, timestamp
  @database.expire_at key, redis_pos_i(timestamp)
end

#redis_KEYS(pattern) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/redis/keys.rb', line 9

def redis_KEYS pattern
  @database.reduce([]) do |memo, key_val|
    key = key_val[0]
    memo.push key if File.fnmatch(pattern, key)
    memo
  end
end

#redis_MOVE(key, db) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/redis/keys.rb', line 151

def redis_MOVE key, db
  raise unless @database.has_key? key
  raise if @databases[redis_i db].has_key? key
  @databases[redis_i db][key] = @database[key]
  @database.delete key
  true
rescue
  false
end

#redis_PERSIST(key) ⇒ Object



130
131
132
# File 'lib/redis/keys.rb', line 130

def redis_PERSIST key
  @database.persist key
end

#redis_RANDOMKEYObject



4
5
6
7
# File 'lib/redis/keys.rb', line 4

def redis_RANDOMKEY
  return nil if @database.empty?
  @database.random_key
end

#redis_RENAME(key, newkey) ⇒ Object



138
139
140
141
142
143
# File 'lib/redis/keys.rb', line 138

def redis_RENAME key, newkey
  raise 'key and newkey are identical' if key == newkey
  raise 'key not found' unless @database.has_key? key
  @database[newkey] = @database[key]
  @database.delete key
end

#redis_RENAMENX(key, newkey) ⇒ Object



145
146
147
148
149
# File 'lib/redis/keys.rb', line 145

def redis_RENAMENX key, newkey
  return false if @database.has_key? newkey
  redis_RENAME key, newkey
  true
end

#redis_SORT(key, *args) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/redis/keys.rb', line 17

def redis_SORT key, *args
  record = @database[key] || []
  sort = 'ASC'
  gets = []
  alpha = false
  by = by_hash = offset = count = store = nil
  until args.empty?
    arg = args.shift
    case arg.upcase
    when 'LIMIT'
      offset = args.shift.to_i
      count = args.shift.to_i
    when 'ASC'
      sort = 'ASC'
    when 'DESC'
      sort = 'DESC'
    when 'ALPHA'
      alpha = true
    when 'STORE'
      store = args.shift
    when 'GET'
      gets << args.shift
    when 'BY'
      by, by_hash = args.shift.split '->', 2
    else
      raise "#{arg} bad argument"
    end
  end
  result = record.sort do |a, b|
    if by
      a = @database[by.sub /\*/, a]
      a = a[by_hash] if by_hash
      b = @database[by.sub /\*/, b]
      b = b[by_hash] if by_hash
    end
    if alpha
      a = a.to_s
      b = b.to_s
    else
      a = a.to_f
      b = b.to_f
    end
    if sort == 'DESC'
      b <=> a
    else
      a <=> b
    end
  end
  unless gets.empty?
    original = result
    result = []
    original.each do |r|
      gets.each do |g|
        get, get_hash = g.split('->', 2)
        r = @database[get.sub /\*/, r] unless get == '#'
        r = r[get_hash] if get_hash
        result << r
      end
    end
  end
  if count and offset
    result = result[offset,count]
  elsif count
    result = result[0,count]
  elsif offset
    result = result[offset..-1]
  end
  if Array === result[0]
    result = result.collect {|r| r.first}
  end
  @database[store] = result if store
  result
end

#redis_TTL(key) ⇒ Object



134
135
136
# File 'lib/redis/keys.rb', line 134

def redis_TTL key
  @database.ttl key
end

#redis_TYPE(key) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/redis/keys.rb', line 100

def redis_TYPE key
  if String === @database[key]
    'string'
  elsif Numeric === @database[key]
    'string'
  elsif Array === @database[key]
    'list'
  elsif Set === @database[key]
    'set'
  elsif ZSet === @database[key]
    'zset'
  elsif Hash === @database[key]
    'hash'
  else
    'unknown'
  end
end