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
28
29
30
31
# 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)
    options[:pool_timeout] = 1073741823
  end
  last_driver = Redis::Connection.drivers.last.name
  options[:driver] ||= last_driver[last_driver.rindex(':') + 1, last_driver.length].downcase.to_sym
  if options[:driver] == :synchrony
      require "xunch/connection/fiber_redis_pool"
      @pool = FiberRedisPool.new(options)
  else
      require "xunch/connection/threaded_redis_pool"
      @pool = ThreadedRedisPool.new(options)
  end
end

Instance Method Details

#del(*keys) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xunch/shard/redis.rb', line 43

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



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

def destroy
  @pool.destroy
end

#exists(key) ⇒ Object



37
38
39
40
41
# File 'lib/xunch/shard/redis.rb', line 37

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

#expire(key, ttl) ⇒ Object



57
58
59
60
61
# File 'lib/xunch/shard/redis.rb', line 57

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



73
74
75
76
77
# File 'lib/xunch/shard/redis.rb', line 73

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

#hget(key, *fields) ⇒ Object



131
132
133
134
135
# File 'lib/xunch/shard/redis.rb', line 131

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

#hgetall(key) ⇒ Object



137
138
139
140
141
# File 'lib/xunch/shard/redis.rb', line 137

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



158
159
160
161
162
163
164
165
166
167
# File 'lib/xunch/shard/redis.rb', line 158

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



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

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

#hmset(hash, ttl) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/xunch/shard/redis.rb', line 169

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/xunch/shard/redis.rb', line 120

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



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

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



192
193
194
195
196
# File 'lib/xunch/shard/redis.rb', line 192

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

#lrange(key, start, stop) ⇒ Object



232
233
234
235
236
# File 'lib/xunch/shard/redis.rb', line 232

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

#lrem(key, *value) ⇒ Object



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

def lrem(key,*value)
  if(value.length > 3)
    with do | redis |
      redis.pipelined do
        value.each{ |v|
          redis.lrem(key,1,v)
        }
      end
    end
  else
    with do | redis |
      value.each{ |v|
          redis.lrem(key,1,v)
        }
    end
  end
end

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/xunch/shard/redis.rb', line 216

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



79
80
81
82
83
# File 'lib/xunch/shard/redis.rb', line 79

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



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/xunch/shard/redis.rb', line 106

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.mapped_mset(hash)
    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`



243
244
245
246
247
# File 'lib/xunch/shard/redis.rb', line 243

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



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

def set(key, value, ttl)
  with do | redis |
    if(ttl > 0)
      redis.setex(key,ttl,value)
    else
      redis.set(key,value)
    end
  end
end

#ttl(key) ⇒ Object



63
64
65
66
67
# File 'lib/xunch/shard/redis.rb', line 63

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

#type(key) ⇒ Object



249
250
251
252
253
# File 'lib/xunch/shard/redis.rb', line 249

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