Class: Twingly::UrlCache

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

Constant Summary collapse

CACHE_VALUE =
""
VERSION =
"1.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl: 0) ⇒ UrlCache

Returns a new instance of UrlCache.



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

def initialize(ttl: 0)
  @cache = Dalli::Client.new(servers, options)
  @ttl = ttl
end

Instance Attribute Details

#ttlObject (readonly)

Returns the value of attribute ttl.



8
9
10
# File 'lib/twingly/url_cache.rb', line 8

def ttl
  @ttl
end

Instance Method Details

#cache!(url) ⇒ Object



17
18
19
20
21
22
# File 'lib/twingly/url_cache.rb', line 17

def cache!(url)
  key = key_for(url)
  Retryable.retryable(tries: 3, on: Dalli::RingError) do
    !!@cache.set(key, CACHE_VALUE, ttl, raw: true)
  end
end

#cached?(url) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/twingly/url_cache.rb', line 24

def cached?(url)
  key = key_for(url)
  Retryable.retryable(tries: 3, on: Dalli::RingError) do
    @cache.get(key, raw: true) == CACHE_VALUE
  end
end