Module: ActiveCachedResource::Caching::ClassMethods
- Defined in:
- lib/active_cached_resource/caching.rb
Instance Method Summary collapse
-
#clear_cache(pattern = nil) ⇒ void
Clears the cache for the specified pattern.
-
#find_with_cache(scope, options = {}) ⇒ Object, ...
Finds resources similarly to ActiveRecord’s
findmethod, with caching support.
Instance Method Details
#clear_cache(pattern = nil) ⇒ void
This method returns an undefined value.
Clears the cache for the specified pattern.
148 149 150 |
# File 'lib/active_cached_resource/caching.rb', line 148 def clear_cache(pattern = nil) cached_resource.cache.clear("#{cache_key_prefix}/#{pattern}") end |
#find_with_cache(scope, options = {}) ⇒ Object, ...
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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/active_cached_resource/caching.rb', line 121 def find_with_cache(*orig_args) args = orig_args.deep_dup # Avoid mutating original arguments = (*args) should_reload = .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 [:params] = {} if [:params].blank? [:params][RELOAD_PARAM] = should_reload args << 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 |