Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Moneta::Expires as cache object to allow for non-native expiring caches #39

Merged
merged 3 commits into from
Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/parse/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ResponseError < Parse::Error; end;

# @!attribute cache
# The underlying cache store for caching API requests.
# @return [Moneta::Transformer]
# @return [Object]
# @!attribute [r] application_id
# The Parse application identifier to be sent in every API request.
# @return [String]
Expand Down Expand Up @@ -206,8 +206,9 @@ def setup(opts = {})
# the logging performed by {Parse::Middleware::BodyBuilder}.
# @option opts [Object] :adapter The connection adapter. By default it uses
# the `Faraday.default_adapter` which is Net/HTTP.
# @option opts [Moneta::Transformer] :cache A caching adapter of type
# {https://github.com/minad/moneta Moneta::Transformer} that will be used
# @option opts [Object] :cache A caching adapter of type
# {https://github.com/minad/moneta Moneta::Transformer} or
# {https://github.com/minad/moneta Moneta::Expires} that will be used
# by the caching middleware {Parse::Middleware::Caching}.
# Caching queries and object fetches can help improve the performance of
# your application, even if it is for a few seconds. Only successful GET
Expand All @@ -223,7 +224,7 @@ def setup(opts = {})
# @option opts [Hash] :faraday You may pass a hash of options that will be
# passed to the Faraday constructor.
# @raise Parse::Error::ConnectionError if the client was not properly configured with required keys or url.
# @raise ArgumentError if the cache instance passed to the :cache option is not of Moneta::Transformer.
# @raise ArgumentError if the cache instance passed to the :cache option is not of Moneta::Transformer or Moneta::Expires
# @see Parse::Middleware::BodyBuilder
# @see Parse::Middleware::Caching
# @see Parse::Middleware::Authentication
Expand Down Expand Up @@ -277,8 +278,8 @@ def initialize(opts = {})
end
end

unless opts[:cache].is_a?(Moneta::Transformer)
raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta::Transformer store."
unless [:key?, :[], :delete, :store].all? { |method| opts[:cache].respond_to?(method) }
raise ArgumentError, "Parse::Client option :cache needs to be a type of Moneta store"
end
self.cache = opts[:cache]
conn.use Parse::Middleware::Caching, self.cache, {expires: opts[:expires].to_i }
Expand Down
8 changes: 4 additions & 4 deletions lib/parse/client/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def caching?

# @!attribute [rw] store
# The internal moneta cache store instance.
# @return [Moneta::Transformer]
# @return [Object]
attr_accessor :store

# @!attribute [rw] expires
Expand All @@ -73,16 +73,16 @@ def caching?
# @param store [Moneta] An instance of the Moneta cache store to use.
# @param opts [Hash] additional options.
# @option opts [Integer] :expires the default expiration for a cache entry.
# @raise ArgumentError, if `store` is not a Moneta::Transformer instance.
# @raise ArgumentError, if `store` is not a Moneta::Transformer or Moneta::Expires instance.
def initialize(adapter, store, opts = {})
super(adapter)
@store = store
@opts = {expires: 0}
@opts.merge!(opts) if opts.is_a?(Hash)
@expires = @opts[:expires]

unless @store.is_a?(Moneta::Transformer)
raise ArgumentError, "Caching store object must a Moneta key/value store (Moneta::Transformer)."
unless [:key?, :[], :delete, :store].all? { |method| @store.respond_to?(method) }
raise ArgumentError, "Caching store object must a Moneta key/value store."
end

end
Expand Down
32 changes: 32 additions & 0 deletions test/lib/parse/cache_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative '../../test_helper'

class TestCache < Minitest::Test
def setup
@init_object = {
server_url: 'http://b.com/parse',
app_id: 'abc',
api_key: 'def'
}
end

def test_no_cache_ok
assert Parse.setup(@init_object)
end

def test_moneta_transformer_accepted
init = @init_object.merge(cache: Moneta.new(:LRUHash))
assert init[:cache].is_a?(Moneta::Transformer)
assert Parse.setup(init)
end

def test_moneta_expire_accepted
init = @init_object.merge(cache: Moneta.new(:LRUHash, expires: 13))
assert init[:cache].is_a?(Moneta::Expires)
assert Parse.setup(init)
end

def test_bad_cache_type_rejected
init = @init_object.merge(cache: 'hamster')
assert_raises(ArgumentError) { Parse.setup(init) }
end
end