Class: Canonicurl::Cache
- Inherits:
-
Object
- Object
- Canonicurl::Cache
- Defined in:
- lib/canonicurl/cache.rb
Constant Summary collapse
- CANONICAL =
'C'
- ERROR =
'E'
- RESOLVING =
'R'
- TTL =
90 days ~ 3 months
60 * 60 * 24 * 90
- REDIRECTS =
5
- CONNECTION_TIMEOUT =
5
- KEY_PREFIX =
'curl:'
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
-
#key_prefix ⇒ Object
readonly
Returns the value of attribute key_prefix.
-
#redirects ⇒ Object
Returns the value of attribute redirects.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#ttl ⇒ Object
Returns the value of attribute ttl.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(url, resolver) ⇒ Object
- #get(url) ⇒ Object
-
#initialize(options = {}) ⇒ Cache
constructor
A new instance of Cache.
- #key(url) ⇒ Object
- #set(url, canonical_url, url_key = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Cache
Returns a new instance of Cache.
23 24 25 26 27 28 29 |
# File 'lib/canonicurl/cache.rb', line 23 def initialize(={}) @db = [:db] || Redis.connect @ttl = [:ttl] || TTL @timeout = [:timeout] || CONNECTION_TIMEOUT @redirects = [:redirects] || REDIRECTS @key_prefix = [:key_prefix] || KEY_PREFIX end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
15 16 17 |
# File 'lib/canonicurl/cache.rb', line 15 def db @db end |
#key_prefix ⇒ Object (readonly)
Returns the value of attribute key_prefix.
16 17 18 |
# File 'lib/canonicurl/cache.rb', line 16 def key_prefix @key_prefix end |
#redirects ⇒ Object
Returns the value of attribute redirects.
15 16 17 |
# File 'lib/canonicurl/cache.rb', line 15 def redirects @redirects end |
#timeout ⇒ Object
Returns the value of attribute timeout.
15 16 17 |
# File 'lib/canonicurl/cache.rb', line 15 def timeout @timeout end |
#ttl ⇒ Object
Returns the value of attribute ttl.
15 16 17 |
# File 'lib/canonicurl/cache.rb', line 15 def ttl @ttl end |
Class Method Details
.url(code_or_url) ⇒ Object
18 19 20 |
# File 'lib/canonicurl/cache.rb', line 18 def self.url(code_or_url) code_or_url && code_or_url.size > 1 ? code_or_url : nil end |
Instance Method Details
#fetch(url, resolver) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/canonicurl/cache.rb', line 37 def fetch(url, resolver) k = key(url) # New URL resolve it if @db.setnx(k, RESOLVING) # lock it if key doesn't exist return resolve(url, k, resolver) end result = @db.get(k) if !result.nil? && result.size > 1 return result # Found old noncanonical URL mapping end # CANONICAL, ERROR or RESOLVING result == CANONICAL ? url : result end |
#get(url) ⇒ Object
32 33 34 |
# File 'lib/canonicurl/cache.rb', line 32 def get(url) @db.get key(url) end |
#key(url) ⇒ Object
66 67 68 |
# File 'lib/canonicurl/cache.rb', line 66 def key(url) "#{@key_prefix}#{Digest::MD5.hexdigest(url)}" end |
#set(url, canonical_url, url_key = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/canonicurl/cache.rb', line 55 def set(url, canonical_url, url_key=nil) url_key = url_key || key(url) if url == canonical_url @db.setex(url_key, @ttl, CANONICAL) else @db.setex(url_key, @ttl, canonical_url) @db.setex(key(canonical_url), @ttl, CANONICAL) # preemptively set the canonical_url end end |