Skip to content

Commit b8cd84e

Browse files
committed
Add persistent HTTP cache.
Avoid exceeding Github limits and speed up fetching in development.
1 parent c72ea76 commit b8cd84e

4 files changed

Lines changed: 65 additions & 8 deletions

File tree

Gemfile.lock

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PATH
33
specs:
44
nanoc-github (1.0.1)
55
concurrent-ruby (>= 1.1.6, < 2.0)
6+
faraday-http-cache
67
nanoc (~> 4.0)
78
octokit (~> 4.0)
89

@@ -23,8 +24,10 @@ GEM
2324
ddplugin (1.0.2)
2425
diff-lcs (1.3)
2526
equatable (0.6.1)
26-
faraday (1.0.0)
27+
faraday (0.17.3)
2728
multipart-post (>= 1.2, < 3)
29+
faraday-http-cache (2.0.0)
30+
faraday (~> 0.8)
2831
hamster (3.0.0)
2932
concurrent-ruby (~> 1.0)
3033
hashdiff (1.0.1)
@@ -63,7 +66,7 @@ GEM
6366
nanoc-checking (~> 1.0)
6467
nanoc-cli (~> 4.11, >= 4.11.15)
6568
nanoc-core (~> 4.11, >= 4.11.15)
66-
octokit (4.17.0)
69+
octokit (4.18.0)
6770
faraday (>= 0.9)
6871
sawyer (~> 0.8.0, >= 0.5.3)
6972
parallel (1.19.1)
@@ -78,7 +81,7 @@ GEM
7881
addressable (>= 2.3.5)
7982
faraday (> 0.8, < 2.0)
8083
slow_enumerator_tools (1.1.0)
81-
tomlrb (1.2.9)
84+
tomlrb (1.3.0)
8285
tty-color (0.5.1)
8386
tty-command (0.9.0)
8487
pastel (~> 0.7.0)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Nanoc::Github
22

3-
Content source from git repository. A way to have your writing in public and open for editing while not being distracted
3+
Content source from git repository. A way to have your writing in public and open for editing while not being distracted
44
by static site generator trivia this content is usually mixed with.
55

66
## Usage
@@ -28,6 +28,7 @@ data_sources:
2828
access_token: secret123 # github access token, not required for public repositories (default: nil)
2929
path: posts/ # subdirectory of the content in given repository (default: nil)
3030
concurrency: 10 # how many threads to spawn to fetch data (default: 5)
31+
verbose: true # show HTTP cache hit/miss on STDOUT (default: false)
3132
```
3233
3334
## Status

lib/nanoc/github.rb

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,54 @@
11
require "nanoc"
22
require "octokit"
33
require "concurrent-ruby"
4+
require "faraday/http_cache"
5+
require "pstore"
46

57
module Nanoc
68
module Github
79
REGEX = /^(?<metadata>---\s*\n.*?\n?)^(---\s*$\n?)/m
810

11+
class Cache
12+
def initialize(cache_dir)
13+
@store = PStore.new(File.join(cache_dir, "nanoc-github.store"))
14+
end
15+
16+
def write(name, value, options = nil)
17+
store.transaction { store[name] = value }
18+
end
19+
20+
def read(name, options = nil)
21+
store.transaction(true) { store[name] }
22+
end
23+
24+
def delete(name, options = nil)
25+
store.transaction { store.delete(name) }
26+
end
27+
28+
private
29+
attr_reader :store
30+
end
31+
932
class Source < Nanoc::DataSource
1033
identifier :github
1134

35+
def up
36+
stack = Faraday::RackBuilder.new do |builder|
37+
builder.use Faraday::HttpCache,
38+
serializer: Marshal,
39+
shared_cache: false,
40+
store: Cache.new(tmp_dir),
41+
logger: verbose ? logger : nil
42+
builder.use Faraday::Request::Retry,
43+
exceptions: [Octokit::ServerError]
44+
builder.use Octokit::Middleware::FollowRedirects
45+
builder.use Octokit::Response::RaiseError
46+
builder.use Octokit::Response::FeedParser
47+
builder.adapter Faraday.default_adapter
48+
end
49+
Octokit.middleware = stack
50+
end
51+
1252
def items
1353
@items ||= begin
1454
repository_items.map do |item|
@@ -51,7 +91,7 @@ def repository_items
5191
def encoding
5292
@config[:encoding] || "utf-8"
5393
end
54-
94+
5595
def concurrency
5696
@config[:concurrency] || 5
5797
end
@@ -67,6 +107,18 @@ def path
67107
def repository
68108
@config[:repository]
69109
end
110+
111+
def verbose
112+
@config[:verbose]
113+
end
114+
115+
def logger
116+
Logger.new(STDOUT)
117+
end
118+
119+
def tmp_dir
120+
File.join(@site_config.dir, "tmp")
121+
end
70122
end
71123
end
72124
end

nanoc-github.gemspec

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Gem::Specification.new do |spec|
2424
spec.require_paths = ["lib"]
2525
spec.extra_rdoc_files = Dir["README.md", "LICENSE.txt"]
2626

27-
spec.add_dependency "nanoc", "~> 4.0"
28-
spec.add_dependency "octokit", "~> 4.0"
29-
spec.add_dependency "concurrent-ruby", ">= 1.1.6", "< 2.0"
27+
spec.add_dependency "nanoc", "~> 4.0"
28+
spec.add_dependency "octokit", "~> 4.0"
29+
spec.add_dependency "concurrent-ruby", ">= 1.1.6", "< 2.0"
30+
spec.add_dependency "faraday-http-cache"
3031
end

0 commit comments

Comments
 (0)