Class: SSDB
- Inherits:
-
Object
- Object
- SSDB
- Includes:
- MonitorMixin
- Defined in:
- lib/ssdb.rb,
lib/ssdb/client.rb,
lib/ssdb/version.rb
Defined Under Namespace
Classes: Batch, Client, Future
Constant Summary collapse
- Error =
Class.new(RuntimeError)
- ConnectionError =
Class.new(Error)
- TimeoutError =
Class.new(Error)
- CommandError =
Class.new(Error)
- FutureNotReady =
Class.new(Error)
- T_BOOL =
->r { r == "1" }
- T_INT =
->r { r.to_i }
- T_CINT =
->r { r.to_i if r }
- T_VBOOL =
->r { r.each_slice(2).map {|_, v| v == "1" }}
- T_VINT =
->r { r.each_slice(2).map {|_, v| v.to_i }}
- T_STRSTR =
->r { r.each_slice(2).to_a }
- T_STRINT =
->r { r.each_slice(2).map {|v, s| [v, s.to_i] } }
- T_MAPINT =
->r,n { h = {}; r.each_slice(2) {|k, v| h[k] = v }; n.map {|k| h[k].to_i } }
- T_MAPSTR =
->r,n { h = {}; r.each_slice(2) {|k, v| h[k] = v }; n.map {|k| h[k] } }
- BLANK =
"".freeze
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
- #client ⇒ Object readonly
Class Method Summary collapse
-
.current ⇒ SSDB
The current/global SSDB connection.
- .current=(ssdb) ⇒ Object
Instance Method Summary collapse
-
#batch ⇒ Object
Execute a batch operation.
-
#decr(key, value = 1) ⇒ Object
Decrements a ‘key` by value.
-
#del(key) ⇒ Object
Delete ‘key`.
-
#exists(key) ⇒ Boolean
(also: #exists?)
Checks existence of ‘key`.
-
#get(key) ⇒ String
Returns value at ‘key`.
-
#incr(key, value = 1) ⇒ Object
Increments a ‘key` by value.
-
#initialize(*a) ⇒ SSDB
constructor
A new instance of SSDB.
-
#keys(start, stop, opts = {}) ⇒ Array<String>
Scans keys between ‘start` and `stop`.
-
#multi_del(keys) ⇒ Object
Deletes multiple keys.
-
#multi_exists(keys) ⇒ Array<Boolean>
Checks existence of multiple keys.
-
#multi_get(keys) ⇒ Array<String>
Retrieves multiple keys.
-
#multi_set(pairs) ⇒ Object
Sets multiple keys.
-
#multi_zdel(key, members) ⇒ Object
Deletes multiple members from ‘key`.
-
#multi_zexists(keys) ⇒ Array<Boolean>
Checks existence of multiple sets.
-
#multi_zget(key, members) ⇒ Array<Float>
Retrieves multiple scores from ‘key`.
-
#multi_zset(key, pairs) ⇒ Object
Sets multiple members of ‘key`.
-
#multi_zsize(keys) ⇒ Array<Boolean>
Returns cardinalities of multiple sets.
-
#rscan(start, stop, opts = {}) ⇒ Array<Array<String,String>>
Reverse-scans keys between ‘start` and `stop`.
-
#scan(start, stop, opts = {}) ⇒ Array<Array<String,String>>
Scans keys between ‘start` and `stop`.
-
#set(key, value) ⇒ Object
Sets ‘value` at `key`.
-
#zadd(key, score, member) ⇒ Object
Redis ‘compatibility’.
-
#zdecr(key, member, score = 1) ⇒ Object
Decrements the ‘member` in `key` by `score`.
-
#zdel(key, member) ⇒ Object
Delete an ‘member` from a zset `key`.
-
#zexists(key) ⇒ Boolean
(also: #zexists?)
Checks existence of a zset at ‘key`.
-
#zget(key, member) ⇒ Float
Returns the score of ‘member` at `key`.
-
#zincr(key, member, score = 1) ⇒ Object
Increments the ‘member` in `key` by `score`.
-
#zkeys(key, start, stop, opts = {}) ⇒ Array<String>
Lists members at ‘key` starting at `start_member` between `start` and `stop` scores.
-
#zlist(start, stop, opts = {}) ⇒ Array<String>
List zset keys between ‘start` and `stop`.
-
#zrscan(key, start, stop, opts = {}) ⇒ Array<Array<String,Float>>
Reverse scans for members at ‘key` starting at `start_member` between `start` and `stop` scores.
-
#zscan(key, start, stop, opts = {}) ⇒ Array<Array<String,Float>>
Scans for members at ‘key` starting at `start_member` between `start` and `stop` scores.
-
#zset(key, member, score) ⇒ Object
Sets the ‘score` of `member` at `key`.
-
#zsize(key) ⇒ Object
Returns the cardinality of a set ‘key`.
Constructor Details
Instance Attribute Details
#client ⇒ Object (readonly)
24 25 26 |
# File 'lib/ssdb.rb', line 24 def client @client end |
Class Method Details
.current ⇒ SSDB
Returns the current/global SSDB connection.
27 28 29 |
# File 'lib/ssdb.rb', line 27 def self.current @current ||= SSDB.new end |
.current=(ssdb) ⇒ Object
32 33 34 |
# File 'lib/ssdb.rb', line 32 def self.current=(ssdb) @current = ssdb end |
Instance Method Details
#batch ⇒ Object
Execute a batch operation
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ssdb.rb', line 65 def batch mon_synchronize do begin original, @client = @client, SSDB::Batch.new yield(self) @client.values = original.perform(@client) ensure @client = original end end end |
#decr(key, value = 1) ⇒ Object
Decrements a ‘key` by value
111 112 113 114 115 |
# File 'lib/ssdb.rb', line 111 def decr(key, value = 1) mon_synchronize do perform ["decr", key, value], proc: T_INT end end |
#del(key) ⇒ Object
Delete ‘key`.
131 132 133 134 135 |
# File 'lib/ssdb.rb', line 131 def del(key) mon_synchronize do perform ["del", key] end end |
#exists(key) ⇒ Boolean Also known as: exists?
Checks existence of ‘key`.
121 122 123 124 125 |
# File 'lib/ssdb.rb', line 121 def exists(key) mon_synchronize do perform ["exists", key], proc: T_BOOL end end |
#get(key) ⇒ String
Returns value at ‘key`.
81 82 83 84 85 |
# File 'lib/ssdb.rb', line 81 def get(key) mon_synchronize do perform ["get", key] end end |
#incr(key, value = 1) ⇒ Object
Increments a ‘key` by value
101 102 103 104 105 |
# File 'lib/ssdb.rb', line 101 def incr(key, value = 1) mon_synchronize do perform ["incr", key, value], proc: T_INT end end |
#keys(start, stop, opts = {}) ⇒ Array<String>
Scans keys between ‘start` and `stop`.
144 145 146 147 148 149 |
# File 'lib/ssdb.rb', line 144 def keys(start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["keys", start, stop, limit], multi: true end end |
#multi_del(keys) ⇒ Object
Deletes multiple keys
202 203 204 205 206 207 |
# File 'lib/ssdb.rb', line 202 def multi_del(keys) keys = Array(keys) unless keys.is_a?(Array) mon_synchronize do perform ["multi_del", *keys], proc: T_INT end end |
#multi_exists(keys) ⇒ Array<Boolean>
Checks existence of multiple keys
213 214 215 216 217 218 |
# File 'lib/ssdb.rb', line 213 def multi_exists(keys) keys = Array(keys) unless keys.is_a?(Array) mon_synchronize do perform ["multi_exists", *keys], multi: true, proc: T_VBOOL end end |
#multi_get(keys) ⇒ Array<String>
Retrieves multiple keys
192 193 194 195 196 197 |
# File 'lib/ssdb.rb', line 192 def multi_get(keys) keys = Array(keys) unless keys.is_a?(Array) mon_synchronize do perform ["multi_get", *keys], multi: true, proc: T_MAPSTR, args: [keys] end end |
#multi_set(pairs) ⇒ Object
Sets multiple keys
182 183 184 185 186 |
# File 'lib/ssdb.rb', line 182 def multi_set(pairs) mon_synchronize do perform ["multi_set", *pairs.to_a].flatten, proc: T_INT end end |
#multi_zdel(key, members) ⇒ Object
Deletes multiple members from ‘key`
413 414 415 416 417 418 |
# File 'lib/ssdb.rb', line 413 def multi_zdel(key, members) members = Array(members) unless members.is_a?(Array) mon_synchronize do perform ["multi_zdel", key, *members], proc: T_INT end end |
#multi_zexists(keys) ⇒ Array<Boolean>
Checks existence of multiple sets
369 370 371 372 373 374 |
# File 'lib/ssdb.rb', line 369 def multi_zexists(keys) keys = Array(keys) unless keys.is_a?(Array) mon_synchronize do perform ["multi_zexists", *keys], multi: true, proc: T_VBOOL end end |
#multi_zget(key, members) ⇒ Array<Float>
Retrieves multiple scores from ‘key`
402 403 404 405 406 407 |
# File 'lib/ssdb.rb', line 402 def multi_zget(key, members) members = Array(members) unless members.is_a?(Array) mon_synchronize do perform ["multi_zget", key, *members], multi: true, proc: T_MAPINT, args: [members] end end |
#multi_zset(key, pairs) ⇒ Object
Sets multiple members of ‘key`
391 392 393 394 395 |
# File 'lib/ssdb.rb', line 391 def multi_zset(key, pairs) mon_synchronize do perform ["multi_zset", key, *pairs.to_a].flatten, proc: T_INT end end |
#multi_zsize(keys) ⇒ Array<Boolean>
Returns cardinalities of multiple sets
380 381 382 383 384 385 |
# File 'lib/ssdb.rb', line 380 def multi_zsize(keys) keys = Array(keys) unless keys.is_a?(Array) mon_synchronize do perform ["multi_zsize", *keys], multi: true, proc: T_VINT end end |
#rscan(start, stop, opts = {}) ⇒ Array<Array<String,String>>
Reverse-scans keys between ‘start` and `stop`.
172 173 174 175 176 177 |
# File 'lib/ssdb.rb', line 172 def rscan(start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["rscan", start, stop, limit], multi: true, proc: T_STRSTR end end |
#scan(start, stop, opts = {}) ⇒ Array<Array<String,String>>
Scans keys between ‘start` and `stop`.
158 159 160 161 162 163 |
# File 'lib/ssdb.rb', line 158 def scan(start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["scan", start, stop, limit], multi: true, proc: T_STRSTR end end |
#set(key, value) ⇒ Object
Sets ‘value` at `key`.
91 92 93 94 95 |
# File 'lib/ssdb.rb', line 91 def set(key, value) mon_synchronize do perform ["set", key, value], proc: T_BOOL end end |
#zadd(key, score, member) ⇒ Object
Redis ‘compatibility’.
247 248 249 |
# File 'lib/ssdb.rb', line 247 def zadd(key, score, member) zset(key, member, score) end |
#zdecr(key, member, score = 1) ⇒ Object
Decrements the ‘member` in `key` by `score`
267 268 269 270 271 |
# File 'lib/ssdb.rb', line 267 def zdecr(key, member, score = 1) mon_synchronize do perform ["zdecr", key, member, score], proc: T_INT end end |
#zdel(key, member) ⇒ Object
Delete an ‘member` from a zset `key`.
297 298 299 300 301 |
# File 'lib/ssdb.rb', line 297 def zdel(key, member) mon_synchronize do perform ["zdel", key, member], proc: T_BOOL end end |
#zexists(key) ⇒ Boolean Also known as: zexists?
Checks existence of a zset at ‘key`.
277 278 279 280 281 |
# File 'lib/ssdb.rb', line 277 def zexists(key) mon_synchronize do perform ["zexists", key], proc: T_BOOL end end |
#zget(key, member) ⇒ Float
Returns the score of ‘member` at `key`.
225 226 227 228 229 |
# File 'lib/ssdb.rb', line 225 def zget(key, member) mon_synchronize do perform ["zget", key, member], proc: T_CINT end end |
#zincr(key, member, score = 1) ⇒ Object
Increments the ‘member` in `key` by `score`
256 257 258 259 260 |
# File 'lib/ssdb.rb', line 256 def zincr(key, member, score = 1) mon_synchronize do perform ["zincr", key, member, score], proc: T_INT end end |
#zkeys(key, start, stop, opts = {}) ⇒ Array<String>
Lists members at ‘key` starting at `start_member` between `start` and `stop` scores.
326 327 328 329 330 331 |
# File 'lib/ssdb.rb', line 326 def zkeys(key, start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["zkeys", key, BLANK, start, stop, limit], multi: true end end |
#zlist(start, stop, opts = {}) ⇒ Array<String>
List zset keys between ‘start` and `stop`.
310 311 312 313 314 315 |
# File 'lib/ssdb.rb', line 310 def zlist(start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["zlist", start, stop, limit], multi: true end end |
#zrscan(key, start, stop, opts = {}) ⇒ Array<Array<String,Float>>
Reverse scans for members at ‘key` starting at `start_member` between `start` and `stop` scores.
358 359 360 361 362 363 |
# File 'lib/ssdb.rb', line 358 def zrscan(key, start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["zrscan", key, BLANK, start, stop, limit], multi: true, proc: T_STRINT end end |
#zscan(key, start, stop, opts = {}) ⇒ Array<Array<String,Float>>
Scans for members at ‘key` starting at `start_member` between `start` and `stop` scores.
342 343 344 345 346 347 |
# File 'lib/ssdb.rb', line 342 def zscan(key, start, stop, opts = {}) limit = opts[:limit] || -1 mon_synchronize do perform ["zscan", key, BLANK, start, stop, limit], multi: true, proc: T_STRINT end end |
#zset(key, member, score) ⇒ Object
Sets the ‘score` of `member` at `key`.
236 237 238 239 240 |
# File 'lib/ssdb.rb', line 236 def zset(key, member, score) mon_synchronize do perform ["zset", key, member, score], proc: T_BOOL end end |
#zsize(key) ⇒ Object
Returns the cardinality of a set ‘key`.
287 288 289 290 291 |
# File 'lib/ssdb.rb', line 287 def zsize(key) mon_synchronize do perform ["zsize", key], proc: T_INT end end |