twingly-url_cache 1.0.1 → 3.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 +23 -4
- data/lib/twingly/url_cache/version.rb +1 -1
- data/lib/twingly/url_cache.rb +37 -9
- metadata +10 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1ed2d18cc27d219c6d13d00683ba7b3452e3f4e1820706d538c86f7ea102515c
|
4
|
+
data.tar.gz: de311bd4e8dddc0c8d0d28aaf48c56bd49940920d12a0c233261eee0e1b24536
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f50e165dbb8716c6fbbb3ada282e1d2e41a03579a7d9905853dcbcfe92c3c16014979e074d165f3889db0edcffac7b622e215222df6c99533fc914db4e17c90
|
7
|
+
data.tar.gz: 38c9491b219f5e951e0a8a438402674e941c7c639e6c33486cd7f8b063e2595afbce60d69e4789df4f72cf81c4571ee888d44a4e5958a05029da0e83859c05db
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Twingly::UrlCache
|
2
2
|
|
3
|
+
[](https://github.com/twingly/twingly-url_cache/actions)
|
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
|
```
|
@@ -48,9 +50,26 @@ MEMCACHIER_USERNAME
|
|
48
50
|
|
49
51
|
After checking out the repo, run `bin/setup` to install dependencies. Start memcached. Then, run `bundle exec rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
52
|
|
51
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
54
|
+
|
55
|
+
## Release workflow
|
56
|
+
|
57
|
+
* Update the examples in this README if needed.
|
58
|
+
|
59
|
+
* Bump the version in `lib/twingly/url_cache/version.rb` in a commit, no need to push (the release task does that).
|
60
|
+
|
61
|
+
* Ensure you are signed in to RubyGems.org as [twingly][twingly-rubygems] with `gem signin`.
|
62
|
+
|
63
|
+
* Build and [publish](http://guides.rubygems.org/publishing/) the gem. This will create the proper tag in git, push the commit and tag and upload to RubyGems.
|
64
|
+
|
65
|
+
bundle exec rake release
|
66
|
+
|
67
|
+
* Update the changelog with [GitHub Changelog Generator](https://github.com/skywinder/github-changelog-generator/) (`gem install github_changelog_generator` if you don't have it, set `CHANGELOG_GITHUB_TOKEN` to a personal access token to avoid rate limiting by GitHub). This command will update `CHANGELOG.md`. You need to commit and push manually.
|
68
|
+
|
69
|
+
github_changelog_generator -u twingly -p twingly-url_cache
|
70
|
+
|
71
|
+
[twingly-rubygems]: https://rubygems.org/profiles/twingly
|
52
72
|
|
53
73
|
## Contributing
|
54
74
|
|
55
75
|
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: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Twingly AB
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dalli
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.2.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 3.2.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: retryable
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,34 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: bundler
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.10'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1.10'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rake
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
45
|
- - "~>"
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
47
|
+
version: '12'
|
62
48
|
type: :development
|
63
49
|
prerelease: false
|
64
50
|
version_requirements: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
52
|
- - "~>"
|
67
53
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
54
|
+
version: '12'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: rspec
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,7 +93,7 @@ files:
|
|
107
93
|
homepage: https://github.com/twingly/twingly-url_cache
|
108
94
|
licenses: []
|
109
95
|
metadata: {}
|
110
|
-
post_install_message:
|
96
|
+
post_install_message:
|
111
97
|
rdoc_options: []
|
112
98
|
require_paths:
|
113
99
|
- lib
|
@@ -122,9 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
108
|
- !ruby/object:Gem::Version
|
123
109
|
version: '0'
|
124
110
|
requirements: []
|
125
|
-
|
126
|
-
|
127
|
-
signing_key:
|
111
|
+
rubygems_version: 3.3.7
|
112
|
+
signing_key:
|
128
113
|
specification_version: 4
|
129
114
|
summary: URL cache, remember if an URL been seen before
|
130
115
|
test_files: []
|