twingly-url_cache 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +4 -3
- data/lib/twingly/url_cache.rb +37 -9
- data/lib/twingly/url_cache/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 24b2b74670becb8febd7328f5282942778233895bba160e82f46d6d9058b3bb5
|
4
|
+
data.tar.gz: 7f357962eaca25d72bae9dda7f85e79f3e3980bb1a203eebfbfa681ac729217d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a191f98e4a5362bae8e1cd512021d5b0e8354dc85e8b8b9b233a25b314a8c1c294f54124403087d925b0f1bd4692a5868c1227f748d0ecde99206fa0cf2ec84
|
7
|
+
data.tar.gz: a76d8f8ee36e63685f0469647334245042c0e9ca9ac9d5ffd6113e3f1c1dc1b70bbf244167f37fe06a201ce1425357a544b3b7e59bfe3195bd452efd0e37f2bc
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Twingly::UrlCache
|
2
2
|
|
3
|
+
[](https://travis-ci.org/twingly/twingly-url_cache)
|
4
|
+
|
3
5
|
URL cache, remember if an URL been seen before. Uses [Memcached] to cache data, prefers [MemCachier].
|
4
6
|
|
5
7
|
[Memcached]: http://memcached.org/
|
@@ -36,10 +38,10 @@ Initialize an instance, cache an URL with `#cache!` and look if it's cached with
|
|
36
38
|
=> false
|
37
39
|
```
|
38
40
|
|
39
|
-
|
41
|
+
Optional environment variables:
|
40
42
|
|
41
43
|
```Shell
|
42
|
-
MEMCACHIER_SERVERS
|
44
|
+
MEMCACHIER_SERVERS # Defaults to localhost
|
43
45
|
MEMCACHIER_PASSWORD
|
44
46
|
MEMCACHIER_USERNAME
|
45
47
|
```
|
@@ -53,4 +55,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
53
55
|
## Contributing
|
54
56
|
|
55
57
|
Bug reports and pull requests are welcome on GitHub at https://github.com/twingly/twingly-url_cache.
|
56
|
-
|
data/lib/twingly/url_cache.rb
CHANGED
@@ -5,26 +5,35 @@ require "retryable"
|
|
5
5
|
|
6
6
|
module Twingly
|
7
7
|
class UrlCache
|
8
|
+
class Error < StandardError; end
|
9
|
+
class ServerError < Error; end
|
10
|
+
|
8
11
|
attr_reader :ttl
|
9
12
|
|
10
13
|
CACHE_VALUE = ""
|
11
14
|
|
12
|
-
def initialize(ttl: 0)
|
13
|
-
@
|
14
|
-
@
|
15
|
+
def initialize(ttl: 0, cache: default_cache)
|
16
|
+
@ttl = ttl
|
17
|
+
@cache = cache
|
15
18
|
end
|
16
19
|
|
17
20
|
def cache!(url)
|
18
21
|
key = key_for(url)
|
19
|
-
|
20
|
-
|
22
|
+
|
23
|
+
retry_transient_exceptions do
|
24
|
+
with_exception_class_conversion do
|
25
|
+
!!@cache.set(key, CACHE_VALUE, ttl, raw: true)
|
26
|
+
end
|
21
27
|
end
|
22
28
|
end
|
23
29
|
|
24
30
|
def cached?(url)
|
25
31
|
key = key_for(url)
|
26
|
-
|
27
|
-
|
32
|
+
|
33
|
+
retry_transient_exceptions do
|
34
|
+
with_exception_class_conversion do
|
35
|
+
@cache.get(key, raw: true) == CACHE_VALUE
|
36
|
+
end
|
28
37
|
end
|
29
38
|
end
|
30
39
|
|
@@ -34,6 +43,14 @@ module Twingly
|
|
34
43
|
Digest::MD5.digest(url)
|
35
44
|
end
|
36
45
|
|
46
|
+
def default_cache
|
47
|
+
retry_transient_exceptions do
|
48
|
+
with_exception_class_conversion do
|
49
|
+
Dalli::Client.new(servers, options)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
37
54
|
def options
|
38
55
|
{
|
39
56
|
username: ENV.fetch("MEMCACHIER_USERNAME") { },
|
@@ -45,8 +62,19 @@ module Twingly
|
|
45
62
|
end
|
46
63
|
|
47
64
|
def servers
|
48
|
-
ENV.fetch("MEMCACHIER_SERVERS").split(",")
|
65
|
+
ENV.fetch("MEMCACHIER_SERVERS") { "localhost" }.split(",")
|
66
|
+
end
|
67
|
+
|
68
|
+
def with_exception_class_conversion
|
69
|
+
yield
|
70
|
+
rescue Dalli::RingError => error
|
71
|
+
raise ServerError, error.message
|
72
|
+
rescue Dalli::DalliError => error
|
73
|
+
raise Error, error.message
|
74
|
+
end
|
75
|
+
|
76
|
+
def retry_transient_exceptions(&block)
|
77
|
+
Retryable.retryable(tries: 3, on: ServerError, &block)
|
49
78
|
end
|
50
79
|
end
|
51
80
|
end
|
52
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twingly-url_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Twingly AB
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dalli
|
@@ -122,8 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
|
-
|
126
|
-
rubygems_version: 2.4.5.1
|
125
|
+
rubygems_version: 3.0.2
|
127
126
|
signing_key:
|
128
127
|
specification_version: 4
|
129
128
|
summary: URL cache, remember if an URL been seen before
|