'Cache the given resource.
Allow API methods to cache objects, such as results from a DB query,
to be used by API extensions within the same API request.
The resource_to_cache can be a list or an individual resource,
but ultimately resources are cached individually using the given
id_attribute.
Different resources types might need to be cached during the same
request, they can be cached using the name parameter. For example:
Controller 1:
request.cache_resource(db_volumes, \'volumes\')
request.cache_resource(db_volume_types, \'types\')
Controller 2:
db_volumes = request.cached_resource(\'volumes\')
db_type_1 = request.cached_resource_by_id(\'1\', \'types\')
If no name is given, a default name will be used for the resource.
An instance of this class only lives for the lifetime of a
single API request, so there\'s no need to implement full
cache management.'

def cache_resource(self, resource_to_cache, id_attribute='id', name=None):

    if (not isinstance(resource_to_cache, list)):
        resource_to_cache = [resource_to_cache]
    if (not name):
        name = self.path
    cached_resources = self._resource_cache.setdefault(name, {})
    for resource in resource_to_cache:
        cached_resources[resource[id_attribute]] = resource
