Module: ActiveCachedResource::Caching::ClassMethods
- Defined in:
- lib/active_cached_resource/caching.rb
Instance Method Summary collapse
-
#clear_cache ⇒ void
Clears the entire cache for the specified model that matches current prefix.
-
#delete_from_cache(id) ⇒ Object
Deletes a resource from the cache.
-
#find_with_cache(scope, options = {}) ⇒ ActiveCachedResource::Collection, ...
Finds resources similarly to ActiveRecord’s
find
method, with caching support.
Instance Method Details
#clear_cache ⇒ void
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.
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, ...
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
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 = (*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][Constants::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 |