Commit aa9abb36074cc8da496288608c6fd97de738d55b

Authored by Luke Baker
1 parent cf825616

update prompt_queue to expire every after 30 days

on every read or write we update the expiration to 30 days from that point, so it'll only expire after 30 days of inactivity
Showing 1 changed file with 7 additions and 2 deletions   Show diff stats
app/models/question.rb
@@ -37,6 +37,8 @@ class Question < ActiveRecord::Base @@ -37,6 +37,8 @@ class Question < ActiveRecord::Base
37 # regenerating prompts too frequently 37 # regenerating prompts too frequently
38 @@percent_full = 0.9 38 @@percent_full = 0.9
39 @@num_prompts = 1000 39 @@num_prompts = 1000
  40 + # expire prompts after 30 days of no use
  41 + @@expire_prompt_cache_in_seconds = 60 * 60 * 24 * 30
40 42
41 named_scope :created_by, lambda { |id| 43 named_scope :created_by, lambda { |id|
42 {:conditions => { :local_identifier => id } } 44 {:conditions => { :local_identifier => id } }
@@ -504,6 +506,7 @@ class Question < ActiveRecord::Base @@ -504,6 +506,7 @@ class Question < ActiveRecord::Base
504 # 2 because redis starts indexes at 0 506 # 2 because redis starts indexes at 0
505 new_size = (@@num_prompts * @@percent_full - 2).floor 507 new_size = (@@num_prompts * @@percent_full - 2).floor
506 $redis.ltrim(self.pq_key, 0, new_size) 508 $redis.ltrim(self.pq_key, 0, new_size)
  509 + $redis.expire(self.pq_key, @@expire_prompt_cache_in_seconds)
507 end 510 end
508 511
509 def add_prompt_to_queue 512 def add_prompt_to_queue
@@ -518,6 +521,7 @@ class Question < ActiveRecord::Base @@ -518,6 +521,7 @@ class Question < ActiveRecord::Base
518 prompts.each do |prompt| 521 prompts.each do |prompt|
519 $redis.rpush(self.pq_key, prompt.id) 522 $redis.rpush(self.pq_key, prompt.id)
520 end 523 end
  524 + $redis.expire(self.pq_key, @@expire_prompt_cache_in_seconds)
521 return prompts 525 return prompts
522 end 526 end
523 end 527 end
@@ -526,8 +530,9 @@ class Question < ActiveRecord::Base @@ -526,8 +530,9 @@ class Question < ActiveRecord::Base
526 begin 530 begin
527 prompt_id = $redis.lpop(self.pq_key) 531 prompt_id = $redis.lpop(self.pq_key)
528 prompt = prompt_id.nil? ? nil : Prompt.find(prompt_id.to_i) 532 prompt = prompt_id.nil? ? nil : Prompt.find(prompt_id.to_i)
529 - end until (prompt.nil? || prompt.active?)  
530 - prompt 533 + end until (prompt.nil? || prompt.active?)
  534 + $redis.expire(self.pq_key, @@expire_prompt_cache_in_seconds)
  535 + prompt
531 end 536 end
532 537
533 def record_prompt_cache_miss 538 def record_prompt_cache_miss