Class: Xunch::RedisClient

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/shard/redis.rb

Constant Summary collapse

DEFAULTS =
{
  :size => 1,
  :timeout => nil,
  :pool_timeout => 0,
  :driver => :hiredis
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RedisClient

Returns a new instance of RedisClient.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xunch/shard/redis.rb', line 14

def initialize(options = {})
  @options = DEFAULTS.merge(options)
  if RUBY_PLATFORM =~ /mingw/
    @options[:driver] = nil
  end
  if(@options[:pool_timeout] > 0)
    @pool_timeout = @options[:pool_timeout]
  else
    @pool_timeout = 1073741823
  end
  @pool = Array.new(@options[:size]) { redis = Redis.new(@options) }
  @mutex = Mutex.new
  @resource = ConditionVariable.new
end

Instance Method Details

#del(*keys) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xunch/shard/redis.rb', line 39

def del(*keys)
  if keys.length > 5
    with do | redis |
      redis.pipelined do
        redis.del(*keys)
      end
    end
  else
    with do | redis |
      redis.del(*keys)
    end
  end
end

#destroyObject



29
30
31
# File 'lib/xunch/shard/redis.rb', line 29

def destroy
  @pool.each {|redis| redis.quit if redis and redis.connected?}
end

#exists(key) ⇒ Object



33
34
35
36
37
# File 'lib/xunch/shard/redis.rb', line 33

def exists(key)
  with do | redis |
    redis.exists(key)
  end
end

#expire(key, ttl) ⇒ Object



53
54
55
56
57
# File 'lib/xunch/shard/redis.rb', line 53

def expire(key, ttl)
  with do | redis |
    redis.expire(key,ttl)
  end
end

#get(key) ⇒ String

get value with key

Parameters:

  • key (String)

Returns:

  • (String)

    value cache in redis



69
70
71
72
73
# File 'lib/xunch/shard/redis.rb', line 69

def get(key)
  with do | redis |
    redis.get(key)
  end
end

#hget(key, *fields) ⇒ Object



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

def hget(key, *fields)
  with do | redis |
    redis.mapped_hmget(key, *fields)
  end
end

#hgetall(key) ⇒ Object



129
130
131
132
133
# File 'lib/xunch/shard/redis.rb', line 129

def hgetall(key)
  with do | redis |
    redis.hgetall(key)
  end
end

#hmget(keys, *fields) ⇒ Object

multi get hash type keys with fields

Parameters:

  • keys (Array)

    redis hash key

  • fields (Array)

    hash field names



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

def hmget(keys,*fields)
  # block =  lambda { |args| p args }
  with do | redis |
    redis.pipelined do
      keys.each { | key |
        redis.mapped_hmget(key, *fields)
      }
    end
  end
end

#hmgetall(keys) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/xunch/shard/redis.rb', line 174

def hmgetall(keys)
  with do | redis |
    redis.pipelined do
      keys.each { | key |
        redis.hgetall(key)
      }
    end
  end
end

#hmset(hash, ttl) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/xunch/shard/redis.rb', line 161

def hmset(hash, ttl)
  with do | redis |
    redis.pipelined do
      hash.each { | key, value |
        redis.mapped_hmset(key, value)
        if(ttl > 0)
          redis.expire(key, ttl)
        end
      }
    end
  end
end

#hset(key, hash, ttl) ⇒ Object



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

def hset(key, hash, ttl)
  with do | redis |
    redis.pipelined do
      redis.mapped_hmset(key, hash)
      if(ttl > 0)
        redis.expire(key,ttl)
      end
    end
  end
end

#hsetall(key, hash, ttl) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/xunch/shard/redis.rb', line 135

def hsetall(key,hash,ttl)
  with do | redis |
    redis.pipelined do
      redis.mapped_hmset(key, hash)
      if(ttl > 0)
        redis.expire(key,ttl)
      end
    end
  end
end

#llen(key) ⇒ Object



184
185
186
187
188
# File 'lib/xunch/shard/redis.rb', line 184

def llen(key)
  with do | redis |
    redis.llen(key)
  end
end

#lrange(key, start, stop) ⇒ Object



212
213
214
215
216
# File 'lib/xunch/shard/redis.rb', line 212

def lrange(key, start, stop)
  with do | redis |
    redis.lrange(key,start,stop)
  end
end

#lrem(key, value) ⇒ Object



190
191
192
193
194
# File 'lib/xunch/shard/redis.rb', line 190

def lrem(key,value)
  with do | redis |
    redis.lrem(key,1,value)
  end
end

#lset(temp_key, new_key, values, ttl) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/xunch/shard/redis.rb', line 196

def lset(temp_key,new_key,values,ttl)
  with do | redis |
    redis.pipelined do
      redis.del(temp_key)
      values.each {
        | value |
        redis.rpush(temp_key,value)
      }
      result = redis.rename(temp_key,new_key)
      if(ttl > 0)
       redis.expire(new_key,ttl)
      end
    end
  end
end

#mget(keys) ⇒ Object



75
76
77
78
79
# File 'lib/xunch/shard/redis.rb', line 75

def mget(keys)
  with do | redis |
    redis.mget(*keys)
  end
end

#mset(hash, ttl) ⇒ Object

multi set key value with expire time in second NOTE: use pipeline inner

Parameters:

  • hash (Hash)

    key value pairs

  • ttl (Fixnum)

    time to live



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/xunch/shard/redis.rb', line 98

def mset(hash, ttl)
  with do | redis |
    if(ttl != 0)
      redis.pipelined do
        hash.each { |key,value|
          redis.setex(key,ttl,value)
        }
      end
    else
      redis.mset(values)
    end
  end
end

#rename(old_key, new_key) ⇒ String

Rename a key. If the new key already exists it is overwritten.

Parameters:

  • old_name (String)
  • new_name (String)

Returns:

  • (String)

    ‘OK`



223
224
225
226
227
# File 'lib/xunch/shard/redis.rb', line 223

def rename(old_key, new_key)
  with do | redis |
    redis.rename(old_key,new_key)
  end
end

#set(key, value, ttl) ⇒ Object

set key value with expire time in second

Parameters:

  • key (String)
  • value (String)
  • ttl (Fixnum)

    time to live



87
88
89
90
91
# File 'lib/xunch/shard/redis.rb', line 87

def set(key, value, ttl)
  with do | redis |
    redis.setex(key,ttl,value)
  end
end

#ttl(key) ⇒ Object



59
60
61
62
63
# File 'lib/xunch/shard/redis.rb', line 59

def ttl(key)
  with do | redis |
    redis.ttl(key)
  end
end

#type(key) ⇒ Object



229
230
231
232
233
# File 'lib/xunch/shard/redis.rb', line 229

def type(key)
  with do | redis |
    redis.type(key)
  end
end