Skip to content

Commit

Permalink
ADDED: RedisObject option: quantize
Browse files Browse the repository at this point in the history
  • Loading branch information
delano committed Apr 10, 2011
1 parent 2874c47 commit 89cd340
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
@@ -1,11 +1,13 @@
FAMILIA, CHANGES


#### 0.7.1 (2011-03-??) ###############################
#### 0.7.1 (2011-04-09) ###############################

* FIXED: Explicitly convert boolean conf options to true or false. Redis client
treats "true" as false.
* CHANGE: Fix for Familia objects with custom suffixes
* ADDED: Familia.qnow, Familia::Object.qstamp, RedisObject#qstamp
* ADDED: RedisObject option: quantize


#### 0.7.0 (2011-03-04) ###############################
Expand Down
11 changes: 11 additions & 0 deletions lib/familia/object.rb
Expand Up @@ -97,6 +97,13 @@ def install_redis_object name, klass, opts
redis_objects[name]
end

def qstamp quantum=nil, pattern=nil, now=Familia.now
quantum ||= ttl || 10.minutes
pattern ||= '%H%M'
rounded = now - (now % quantum)
Time.at(rounded).utc.strftime(pattern)
end

# Creates a class method called +name+ that
# returns an instance of the RedisObject +klass+
def install_class_redis_object name, klass, opts
Expand Down Expand Up @@ -335,6 +342,10 @@ def initialize_redis_objects
end
end

def qstamp quantum=nil, pattern=nil, now=Familia.now
self.class.qstamp ttl, pattern, now
end

def redis
self.class.redis
end
Expand Down
25 changes: 23 additions & 2 deletions lib/familia/redisobject.rb
Expand Up @@ -83,6 +83,12 @@ def inherited(obj)
#
# :default => the default value (String-only)
#
# :dump_method => the instance method to call to serialize the
# object before sending it to Redis (default: Familia.dump_method).
#
# :load_method => the class method to call to deserialize the
# object after it's read from Redis (default: Familia.load_method).
#
# :db => the redis database to use (ignored if :redis is used).
#
# :redis => an instance of Redis.
Expand Down Expand Up @@ -150,10 +156,12 @@ def rediskey
# We need to check if the parent has a specific suffix
# for the case where we have specified one other than :object.
suffix = parent.kind_of?(Familia) && parent.class.suffix != :object ? parent.class.suffix : name
parent.rediskey(name, nil)
k = parent.rediskey(name, nil)
else
[name].flatten.compact.join(Familia.delim)
k = [name].flatten.compact.join(Familia.delim)
end
k = [k, qstamp].join(Familia.delim) if @opts[:quantize]
k
end

def class?
Expand All @@ -164,6 +172,13 @@ def parent?
Class === parent || Module === parent || parent.kind_of?(Familia)
end

def qstamp quantum=nil, pattern=nil, now=Familia.now
quantum ||= ttl || 10.minutes
pattern ||= '%H%M'
rounded = now - (now % quantum)
Time.at(rounded).utc.strftime(pattern)
end

def update_expiration(ttl=nil)
ttl ||= self.ttl
return if ttl.to_i.zero? # nil will be zero
Expand Down Expand Up @@ -898,6 +913,12 @@ def value= v
alias_method :replace, :value=
alias_method :set, :value=

def setnx v
ret = redis.set rediskey, to_redis(v)
update_expiration
ret
end

def increment
redis.incr rediskey
end
Expand Down
6 changes: 6 additions & 0 deletions lib/familia/test_helpers.rb
Expand Up @@ -32,3 +32,9 @@ class Customer < Storable
class_string :message
end

class Limiter < Storable
include Familia
index :name
field :name
string :counter, :quantize => true, :ttl => 1.hour
end
12 changes: 11 additions & 1 deletion try/10_familia_try.rb
Expand Up @@ -41,4 +41,14 @@

## Familia.qnow
Familia.qnow 10.minutes, 1302468980
#=> 1302468600
#=> 1302468600

## Familia::Object.qstamp
Limiter.qstamp 10.minutes, '%H:%M', 1302468980
#=> '20:50'

## Familia::Object#qstamp
limiter = Limiter.new :request
limiter.qstamp 10.minutes, '%H:%M', 1302468980
#=> '20:50'

20 changes: 20 additions & 0 deletions try/20_redis_object_try.rb
Expand Up @@ -15,4 +15,24 @@
#=> true


## Limiter#qstamp
@limiter = Limiter.new :requests
@limiter.counter.qstamp 10.minutes, '%H:%M', 1302468980
#=> '20:50'

## Redis Objects can be stored to quantized keys
Familia.split(@limiter.counter.rediskey).size
#=> 5

## Increment counter
@limiter.counter.clear
@limiter.counter.increment
#=> 1

## Check ttl
@limiter.counter.ttl
#=> 3600

## Check realttl
@limiter.counter.realttl
#=> 3600

0 comments on commit 89cd340

Please sign in to comment.