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 37 # regenerating prompts too frequently
38 38 @@percent_full = 0.9
39 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 43 named_scope :created_by, lambda { |id|
42 44 {:conditions => { :local_identifier => id } }
... ... @@ -504,6 +506,7 @@ class Question < ActiveRecord::Base
504 506 # 2 because redis starts indexes at 0
505 507 new_size = (@@num_prompts * @@percent_full - 2).floor
506 508 $redis.ltrim(self.pq_key, 0, new_size)
  509 + $redis.expire(self.pq_key, @@expire_prompt_cache_in_seconds)
507 510 end
508 511  
509 512 def add_prompt_to_queue
... ... @@ -518,6 +521,7 @@ class Question < ActiveRecord::Base
518 521 prompts.each do |prompt|
519 522 $redis.rpush(self.pq_key, prompt.id)
520 523 end
  524 + $redis.expire(self.pq_key, @@expire_prompt_cache_in_seconds)
521 525 return prompts
522 526 end
523 527 end
... ... @@ -526,8 +530,9 @@ class Question < ActiveRecord::Base
526 530 begin
527 531 prompt_id = $redis.lpop(self.pq_key)
528 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 536 end
532 537  
533 538 def record_prompt_cache_miss
... ...