faraday-manual-cache 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72ed2d3a4eb4bacc4e2f682d7d2f0621aecfc2ae
4
- data.tar.gz: 1ff4fcf690ae465b312fa324152d40bd4bc1910a
3
+ metadata.gz: 9908ab5ca8956f133b3371bb8313fc70eefa01e9
4
+ data.tar.gz: 71e6b04c0c4bd4f512acab094c09cc4c68f60c41
5
5
  SHA512:
6
- metadata.gz: 781465c3431f1167f6dea2df07b617b1a4b8de965dbfd890e6a018ea50792a2b84d7a630760d67bafa611d82c061fc68bc6b6393ef4849225670f42719469d24
7
- data.tar.gz: 5dd8050ea269687ce3ccf5137d42ae20001382f11763ddf5cb4088deeb43be7575e4e6513d9e5a7c2f9d89e43c8f10fb744ccb8332aa8c37af7d98e064fcbce0
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.1'
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
 
@@ -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
- return to_response(cached_response(env)) if cacheable?(env) &&
36
- cached_response(env)
40
+ response_env = cached_response(env)
37
41
 
38
- @app.call(env).on_complete do |response_env|
39
- cache_response(response_env) if cacheable?(env)
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
- @store.write(env.url, env, expires_in: @expires_in)
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
- @cached_response ||= @store.fetch(env.url)
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.1
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-22 00:00:00.000000000 Z
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: '0'
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.2.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: