redis cache populates
This commit is contained in:
parent
01515725fe
commit
289d582ce9
2 changed files with 8 additions and 20 deletions
|
|
@ -11,7 +11,6 @@ class RedisCache
|
||||||
def initialize(user, order_field)
|
def initialize(user, order_field)
|
||||||
@user = user
|
@user = user
|
||||||
@order_field = order_field.to_s
|
@order_field = order_field.to_s
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
|
|
@ -29,19 +28,14 @@ class RedisCache
|
||||||
post_ids[0...limit]
|
post_ids[0...limit]
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [RedisCache] self
|
|
||||||
def ensure_populated!
|
def ensure_populated!
|
||||||
self.repopulate! unless cache_exists?
|
self.repopulate! unless cache_exists?
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [RedisCache] self
|
|
||||||
def repopulate!
|
def repopulate!
|
||||||
self.populate! && self.trim!
|
self.populate! && self.trim!
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [RedisCache] self
|
|
||||||
def populate!
|
def populate!
|
||||||
# user executes query and gets back hashes
|
# user executes query and gets back hashes
|
||||||
sql = @user.visible_posts_sql(:limit => CACHE_LIMIT, :order => self.order)
|
sql = @user.visible_posts_sql(:limit => CACHE_LIMIT, :order => self.order)
|
||||||
|
|
@ -50,19 +44,13 @@ class RedisCache
|
||||||
# hashes are inserted into set in a single transaction
|
# hashes are inserted into set in a single transaction
|
||||||
redis.multi do
|
redis.multi do
|
||||||
hashes.each do |h|
|
hashes.each do |h|
|
||||||
self.redis.zadd(set_key, h[@order_field], h["id"])
|
self.redis.zadd(set_key, h[@order_field].to_i, h["id"])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [RedisCache] self
|
|
||||||
def trim!
|
def trim!
|
||||||
puts "cache limit #{CACHE_LIMIT}"
|
|
||||||
puts "cache size #{self.size}"
|
|
||||||
self.redis.zremrangebyrank(set_key, 0, -(CACHE_LIMIT+1))
|
self.redis.zremrangebyrank(set_key, 0, -(CACHE_LIMIT+1))
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param order [Symbol, String]
|
# @param order [Symbol, String]
|
||||||
|
|
|
||||||
|
|
@ -40,21 +40,21 @@ describe RedisCache do
|
||||||
@timestamp = Time.now.to_i
|
@timestamp = Time.now.to_i
|
||||||
30.times do |n|
|
30.times do |n|
|
||||||
created_time = @timestamp - n*1000
|
created_time = @timestamp - n*1000
|
||||||
@redis.zadd("cache_stream_#{bob.id}_created_at", created_time, n)
|
@redis.zadd("cache_stream_#{bob.id}_created_at", created_time, n.to_s)
|
||||||
@timestamps << created_time
|
@timestamps << created_time
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the most recent post ids (default created at, limit 15)' do
|
it 'returns the most recent post ids (default created at, limit 15)' do
|
||||||
@cache.post_ids.should =~ 15.times.map {|n| n}
|
@cache.post_ids.should =~ 15.times.map {|n| n.to_s}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns posts ids after the specified time' do
|
it 'returns posts ids after the specified time' do
|
||||||
@cache.post_ids(@timestamps[15]).should =~ (15...30).map {|n| n}
|
@cache.post_ids(@timestamps[15]).should =~ (15...30).map {|n| n.to_s}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns post ids with a non-default limit' do
|
it 'returns post ids with a non-default limit' do
|
||||||
@cache.post_ids(@timestamp, 20).should =~ 20.times.map {|n| n}
|
@cache.post_ids(@timestamp, 20).should =~ 20.times.map {|n| n.to_s}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -112,7 +112,7 @@ describe RedisCache do
|
||||||
@timestamp = Time.now.to_i
|
@timestamp = Time.now.to_i
|
||||||
30.times do |n|
|
30.times do |n|
|
||||||
created_time = @timestamp - n*1000
|
created_time = @timestamp - n*1000
|
||||||
@redis.zadd("cache_stream_#{bob.id}_created_at", created_time, n)
|
@redis.zadd("cache_stream_#{bob.id}_created_at", created_time, n.to_s)
|
||||||
@timestamps << created_time
|
@timestamps << created_time
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -126,11 +126,11 @@ describe RedisCache do
|
||||||
@timestamp = Time.now.to_i
|
@timestamp = Time.now.to_i
|
||||||
120.times do |n|
|
120.times do |n|
|
||||||
created_time = @timestamp - n*1000
|
created_time = @timestamp - n*1000
|
||||||
@redis.zadd("cache_stream_#{bob.id}_created_at", created_time, n)
|
@redis.zadd("cache_stream_#{bob.id}_created_at", created_time, n.to_s)
|
||||||
@timestamps << created_time
|
@timestamps << created_time
|
||||||
end
|
end
|
||||||
|
|
||||||
post_ids = 100.times.map{|n| n}
|
post_ids = 100.times.map{|n| n.to_s}
|
||||||
@cache.trim!
|
@cache.trim!
|
||||||
@cache.post_ids(Time.now.to_i, @cache.size).should == post_ids[0...100]
|
@cache.post_ids(Time.now.to_i, @cache.size).should == post_ids[0...100]
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue