Class: Twingly::UrlCache

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly/url_cache.rb,
lib/twingly/url_cache/version.rb

Defined Under Namespace

Classes: Error, ServerError

Constant Summary collapse

CACHE_VALUE =
""
VERSION =
"2.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl: 0, cache: default_cache) ⇒ UrlCache

Returns a new instance of UrlCache.



15
16
17
18
# File 'lib/twingly/url_cache.rb', line 15

def initialize(ttl: 0, cache: default_cache)
  @ttl   = ttl
  @cache = cache
end

Instance Attribute Details

#ttlObject (readonly)

Returns the value of attribute ttl.



11
12
13
# File 'lib/twingly/url_cache.rb', line 11

def ttl
  @ttl
end

Instance Method Details

#cache!(url) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/twingly/url_cache.rb', line 20

def cache!(url)
  key = key_for(url)

  retry_transient_exceptions do
    with_exception_class_conversion do
      !!@cache.set(key, CACHE_VALUE, ttl, raw: true)
    end
  end
end

#cached?(url) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/twingly/url_cache.rb', line 30

def cached?(url)
  key = key_for(url)

  retry_transient_exceptions do
    with_exception_class_conversion do
      @cache.get(key, raw: true) == CACHE_VALUE
    end
  end
end