Class: Parse::Middleware::Caching

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
Protocol
Defined in:
lib/parse/client/caching.rb

Overview

This is a caching middleware for Parse queries using Moneta. The caching middleware will cache all GET requests made to the Parse REST API as long as the API responds with a successful non-empty result payload.

Whenever an object is created or updated, the corresponding entry in the cache when fetching the particular record (using the specific non-Query based API) will be cleared.

Constant Summary collapse

CACHEABLE_HTTP_CODES =

List of status codes that can be cached:

  • 200 - 'OK'

  • 203 - 'Non-Authoritative Information'

  • 300 - 'Multiple Choices'

  • 301 - 'Moved Permanently'

  • 302 - 'Found'

  • 404 - 'Not Found' - removed

  • 410 - 'Gone' - removed

[200, 203, 300, 301, 302].freeze
CACHE_CONTROL =

Cache control header

'Cache-Control'
CONTENT_LENGTH_KEY =

Request env key for the content length

'content-length'
CACHE_RESPONSE_HEADER =

Header in response that is sent if this is a cached result

'X-Cache-Response'
CACHE_EXPIRES_DURATION =

Header in request to set caching information for the middleware.

'X-Parse-Stack-Cache-Expires'

Constants included from Protocol

Protocol::API_KEY, Protocol::APP_ID, Protocol::CONTENT_TYPE, Protocol::CONTENT_TYPE_FORMAT, Protocol::EMAIL, Protocol::INSTALLATION_ID, Protocol::MASTER_KEY, Protocol::PASSWORD, Protocol::REVOCABLE_SESSION, Protocol::SERVER_URL, Protocol::SESSION_TOKEN

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, store, opts = {}) ⇒ Caching

Creates a new caching middleware.

Parameters:

  • adapter (Faraday::Adapter)

    An instance of the Faraday adapter used for the connection. Defaults Faraday::Adapter::NetHttp.

  • store (Moneta)

    An instance of the Moneta cache store to use.

  • opts (Hash) (defaults to: {})

    additional options.

Options Hash (opts):

  • :expires (Integer)

    the default expiration for a cache entry.

Raises:

  • ArgumentError, if `store` is not a Moneta::Transformer or Moneta::Expires instance.


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/parse/client/caching.rb', line 77

def initialize(adapter, store, opts = {})
  super(adapter)
  @store = store
  @opts = {expires: 0}
  @opts.merge!(opts) if opts.is_a?(Hash)
  @expires = @opts[:expires]

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

end

Class Attribute Details

.enabledBoolean

Returns whether the caching middleware should be enabled.

Returns:

  • (Boolean)

    whether the caching middleware should be enabled.


42
43
44
# File 'lib/parse/client/caching.rb', line 42

def enabled
  @enabled
end

.loggingBoolean

Returns whether the logging should be enabled.

Returns:

  • (Boolean)

    whether the logging should be enabled.


46
47
48
# File 'lib/parse/client/caching.rb', line 46

def logging
  @logging
end

Instance Attribute Details

#expiresInteger

The expiration time in seconds for this particular request.

Returns:

  • (Integer)

68
69
70
# File 'lib/parse/client/caching.rb', line 68

def expires
  @expires
end

#storeMoneta::Transformer, Moneta::Expires

The internal moneta cache store instance.

Returns:

  • (Moneta::Transformer, Moneta::Expires)

63
64
65
# File 'lib/parse/client/caching.rb', line 63

def store
  @store
end

Class Method Details

.caching?Boolean

Returns whether caching is enabled.

Returns:

  • (Boolean)

    whether caching is enabled.


54
55
56
# File 'lib/parse/client/caching.rb', line 54

def caching?
  @enabled
end