Class Kotoba_Rss::Cache
In: lib/kotoba_rss/cache.rb
Parent: Object

Keep a cache of fetched RSS feeds. This is to ensure we do not hit an RSS feed too often; an issue when we have many page fetches.

This is also a very useful thing when we are fetching from sites that might be throttling requests from specific users and, or domains (e.g. twitter.com).

Methods

add   get   has_expired?   new  

Included Modules

Singleton

Classes and Modules

Class Kotoba_Rss::Cache::Entry

Constants

ENTRY_EXPIRES_IN_SECONDS = 300

Public Class methods

[Source]

    # File lib/kotoba_rss/cache.rb, line 13
13:   def initialize
14:     @cache = Hash.new
15:   end

Public Instance methods

[Source]

    # File lib/kotoba_rss/cache.rb, line 17
17:   def add(url, result)
18:     entity = Kotoba_Rss::Cache::Entry.new(Time.now,result)
19:     @cache[url] = entity
20:   end

[Source]

    # File lib/kotoba_rss/cache.rb, line 22
22:   def get(url)
23:     @cache[url].feed_result
24:   end

[Source]

    # File lib/kotoba_rss/cache.rb, line 26
26:   def has_expired?(url)
27:     if @cache.has_key?(url)
28:       entry        = @cache[url]
29:       current_time = Time.now
30:       time_expired = current_time - entry.feed_time
31:       if time_expired > ENTRY_EXPIRES_IN_SECONDS
32:         return true
33:       else
34:         return false
35:       end
36:     else
37:       # if we do not have the URL yet then 
38:       # consider it expired
39:       return true
40:     end
41:   end

[Validate]