Module: ActiveCachedResource::Caching::ClassMethods

Defined in:
lib/active_cached_resource/caching.rb

Instance Method Summary collapse

Instance Method Details

#clear_cachevoid

This method returns an undefined value.

Clears the entire cache for the specified model that matches current prefix.



151
152
153
154
# File 'lib/active_cached_resource/caching.rb', line 151

def clear_cache
  cached_resource.logger.debug("Clearing cache for #{name} cache with prefix: #{cache_key_prefix}")
  cached_resource.cache.clear(cache_key_prefix)
end

#delete_from_cache(id) ⇒ Object

Deletes a resource from the cache.

Parameters:

  • id (Object)

    the identifier of the resource to be deleted from the cache.



144
145
146
# File 'lib/active_cached_resource/caching.rb', line 144

def delete_from_cache(id)
  cached_resource.cache.delete(cache_key(id))
end

#find_with_cache(scope, options = {}) ⇒ ActiveCachedResource::Collection, ...

Note:

If the ‘:reload` option is passed (e.g. `:reload => true`), the cache will be bypassed, and the resource(s) will be fetched directly from the server.

Finds resources similarly to ActiveRecord’s find method, with caching support.

This method is also called internally by the ‘where` method. When you use `where` to filter results, it translates the query conditions into parameters and delegates to this method.

Depending on the first argument provided, this method retrieves:

  • ‘:one` - A single resource.

  • ‘:first` - The first resource in the result set.

  • ‘:last` - The last resource in the result set.

  • ‘:all` - An array of all matching resources.

If an Integer or String ID is provided instead of a symbol, it attempts to find a single resource by that ID.

When ‘where` is used, it automatically builds the query parameters and calls `find_with_cache(:all, …)`:

Failure or missing data

A failure to find the requested object by ID raises a ResourceNotFound exception. With any other scope, find returns nil when no data is returned.

Person.find(1)
# => raises ResourceNotFound

Person.find(:all)
Person.find(:first)
Person.find(:last)
# => nil

Examples:

Find a single resource by ID

Person.find(1)
# GET /people/1.json

Find all resources

Person.find(:all)
# GET /people.json

Find all resources with query parameters

Person.find(:all, params: { title: "CEO" })
# GET /people.json?title=CEO

Find the first resource from a custom endpoint

Person.find(:first, from: :managers)
# GET /people/managers.json

Find the last resource from a custom endpoint

Person.find(:last, from: :managers)
# GET /people/managers.json

Find all resources from a nested URL

Person.find(:all, from: "/companies/1/people.json")
# GET /companies/1/people.json

Find a single resource from a custom endpoint

Person.find(:one, from: :leader)
# GET /people/leader.json

Find all developers speaking Ruby

Person.find(:all, from: :developers, params: { language: 'ruby' })
# GET /people/developers.json?language=ruby

Find a single resource from a nested URL

Person.find(:one, from: "/companies/1/manager.json")
# GET /companies/1/manager.json

Find a resource with nested prefix parameters

StreetAddress.find(1, params: { person_id: 1 })
# GET /people/1/street_addresses/1.json

Using ‘where` with parameters

Person.where(title: "CEO")
# Under the hood: Person.find_with_cache(:all, params: { title: "CEO" })
# => GET /people.json?title=CEO

Person.where(language: 'ruby').where(from: :developers)
# Under the hood: Person.find_with_cache(:all, from: :developers, params: { language: 'ruby' })
# => GET /people/developers.json?language=ruby

Parameters:

  • scope (Symbol, Integer, String)

    The scope of the query or the ID of the resource to find. Can be ‘:one`, `:first`, `:last`, `:all`, or a specific ID.

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

    Additional query options.

Options Hash (options):

  • :from (String, Symbol)

    The path or custom endpoint from which to fetch resources.

  • :params (Hash)

    Query and prefix (nested URL) parameters.

Returns:

  • (ActiveCachedResource::Collection, ActiveCachedResource::Resource, nil)

    Returns the requested resource(s) based on the scope:

    • Returns a single resource object if ‘:one`, `:first`, `:last`, or an ID is given.

    • Returns an ActiveCachedResource::Collection containing the resources if ‘:all` is given.

    • Returns ‘nil` if no data is found for `:one`, `:first`, `:last`, or `:all` queries.

Raises:

  • (ResourceNotFound)

    Raises if the requested resource by ID cannot be found.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/active_cached_resource/caching.rb', line 119

def find_with_cache(*orig_args)
  args = orig_args.deep_dup # Avoid mutating original arguments
  options = extract_options(*args)

  should_reload = options.delete(:reload) || !cached_resource.enabled

  # When bypassing cache, include the reload option as a query parameter for collection requests.
  # Hacky but this way ActiveCachedResource::Collection#request_resources! can access it
  if should_reload && args.first == :all
    options[:params] = {} if options[:params].blank?
    options[:params][Constants::RELOAD_PARAM] = should_reload
    args << options
  end

  if args.first == :all
    # Let ActiveCachedResource::Collection handle the caching so that lazy loading is more effective
    return find_via_reload(*args)
  end

  should_reload ? find_via_reload(*args) : find_via_cache(*args)
end