Module: JSONAPIonify::Api::Resource::Caching

Defined in:
lib/jsonapionify/api/resource/caching.rb

Instance Method Summary collapse

Instance Method Details

#cache(key, **options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jsonapionify/api/resource/caching.rb', line 3

def cache key, **options
  raise Errors::DoubleCacheError, "Cache was already called for this action" if @cache_called
  @cache_called = true
  @cache_options.merge! options

  # Build the cache key, and obscure it.
  @__context.meta[:cache_key] = @cache_options[:key] = cache_key(
    path:   @__context.request.path,
    accept: @__context.request.accept,
    params: @__context.params,
    key:    key
  )
  # If the cache exists, then fail to cache miss
  if self.class.cache_store.exist?(@cache_options[:key]) && !@__context.invalidate_cache?
    raise Errors::CacheHit, @cache_options[:key]
  end
end

#cache_key(**options) ⇒ Object



21
22
23
# File 'lib/jsonapionify/api/resource/caching.rb', line 21

def cache_key(**options)
  self.class.cache_key(**options, action_name: @__context.action_name)
end