faraday-manual-cache 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/faraday-manual-cache.gemspec +3 -1
- data/lib/faraday/manual_cache.rb +29 -12
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9908ab5ca8956f133b3371bb8313fc70eefa01e9
|
4
|
+
data.tar.gz: 71e6b04c0c4bd4f512acab094c09cc4c68f60c41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c46b3edf75261d00b1c039fa77f03c247b4cbec6acc21421b6a574f308c034937072347d52d9cbbc5f7f234b96d6a5f73d41f89364dbaedef825ff4e50891b4e
|
7
|
+
data.tar.gz: 254fa5f85405e188bcc66f97275fd0dcd086f418275a79c61c95133c4757b4318342f9b073a9cc211180968c5f90d1f145f3d553def7fa4bc5a1648afe28f6c7
|
data/README.md
CHANGED
@@ -52,6 +52,10 @@ end
|
|
52
52
|
|
53
53
|
As with `faraday-http-cache` it's recommended that `faraday-manual-cache` be fairly low in the middleware stack.
|
54
54
|
|
55
|
+
## TODO
|
56
|
+
|
57
|
+
* Additional cache key options.
|
58
|
+
|
55
59
|
## Contributing
|
56
60
|
|
57
61
|
1. Fork it ( http://github.com/dobs/faraday-manual-cache/fork )
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
Gem::Specification.new do |spec|
|
3
3
|
spec.name = 'faraday-manual-cache'
|
4
|
-
spec.version = '0.0
|
4
|
+
spec.version = '0.1.0'
|
5
5
|
spec.authors = ['Daniel O\'Brien']
|
6
6
|
spec.email = ['dan@dobs.org']
|
7
7
|
spec.summary = %q(A super simple Faraday cache implementation.)
|
@@ -14,6 +14,8 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
15
15
|
spec.require_paths = ['lib']
|
16
16
|
|
17
|
+
spec.required_ruby_version = '>= 1.9'
|
18
|
+
|
17
19
|
spec.add_dependency 'activesupport', '>= 3.0.0'
|
18
20
|
spec.add_dependency 'faraday', '~> 0.9'
|
19
21
|
|
data/lib/faraday/manual_cache.rb
CHANGED
@@ -9,6 +9,7 @@ module Faraday
|
|
9
9
|
# Currently accepts four arguments:
|
10
10
|
#
|
11
11
|
# :expires_in - Cache expiry, in seconds (default: 30).
|
12
|
+
# :logger - A logger object to send cache hit/miss/write messages.
|
12
13
|
# :store - An object (or lookup symbol) for an
|
13
14
|
# ActiveSupport::Cache::Store instance. (default:
|
14
15
|
# MemoryStore).
|
@@ -19,9 +20,13 @@ module Faraday
|
|
19
20
|
super(app)
|
20
21
|
options = args.first || {}
|
21
22
|
@expires_in = options.fetch(:expires_in, 30)
|
23
|
+
@logger = options.fetch(:logger, nil)
|
24
|
+
@namespace = options.fetch(:namespace, 'faraday-manual-cache')
|
22
25
|
@store = options.fetch(:store, :memory_store)
|
23
26
|
@store_options = options.fetch(:store_options, {})
|
24
27
|
|
28
|
+
@store_options[:namespace] ||= @namespace
|
29
|
+
|
25
30
|
initialize_store
|
26
31
|
end
|
27
32
|
|
@@ -32,31 +37,42 @@ module Faraday
|
|
32
37
|
protected
|
33
38
|
|
34
39
|
def call!(env)
|
35
|
-
|
36
|
-
cached_response(env)
|
40
|
+
response_env = cached_response(env)
|
37
41
|
|
38
|
-
|
39
|
-
|
42
|
+
if response_env && !env.request_headers['x-faraday-manual-cache']
|
43
|
+
info "Cache HIT: #{key(env)}"
|
44
|
+
response_env.response_headers['x-faraday-manual-cache'] = 'HIT'
|
45
|
+
to_response(cached_response(env))
|
46
|
+
else
|
47
|
+
info "Cache MISS: #{key(env)}"
|
48
|
+
@app.call(env).on_complete do |response_env|
|
49
|
+
response_env.response_headers['x-faraday-manual-cache'] = 'MISS'
|
50
|
+
cache_response(response_env) if cacheable?(env)
|
51
|
+
end
|
40
52
|
end
|
41
53
|
end
|
42
54
|
|
43
|
-
# Cache the env to the store.
|
44
55
|
def cache_response(env)
|
45
|
-
|
56
|
+
info "Cache WRITE: #{key(env)}"
|
57
|
+
@store.write(key(env), env, expires_in: @expires_in)
|
46
58
|
end
|
47
59
|
|
48
|
-
# Whether or not the env is cacheable.
|
49
60
|
def cacheable?(env)
|
50
61
|
env.method == :get || env.method == :head
|
51
62
|
end
|
52
63
|
|
53
|
-
# Retrieve (and memoize) cached response matching current env.
|
54
64
|
def cached_response(env)
|
55
|
-
@
|
65
|
+
@store.fetch(key(env)) if cacheable?(env)
|
66
|
+
end
|
67
|
+
|
68
|
+
def info(message)
|
69
|
+
@logger.info(message) unless @logger.nil?
|
70
|
+
end
|
71
|
+
|
72
|
+
def key(env)
|
73
|
+
env.url
|
56
74
|
end
|
57
75
|
|
58
|
-
# Checks whether the specified store is a symbol, and if so attempts to
|
59
|
-
# do a lookup against ActiveSupport::Cache.
|
60
76
|
def initialize_store
|
61
77
|
return unless @store.is_a? Symbol
|
62
78
|
|
@@ -64,8 +80,9 @@ module Faraday
|
|
64
80
|
@store = ActiveSupport::Cache.lookup_store(@store, @store_options)
|
65
81
|
end
|
66
82
|
|
67
|
-
# Massage env into a Response object.
|
68
83
|
def to_response(env)
|
84
|
+
env = env.dup
|
85
|
+
env.response_headers['x-faraday-manual-cache'] = 'HIT'
|
69
86
|
response = Response.new
|
70
87
|
response.finish(env) unless env.parallel?
|
71
88
|
env.response = response
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-manual-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel O'Brien
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
94
|
requirements:
|
95
95
|
- - '>='
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: '
|
97
|
+
version: '1.9'
|
98
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - '>='
|
@@ -102,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.
|
105
|
+
rubygems_version: 2.0.3
|
106
106
|
signing_key:
|
107
107
|
specification_version: 4
|
108
108
|
summary: A super simple Faraday cache implementation.
|
109
109
|
test_files: []
|
110
|
-
has_rdoc:
|