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



111
112
113
# File 'lib/redis/keys.rb', line 111

def redis_EXISTS key
  @database.has_key? key
end

#redis_EXPIRE(key, seconds) ⇒ Object



115
116
117
# File 'lib/redis/keys.rb', line 115

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

#redis_EXPIREAT(key, timestamp) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

def redis_TTL key
  @database.ttl key
end

#redis_TYPE(key) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/redis/keys.rb', line 100

def redis_TYPE key
  case @database[key]
  when String, Numeric; 'string'
  when Array; 'list'
  when Set; 'set'
  when ZSet; 'zset'
  when Hash; 'hash'
  else 'unknown'
  end
end