familia 0.7.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,25 @@
1
1
 
2
2
  module Familia
3
-
3
+
4
4
  class RedisObject
5
5
  @registration = {}
6
6
  @classes = []
7
-
7
+
8
8
  # To be called inside every class that inherits RedisObject
9
9
  # +meth+ becomes the base for the class and instances methods
10
10
  # that are created for the given +klass+ (e.g. Obj.list)
11
11
  def RedisObject.register klass, meth
12
12
  registration[meth] = klass
13
13
  end
14
-
14
+
15
15
  def RedisObject.registration
16
16
  @registration
17
17
  end
18
-
18
+
19
19
  def RedisObject.classes
20
20
  @classes
21
21
  end
22
-
22
+
23
23
  @db, @ttl = nil, nil
24
24
  class << self
25
25
  attr_accessor :parent
@@ -45,40 +45,40 @@ module Familia
45
45
  super(obj)
46
46
  end
47
47
  end
48
-
48
+
49
49
  attr_reader :name, :parent
50
50
  attr_writer :redis
51
51
 
52
52
  # RedisObject instances are frozen. `cache` is a hash
53
53
  # for you to store values retreived from Redis. This is
54
54
  # not used anywhere by default, but you're encouraged
55
- # to use it in your specific scenarios.
55
+ # to use it in your specific scenarios.
56
56
  attr_reader :cache
57
-
58
- # +name+: If parent is set, this will be used as the suffix
57
+
58
+ # +name+: If parent is set, this will be used as the suffix
59
59
  # for rediskey. Otherwise this becomes the value of the key.
60
60
  # If this is an Array, the elements will be joined.
61
- #
61
+ #
62
62
  # Options:
63
63
  #
64
- # :class => A class that responds to Familia.load_method and
64
+ # :class => A class that responds to Familia.load_method and
65
65
  # Familia.dump_method. These will be used when loading and
66
- # saving data from/to redis to unmarshal/marshal the class.
66
+ # saving data from/to redis to unmarshal/marshal the class.
67
67
  #
68
68
  # :reference => When true the index of the given value will be
69
- # stored rather than the marshaled value. This assumes that
70
- # the marshaled object is stored at a separate key. When read,
71
- # from_redis looks for that separate key and returns the
72
- # unmarshaled object. :class must be specified. Default: false.
69
+ # stored rather than the marshaled value. This assumes that
70
+ # the marshaled object is stored at a separate key. When read,
71
+ # from_redis looks for that separate key and returns the
72
+ # unmarshaled object. :class must be specified. Default: false.
73
73
  #
74
- # :extend => Extend this instance with the functionality in an
74
+ # :extend => Extend this instance with the functionality in an
75
75
  # other module. Literally: "self.extend opts[:extend]".
76
76
  #
77
77
  # :parent => The Familia object that this redis object belongs
78
78
  # to. This can be a class that includes Familia or an instance.
79
- #
79
+ #
80
80
  # :ttl => the time to live in seconds. When not nil, this will
81
- # set the redis expire for this key whenever #save is called.
81
+ # set the redis expire for this key whenever #save is called.
82
82
  # You can also call it explicitly via #update_expiration.
83
83
  #
84
84
  # :quantize => append a quantized timestamp to the rediskey.
@@ -99,7 +99,7 @@ module Familia
99
99
  #
100
100
  # :redis => an instance of Redis.
101
101
  #
102
- # Uses the redis connection of the parent or the value of
102
+ # Uses the redis connection of the parent or the value of
103
103
  # opts[:redis] or Familia.redis (in that order).
104
104
  def initialize name, opts={}
105
105
  @name, @opts = name, opts
@@ -108,57 +108,57 @@ module Familia
108
108
  self.extend @opts[:extend] if Module === @opts[:extend]
109
109
  @db = @opts.delete(:db)
110
110
  @parent = @opts.delete(:parent)
111
- @ttl ||= @opts.delete(:ttl)
111
+ @ttl ||= @opts.delete(:ttl)
112
112
  @redis ||= @opts.delete(:redis)
113
113
  @cache = {}
114
114
  init if respond_to? :init
115
115
  end
116
-
116
+
117
117
  def clear_cache
118
118
  @cache.clear
119
119
  end
120
-
120
+
121
121
  def echo meth, trace
122
122
  redis.echo "[#{self.class}\##{meth}] #{trace} (#{@opts[:class]}\#)"
123
123
  end
124
-
124
+
125
125
  def redis
126
126
  return @redis if @redis
127
127
  parent? ? parent.redis : Familia.redis(db)
128
128
  end
129
-
129
+
130
130
  # Returns the most likely value for db, checking (in this order):
131
131
  # * the value from :class if it's a Familia object
132
132
  # * the value from :parent
133
133
  # * the value self.class.db
134
134
  # * assumes the db is 0
135
- #
136
- # After this is called once, this method will always return the
135
+ #
136
+ # After this is called once, this method will always return the
137
137
  # same value.
138
- def db
138
+ def db
139
139
  # Note it's important that we select this value at the last
140
- # possible moment rather than in initialize b/c the value
141
- # could be modified after that but before this is called.
140
+ # possible moment rather than in initialize b/c the value
141
+ # could be modified after that but before this is called.
142
142
  if @opts[:class] && @opts[:class].ancestors.member?(Familia)
143
- @opts[:class].db
143
+ @opts[:class].db
144
144
  elsif parent?
145
145
  parent.db
146
146
  else
147
147
  self.class.db || @db || 0
148
148
  end
149
149
  end
150
-
150
+
151
151
  def ttl
152
- @ttl ||
153
- (parent.ttl if parent?) ||
154
- (@opts[:class].ttl if class?) ||
152
+ @ttl ||
153
+ (parent.ttl if parent?) ||
154
+ (@opts[:class].ttl if class?) ||
155
155
  (self.class.ttl if self.class.respond_to?(:ttl))
156
156
  end
157
-
158
- # returns a redis key based on the parent
157
+
158
+ # returns a redis key based on the parent
159
159
  # object so it will include the proper index.
160
160
  def rediskey
161
- if parent?
161
+ if parent?
162
162
  # We need to check if the parent has a specific suffix
163
163
  # for the case where we have specified one other than :object.
164
164
  suffix = parent.kind_of?(Familia) && parent.class.suffix != :object ? parent.class.suffix : name
@@ -179,33 +179,33 @@ module Familia
179
179
  end
180
180
  k
181
181
  end
182
-
182
+
183
183
  def class?
184
184
  !@opts[:class].to_s.empty? && @opts[:class].kind_of?(Familia)
185
185
  end
186
-
186
+
187
187
  def parent?
188
188
  Class === parent || Module === parent || parent.kind_of?(Familia)
189
189
  end
190
-
190
+
191
191
  def qstamp quantum=nil, pattern=nil, now=Familia.now
192
192
  quantum ||= ttl || 10.minutes
193
193
  pattern ||= '%H%M'
194
194
  rounded = now - (now % quantum)
195
195
  Time.at(rounded).utc.strftime(pattern)
196
196
  end
197
-
197
+
198
198
  def update_expiration(ttl=nil)
199
199
  ttl ||= self.ttl
200
200
  return if ttl.to_i.zero? # nil will be zero
201
201
  Familia.ld "#{rediskey} to #{ttl}"
202
202
  expire ttl.to_i
203
203
  end
204
-
204
+
205
205
  def move db
206
206
  redis.move rediskey, db
207
207
  end
208
-
208
+
209
209
  def rename newkey
210
210
  redis.rename rediskey, newkey
211
211
  end
@@ -213,50 +213,50 @@ module Familia
213
213
  def renamenx newkey
214
214
  redis.renamenx rediskey, newkey
215
215
  end
216
-
217
- def type
216
+
217
+ def type
218
218
  redis.type rediskey
219
219
  end
220
-
221
- def delete
220
+
221
+ def delete
222
222
  redis.del rediskey
223
223
  end
224
224
  alias_method :clear, :delete
225
225
  alias_method :del, :delete
226
-
227
- #def destroy!
226
+
227
+ #def destroy!
228
228
  # clear
229
229
  # # TODO: delete redis objects for this instance
230
230
  #end
231
-
231
+
232
232
  def exists?
233
233
  redis.exists(rediskey) && !size.zero?
234
234
  end
235
-
235
+
236
236
  def realttl
237
237
  redis.ttl rediskey
238
238
  end
239
-
239
+
240
240
  def expire sec
241
241
  redis.expire rediskey, sec.to_i
242
242
  end
243
-
243
+
244
244
  def expireat unixtime
245
245
  redis.expireat rediskey, unixtime
246
246
  end
247
-
247
+
248
248
  def persist
249
249
  redis.persist rediskey
250
250
  end
251
-
251
+
252
252
  def dump_method
253
253
  @opts[:dump_method] || Familia.dump_method
254
254
  end
255
-
255
+
256
256
  def load_method
257
257
  @opts[:load_method] || Familia.load_method
258
258
  end
259
-
259
+
260
260
  def to_redis v
261
261
  return v unless @opts[:class]
262
262
  ret = case @opts[:class]
@@ -265,7 +265,7 @@ module Familia
265
265
  else
266
266
  if ::String === v
267
267
  v
268
-
268
+
269
269
  elsif @opts[:reference] == true
270
270
  unless v.respond_to? :index
271
271
  raise Familia::Problem, "#{v.class} does not have an index method"
@@ -277,17 +277,17 @@ module Familia
277
277
 
278
278
  elsif v.respond_to? dump_method
279
279
  v.send dump_method
280
-
280
+
281
281
  else
282
282
  raise Familia::Problem, "No such method: #{v.class}.#{dump_method}"
283
283
  end
284
284
  end
285
285
  if ret.nil?
286
- Familia.ld "[#{self.class}\#to_redis] nil returned for #{@opts[:class]}\##{name}"
286
+ Familia.ld "[#{self.class}\#to_redis] nil returned for #{@opts[:class]}\##{name}"
287
287
  end
288
288
  ret
289
289
  end
290
-
290
+
291
291
  def multi_from_redis *values
292
292
  Familia.ld "multi_from_redis: (#{@opts}) #{values}"
293
293
  return [] if values.empty?
@@ -301,17 +301,17 @@ module Familia
301
301
  @opts[:class].induced_from v
302
302
  else
303
303
  objs = values
304
-
304
+
305
305
  if @opts[:reference] == true
306
306
  objs = @opts[:class].rawmultiget *values
307
307
  end
308
308
  objs.compact!
309
309
  if @opts[:class].respond_to? load_method
310
- objs.collect! { |obj|
310
+ objs.collect! { |obj|
311
311
  begin
312
312
  v = @opts[:class].send load_method, obj
313
313
  if v.nil?
314
- Familia.ld "[#{self.class}\#multi_from_redis] nil returned for #{@opts[:class]}\##{name}"
314
+ Familia.ld "[#{self.class}\#multi_from_redis] nil returned for #{@opts[:class]}\##{name}"
315
315
  end
316
316
  v
317
317
  rescue => ex
@@ -328,28 +328,28 @@ module Familia
328
328
  end
329
329
  ret
330
330
  end
331
-
331
+
332
332
  def from_redis v
333
333
  return @opts[:default] if v.nil?
334
334
  return v unless @opts[:class]
335
335
  ret = multi_from_redis v
336
336
  ret.first unless ret.nil? # return the object or nil
337
- end
338
-
337
+ end
338
+
339
339
  end
340
-
341
-
340
+
341
+
342
342
  class List < RedisObject
343
-
343
+
344
344
  def size
345
345
  redis.llen rediskey
346
346
  end
347
347
  alias_method :length, :size
348
-
348
+
349
349
  def empty?
350
350
  size == 0
351
351
  end
352
-
352
+
353
353
  def push *values
354
354
  echo :push, caller[0] if Familia.debug
355
355
  values.flatten.compact.each { |v| redis.rpush rediskey, to_redis(v) }
@@ -357,12 +357,12 @@ module Familia
357
357
  update_expiration
358
358
  self
359
359
  end
360
-
360
+
361
361
  def << v
362
362
  push v
363
363
  end
364
364
  alias_method :add, :<<
365
-
365
+
366
366
  def unshift *values
367
367
  values.flatten.compact.each { |v| redis.lpush rediskey, to_redis(v) }
368
368
  # TODO: test maxlength
@@ -370,15 +370,15 @@ module Familia
370
370
  update_expiration
371
371
  self
372
372
  end
373
-
373
+
374
374
  def pop
375
375
  from_redis redis.rpop(rediskey)
376
376
  end
377
-
377
+
378
378
  def shift
379
379
  from_redis redis.lpop(rediskey)
380
380
  end
381
-
381
+
382
382
  def [] idx, count=nil
383
383
  if idx.is_a? Range
384
384
  range idx.first, idx.last
@@ -393,23 +393,23 @@ module Familia
393
393
  end
394
394
  end
395
395
  alias_method :slice, :[]
396
-
396
+
397
397
  def delete v, count=0
398
398
  redis.lrem rediskey, count, to_redis(v)
399
399
  end
400
400
  alias_method :remove, :delete
401
401
  alias_method :rem, :delete
402
402
  alias_method :del, :delete
403
-
403
+
404
404
  def range sidx=0, eidx=-1
405
405
  el = rangeraw sidx, eidx
406
406
  multi_from_redis *el
407
407
  end
408
-
408
+
409
409
  def rangeraw sidx=0, eidx=-1
410
410
  redis.lrange(rediskey, sidx, eidx)
411
411
  end
412
-
412
+
413
413
  def members count=-1
414
414
  echo :members, caller[0] if Familia.debug
415
415
  count -= 1 if count > 0
@@ -417,36 +417,36 @@ module Familia
417
417
  end
418
418
  alias_method :all, :members
419
419
  alias_method :to_a, :members
420
-
420
+
421
421
  def membersraw count=-1
422
422
  count -= 1 if count > 0
423
423
  rangeraw 0, count
424
424
  end
425
-
425
+
426
426
  #def revmembers count=1 #TODO
427
427
  # range -count, 0
428
428
  #end
429
-
429
+
430
430
  def each &blk
431
431
  range.each &blk
432
432
  end
433
-
433
+
434
434
  def each_with_index &blk
435
435
  range.each_with_index &blk
436
436
  end
437
-
437
+
438
438
  def eachraw &blk
439
439
  rangeraw.each &blk
440
440
  end
441
-
441
+
442
442
  def eachraw_with_index &blk
443
443
  rangeraw.each_with_index &blk
444
444
  end
445
-
445
+
446
446
  def collect &blk
447
447
  range.collect &blk
448
448
  end
449
-
449
+
450
450
  def select &blk
451
451
  range.select &blk
452
452
  end
@@ -454,15 +454,15 @@ module Familia
454
454
  def collectraw &blk
455
455
  rangeraw.collect &blk
456
456
  end
457
-
457
+
458
458
  def selectraw &blk
459
459
  rangeraw.select &blk
460
460
  end
461
-
461
+
462
462
  def at idx
463
463
  from_redis redis.lindex(rediskey, idx)
464
464
  end
465
-
465
+
466
466
  def first
467
467
  at 0
468
468
  end
@@ -470,12 +470,12 @@ module Familia
470
470
  def last
471
471
  at -1
472
472
  end
473
-
473
+
474
474
  # TODO: def replace
475
475
  ## Make the value stored at KEY identical to the given list
476
476
  #define_method :"#{name}_sync" do |*latest|
477
477
  # latest = latest.flatten.compact
478
- # # Do nothing if we're given an empty Array.
478
+ # # Do nothing if we're given an empty Array.
479
479
  # # Otherwise this would clear all current values
480
480
  # if latest.empty?
481
481
  # false
@@ -492,31 +492,31 @@ module Familia
492
492
  # true
493
493
  # end
494
494
  #end
495
-
495
+
496
496
  Familia::RedisObject.register self, :list
497
497
  end
498
-
498
+
499
499
  class Set < RedisObject
500
-
500
+
501
501
  def size
502
502
  redis.scard rediskey
503
503
  end
504
504
  alias_method :length, :size
505
-
505
+
506
506
  def empty?
507
507
  size == 0
508
508
  end
509
-
509
+
510
510
  def add *values
511
511
  values.flatten.compact.each { |v| redis.sadd rediskey, to_redis(v) }
512
512
  update_expiration
513
513
  self
514
514
  end
515
-
515
+
516
516
  def << v
517
517
  add v
518
518
  end
519
-
519
+
520
520
  def members
521
521
  echo :members, caller[0] if Familia.debug
522
522
  el = membersraw
@@ -528,19 +528,19 @@ module Familia
528
528
  def membersraw
529
529
  redis.smembers(rediskey)
530
530
  end
531
-
531
+
532
532
  def each &blk
533
533
  members.each &blk
534
534
  end
535
-
535
+
536
536
  def each_with_index &blk
537
537
  members.each_with_index &blk
538
538
  end
539
-
539
+
540
540
  def collect &blk
541
541
  members.collect &blk
542
542
  end
543
-
543
+
544
544
  def select &blk
545
545
  members.select &blk
546
546
  end
@@ -548,43 +548,43 @@ module Familia
548
548
  def eachraw &blk
549
549
  membersraw.each &blk
550
550
  end
551
-
551
+
552
552
  def eachraw_with_index &blk
553
553
  membersraw.each_with_index &blk
554
554
  end
555
-
555
+
556
556
  def collectraw &blk
557
557
  membersraw.collect &blk
558
558
  end
559
-
559
+
560
560
  def selectraw &blk
561
561
  membersraw.select &blk
562
562
  end
563
-
563
+
564
564
  def member? v
565
565
  redis.sismember rediskey, to_redis(v)
566
566
  end
567
567
  alias_method :include?, :member?
568
-
568
+
569
569
  def delete v
570
570
  redis.srem rediskey, to_redis(v)
571
571
  end
572
572
  alias_method :remove, :delete
573
573
  alias_method :rem, :delete
574
574
  alias_method :del, :delete
575
-
575
+
576
576
  def intersection *setkeys
577
577
  # TODO
578
578
  end
579
-
579
+
580
580
  def pop
581
581
  redis.spop rediskey
582
582
  end
583
-
583
+
584
584
  def move dstkey, v
585
585
  redis.smove rediskey, dstkey, v
586
586
  end
587
-
587
+
588
588
  def random
589
589
  from_redis randomraw
590
590
  end
@@ -592,11 +592,11 @@ module Familia
592
592
  def randomraw
593
593
  redis.srandmember(rediskey)
594
594
  end
595
-
595
+
596
596
  ## Make the value stored at KEY identical to the given list
597
597
  #define_method :"#{name}_sync" do |*latest|
598
598
  # latest = latest.flatten.compact
599
- # # Do nothing if we're given an empty Array.
599
+ # # Do nothing if we're given an empty Array.
600
600
  # # Otherwise this would clear all current values
601
601
  # if latest.empty?
602
602
  # false
@@ -613,57 +613,57 @@ module Familia
613
613
  # true
614
614
  # end
615
615
  #end
616
-
616
+
617
617
  Familia::RedisObject.register self, :set
618
618
  end
619
-
619
+
620
620
  class SortedSet < RedisObject
621
-
621
+
622
622
  def size
623
623
  redis.zcard rediskey
624
624
  end
625
625
  alias_method :length, :size
626
-
626
+
627
627
  def empty?
628
628
  size == 0
629
629
  end
630
-
630
+
631
631
  # NOTE: The argument order is the reverse of #add
632
632
  # e.g. obj.metrics[VALUE] = SCORE
633
633
  def []= v, score
634
634
  add score, v
635
635
  end
636
-
636
+
637
637
  # NOTE: The argument order is the reverse of #[]=
638
638
  def add score, v
639
639
  ret = redis.zadd rediskey, score, to_redis(v)
640
640
  update_expiration
641
641
  ret
642
642
  end
643
-
643
+
644
644
  def score v
645
645
  ret = redis.zscore rediskey, to_redis(v)
646
646
  ret.nil? ? nil : ret.to_f
647
647
  end
648
648
  alias_method :[], :score
649
-
649
+
650
650
  def member? v
651
651
  !rank(v).nil?
652
652
  end
653
653
  alias_method :include?, :member?
654
-
654
+
655
655
  # rank of member +v+ when ordered lowest to highest (starts at 0)
656
656
  def rank v
657
657
  ret = redis.zrank rediskey, to_redis(v)
658
658
  ret.nil? ? nil : ret.to_i
659
659
  end
660
-
660
+
661
661
  # rank of member +v+ when ordered highest to lowest (starts at 0)
662
662
  def revrank v
663
663
  ret = redis.zrevrank rediskey, to_redis(v)
664
664
  ret.nil? ? nil : ret.to_i
665
665
  end
666
-
666
+
667
667
  def members count=-1, opts={}
668
668
  count -= 1 if count > 0
669
669
  el = membersraw count, opts
@@ -676,7 +676,7 @@ module Familia
676
676
  count -= 1 if count > 0
677
677
  rangeraw 0, count, opts
678
678
  end
679
-
679
+
680
680
  def revmembers count=-1, opts={}
681
681
  count -= 1 if count > 0
682
682
  el = revmembersraw count, opts
@@ -687,7 +687,7 @@ module Familia
687
687
  count -= 1 if count > 0
688
688
  revrangeraw 0, count, opts
689
689
  end
690
-
690
+
691
691
  def each &blk
692
692
  members.each &blk
693
693
  end
@@ -695,15 +695,15 @@ module Familia
695
695
  def each_with_index &blk
696
696
  members.each_with_index &blk
697
697
  end
698
-
698
+
699
699
  def collect &blk
700
700
  members.collect &blk
701
701
  end
702
-
702
+
703
703
  def select &blk
704
704
  members.select &blk
705
705
  end
706
-
706
+
707
707
  def eachraw &blk
708
708
  membersraw.each &blk
709
709
  end
@@ -711,50 +711,54 @@ module Familia
711
711
  def eachraw_with_index &blk
712
712
  membersraw.each_with_index &blk
713
713
  end
714
-
714
+
715
715
  def collectraw &blk
716
716
  membersraw.collect &blk
717
717
  end
718
-
718
+
719
719
  def selectraw &blk
720
720
  membersraw.select &blk
721
721
  end
722
-
722
+
723
723
  def range sidx, eidx, opts={}
724
724
  echo :range, caller[0] if Familia.debug
725
725
  el = rangeraw(sidx, eidx, opts)
726
726
  multi_from_redis *el
727
727
  end
728
-
728
+
729
729
  def rangeraw sidx, eidx, opts={}
730
- opts[:with_scores] = true if opts[:withscores]
731
- redis.zrange(rediskey, sidx, eidx, opts)
730
+ # NOTE: :withscores (no underscore) is the correct naming for the
731
+ # redis-4.x gem. We pass :withscores through explicitly b/c
732
+ # redis.zrange et al only accept that one optional argument.
733
+ # Passing `opts`` through leads to an ArgumentError:
734
+ #
735
+ # sorted_sets.rb:374:in `zrevrange': wrong number of arguments (given 4, expected 3) (ArgumentError)
736
+ #
737
+ redis.zrange(rediskey, sidx, eidx, :withscores => opts[:withscores])
732
738
  end
733
-
739
+
734
740
  def revrange sidx, eidx, opts={}
735
741
  echo :revrange, caller[0] if Familia.debug
736
742
  el = revrangeraw(sidx, eidx, opts)
737
743
  multi_from_redis *el
738
744
  end
739
-
745
+
740
746
  def revrangeraw sidx, eidx, opts={}
741
- opts[:with_scores] = true if opts[:withscores]
742
- redis.zrevrange(rediskey, sidx, eidx, opts)
747
+ redis.zrevrange(rediskey, sidx, eidx, :withscores => opts[:withscores])
743
748
  end
744
-
749
+
745
750
  # e.g. obj.metrics.rangebyscore (now-12.hours), now, :limit => [0, 10]
746
751
  def rangebyscore sscore, escore, opts={}
747
752
  echo :rangebyscore, caller[0] if Familia.debug
748
753
  el = rangebyscoreraw(sscore, escore, opts)
749
754
  multi_from_redis *el
750
755
  end
751
-
756
+
752
757
  def rangebyscoreraw sscore, escore, opts={}
753
758
  echo :rangebyscoreraw, caller[0] if Familia.debug
754
- opts[:with_scores] = true if opts[:withscores]
755
- redis.zrangebyscore(rediskey, sscore, escore, opts)
759
+ redis.zrangebyscore(rediskey, sscore, escore, :withscores => opts[:withscores])
756
760
  end
757
-
761
+
758
762
  def remrangebyrank srank, erank
759
763
  redis.zremrangebyrank rediskey, srank, erank
760
764
  end
@@ -762,7 +766,7 @@ module Familia
762
766
  def remrangebyscore sscore, escore
763
767
  redis.zremrangebyscore rediskey, sscore, escore
764
768
  end
765
-
769
+
766
770
  def increment v, by=1
767
771
  redis.zincrby(rediskey, by, v).to_i
768
772
  end
@@ -774,14 +778,14 @@ module Familia
774
778
  end
775
779
  alias_method :decr, :decrement
776
780
  alias_method :decrby, :decrement
777
-
781
+
778
782
  def delete v
779
783
  redis.zrem rediskey, to_redis(v)
780
784
  end
781
785
  alias_method :remove, :delete
782
786
  alias_method :rem, :delete
783
787
  alias_method :del, :delete
784
-
788
+
785
789
  def at idx
786
790
  range(idx, idx).first
787
791
  end
@@ -795,21 +799,21 @@ module Familia
795
799
  def last
796
800
  at(-1)
797
801
  end
798
-
802
+
799
803
  Familia::RedisObject.register self, :zset
800
804
  end
801
805
 
802
806
  class HashKey < RedisObject
803
-
807
+
804
808
  def size
805
809
  redis.hlen rediskey
806
810
  end
807
811
  alias_method :length, :size
808
-
812
+
809
813
  def empty?
810
814
  size == 0
811
815
  end
812
-
816
+
813
817
  def []= n, v
814
818
  ret = redis.hset rediskey, n, to_redis(v)
815
819
  update_expiration
@@ -817,63 +821,63 @@ module Familia
817
821
  end
818
822
  alias_method :put, :[]=
819
823
  alias_method :store, :[]=
820
-
824
+
821
825
  def [] n
822
826
  from_redis redis.hget(rediskey, n)
823
827
  end
824
828
  alias_method :get, :[]
825
-
829
+
826
830
  def fetch n, default=nil
827
831
  ret = self[n]
828
- if ret.nil?
832
+ if ret.nil?
829
833
  raise IndexError.new("No such index for: #{n}") if default.nil?
830
834
  default
831
835
  else
832
836
  ret
833
837
  end
834
838
  end
835
-
839
+
836
840
  def keys
837
841
  redis.hkeys rediskey
838
842
  end
839
-
843
+
840
844
  def values
841
845
  el = redis.hvals(rediskey)
842
846
  multi_from_redis *el
843
847
  end
844
-
848
+
845
849
  def all
846
850
  # TODO: from_redis
847
851
  redis.hgetall rediskey
848
852
  end
849
853
  alias_method :to_hash, :all
850
854
  alias_method :clone, :all
851
-
855
+
852
856
  def has_key? n
853
857
  redis.hexists rediskey, n
854
858
  end
855
859
  alias_method :include?, :has_key?
856
860
  alias_method :member?, :has_key?
857
-
861
+
858
862
  def delete n
859
863
  redis.hdel rediskey, n
860
864
  end
861
865
  alias_method :remove, :delete
862
866
  alias_method :rem, :delete
863
867
  alias_method :del, :delete
864
-
868
+
865
869
  def increment n, by=1
866
870
  redis.hincrby(rediskey, n, by).to_i
867
871
  end
868
872
  alias_method :incr, :increment
869
873
  alias_method :incrby, :increment
870
-
874
+
871
875
  def decrement n, by=1
872
876
  increment n, -by
873
877
  end
874
878
  alias_method :decr, :decrement
875
879
  alias_method :decrby, :decrement
876
-
880
+
877
881
  def update h={}
878
882
  raise ArgumentError, "Argument to bulk_set must be a hash" unless Hash === h
879
883
  data = h.inject([]){ |ret,pair| ret << [pair[0], to_redis(pair[1])] }.flatten
@@ -882,29 +886,29 @@ module Familia
882
886
  ret
883
887
  end
884
888
  alias_method :merge!, :update
885
-
889
+
886
890
  def values_at *names
887
891
  el = redis.hmget(rediskey, *names.flatten.compact)
888
892
  multi_from_redis *el
889
893
  end
890
-
894
+
891
895
  Familia::RedisObject.register self, :hash
892
896
  end
893
-
897
+
894
898
  class String < RedisObject
895
-
899
+
896
900
  def init
897
901
  end
898
-
902
+
899
903
  def size
900
904
  to_s.size
901
905
  end
902
906
  alias_method :length, :size
903
-
907
+
904
908
  def empty?
905
909
  size == 0
906
910
  end
907
-
911
+
908
912
  def value
909
913
  echo :value, caller[0..5] if Familia.debug
910
914
  redis.setnx rediskey, @opts[:default] if @opts[:default]
@@ -912,29 +916,29 @@ module Familia
912
916
  end
913
917
  alias_method :content, :value
914
918
  alias_method :get, :value
915
-
919
+
916
920
  def to_s
917
921
  value.to_s # value can return nil which to_s should not
918
922
  end
919
-
923
+
920
924
  def to_i
921
925
  value.to_i
922
926
  end
923
-
927
+
924
928
  def value= v
925
929
  ret = redis.set rediskey, to_redis(v)
926
930
  update_expiration
927
931
  ret
928
932
  end
929
933
  alias_method :replace, :value=
930
- alias_method :set, :value=
931
-
934
+ alias_method :set, :value=
935
+
932
936
  def setnx v
933
937
  ret = redis.setnx rediskey, to_redis(v)
934
938
  update_expiration
935
939
  ret
936
940
  end
937
-
941
+
938
942
  def increment
939
943
  ret = redis.incr rediskey
940
944
  update_expiration
@@ -962,7 +966,7 @@ module Familia
962
966
  ret
963
967
  end
964
968
  alias_method :decrby, :decrementby
965
-
969
+
966
970
  def append v
967
971
  ret = redis.append rediskey, v
968
972
  update_expiration
@@ -989,19 +993,18 @@ module Familia
989
993
  update_expiration
990
994
  ret
991
995
  end
992
-
996
+
993
997
  def getset v
994
998
  ret = redis.getset rediskey, v
995
999
  update_expiration
996
1000
  ret
997
1001
  end
998
-
1002
+
999
1003
  def nil?
1000
1004
  value.nil?
1001
1005
  end
1002
-
1006
+
1003
1007
  Familia::RedisObject.register self, :string
1004
1008
  end
1005
-
1006
- end
1007
1009
 
1010
+ end