Skip to content

Commit

Permalink
Merge pull request #39 from GrahamW/allow_moneta_expires
Browse files Browse the repository at this point in the history
Allow Moneta::Expires as cache object to allow for non-native expiring caches
  • Loading branch information
apersaud committed Aug 13, 2017
2 parents a17c0f8 + c9daab9 commit 8b586fe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ single requests in a multi-request threaded environment. (See Parse::Webhooks::P
- FIXED: Properties of :array type will always return a Parse::CollectionProxy if their internal value is nil. The object will not be marked dirty until something is added to the array.
- FIXED: Encoding a Parse::Object into JSON will remove any values that are `nil`
which were not explicitly changed to that value.
- [PR#39](https://github.com/modernistik/parse-stack/pull/39): Allow Moneta::Expires as cache object to allow for non-native expiring caches by [GrahamW](https://github.com/GrahamW)

### 1.7.1
- NEW: `:timezone` datatype that maps to `Parse::TimeZone` (which mimics `ActiveSupport::TimeZone`)
Expand Down
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 [Moneta::Transformer,Moneta::Expires]
# @!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 [Moneta::Transformer,Moneta::Expires] :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 [Moneta::Transformer,Moneta::Expires]
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

0 comments on commit 8b586fe

Please sign in to comment.