Class: Xunch::RedisClient
- Inherits:
-
Object
- Object
- Xunch::RedisClient
- Defined in:
- lib/xunch/shard/redis.rb
Constant Summary collapse
- DEFAULTS =
{ :size => 1, :timeout => nil, :pool_timeout => 0, :driver => :hiredis }
Instance Method Summary collapse
- #del(*keys) ⇒ Object
- #destroy ⇒ Object
- #exists(key) ⇒ Object
- #expire(key, ttl) ⇒ Object
-
#get(key) ⇒ String
get value with key.
- #hget(key, *fields) ⇒ Object
- #hgetall(key) ⇒ Object
-
#hmget(keys, *fields) ⇒ Object
multi get hash type keys with fields.
- #hmgetall(keys) ⇒ Object
- #hmset(hash, ttl) ⇒ Object
- #hset(key, hash, ttl) ⇒ Object
- #hsetall(key, hash, ttl) ⇒ Object
-
#initialize(options = {}) ⇒ RedisClient
constructor
A new instance of RedisClient.
- #llen(key) ⇒ Object
- #lrange(key, start, stop) ⇒ Object
- #lrem(key, *value) ⇒ Object
- #lset(temp_key, new_key, values, ttl) ⇒ Object
- #mget(keys) ⇒ Object
-
#mset(hash, ttl) ⇒ Object
multi set key value with expire time in second NOTE: use pipeline inner.
-
#rename(old_key, new_key) ⇒ String
Rename a key.
-
#set(key, value, ttl) ⇒ Object
set key value with expire time in second.
- #ttl(key) ⇒ Object
- #type(key) ⇒ Object
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 32 33 34 35 36 |
# File 'lib/xunch/shard/redis.rb', line 14 def initialize( = {}) # puts options = DEFAULTS.merge() # puts options if RUBY_PLATFORM =~ /mingw/ [:driver] = nil end if([:pool_timeout] <= 0) [:pool_timeout] = 1073741823 end # p options[:driver] last_driver = Redis::Connection.drivers.last [:driver] ||= last_driver[last_driver.rindex('.'), last_driver.length].to_sym if [:driver] == :synchrony require "xunch/connection/fiber_redis_pool" # puts "fiber pool" @pool = FiberRedisPool.new() else require "xunch/connection/threaded_redis_pool" # puts "threaded pool" @pool = ThreadedRedisPool.new() end end |
Instance Method Details
#del(*keys) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/xunch/shard/redis.rb', line 48 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 |
#destroy ⇒ Object
38 39 40 |
# File 'lib/xunch/shard/redis.rb', line 38 def destroy @pool.destroy end |
#exists(key) ⇒ Object
42 43 44 45 46 |
# File 'lib/xunch/shard/redis.rb', line 42 def exists(key) with do | redis | redis.exists(key) end end |
#expire(key, ttl) ⇒ Object
62 63 64 65 66 |
# File 'lib/xunch/shard/redis.rb', line 62 def expire(key, ttl) with do | redis | redis.expire(key,ttl) end end |
#get(key) ⇒ String
get value with key
78 79 80 81 82 |
# File 'lib/xunch/shard/redis.rb', line 78 def get(key) with do | redis | redis.get(key) end end |
#hget(key, *fields) ⇒ Object
136 137 138 139 140 |
# File 'lib/xunch/shard/redis.rb', line 136 def hget(key, *fields) with do | redis | redis.mapped_hmget(key, *fields) end end |
#hgetall(key) ⇒ Object
142 143 144 145 146 |
# File 'lib/xunch/shard/redis.rb', line 142 def hgetall(key) with do | redis | redis.hgetall(key) end end |
#hmget(keys, *fields) ⇒ Object
multi get hash type keys with fields
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/xunch/shard/redis.rb', line 163 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
187 188 189 190 191 192 193 194 195 |
# File 'lib/xunch/shard/redis.rb', line 187 def hmgetall(keys) with do | redis | redis.pipelined do keys.each { | key | redis.hgetall(key) } end end end |
#hmset(hash, ttl) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/xunch/shard/redis.rb', line 174 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
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/xunch/shard/redis.rb', line 125 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
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/xunch/shard/redis.rb', line 148 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
197 198 199 200 201 |
# File 'lib/xunch/shard/redis.rb', line 197 def llen(key) with do | redis | redis.llen(key) end end |
#lrange(key, start, stop) ⇒ Object
237 238 239 240 241 |
# File 'lib/xunch/shard/redis.rb', line 237 def lrange(key, start, stop) with do | redis | redis.lrange(key,start,stop) end end |
#lrem(key, *value) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/xunch/shard/redis.rb', line 203 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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/xunch/shard/redis.rb', line 221 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
84 85 86 87 88 |
# File 'lib/xunch/shard/redis.rb', line 84 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
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/xunch/shard/redis.rb', line 111 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.
248 249 250 251 252 |
# File 'lib/xunch/shard/redis.rb', line 248 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
96 97 98 99 100 101 102 103 104 |
# File 'lib/xunch/shard/redis.rb', line 96 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
68 69 70 71 72 |
# File 'lib/xunch/shard/redis.rb', line 68 def ttl(key) with do | redis | redis.ttl(key) end end |
#type(key) ⇒ Object
254 255 256 257 258 |
# File 'lib/xunch/shard/redis.rb', line 254 def type(key) with do | redis | redis.type(key) end end |