Example #1
0
    def _get_objects_for_keys(self, model, keys):
        # First we fetch any keys that we can from the cache
        results = cache.get_many(
            [get_cache_key_for_pk(model, k) for k in keys])

        # Now we need to compute which keys weren't present in the cache
        missing = [
            k for k in keys if get_cache_key_for_pk(model, k) not in results
        ]

        # We no longer need to know what the keys were so turn it into a list
        results = results.values()
        # Query for any missing objects
        # TODO: should this only be doing the cache.set if it's from a CachedModel?
        # if not then we need to expire it, hook signals?
        objects = list(model._default_manager.filter(pk__in=missing))
        for o in objects:
            cache.set(o.cache_key, o)
        results.extend(objects)

        # Do a simple len() lookup (maybe we shouldn't rely on it returning the right
        # number of objects
        cnt = len(missing) - len(objects)
        if cnt:
            raise CacheMissingWarning("%d objects missing in the database" %
                                      (cnt, ))
        return results
Example #2
0
    def _get_objects_for_keys(self, model, keys):
        # First we fetch any keys that we can from the cache
        results = cache.get_many([get_cache_key_for_pk(model, k) for k in keys])

        # Now we need to compute which keys weren't present in the cache
        missing = [k for k in keys if get_cache_key_for_pk(model, k) not in results]

        # We no longer need to know what the keys were so turn it into a list
        results = results.values()
        # Query for any missing objects
        # TODO: should this only be doing the cache.set if it's from a CachedModel?
        # if not then we need to expire it, hook signals?
        objects = list(model._default_manager.filter(pk__in=missing))
        for o in objects:
            cache.set(o.cache_key, o)
        results.extend(objects)

        # Do a simple len() lookup (maybe we shouldn't rely on it returning the right
        # number of objects
        cnt = len(missing) - len(objects)
        if cnt:
            raise CacheMissingWarning("%d objects missing in the database" % (cnt,))
        return results
Example #3
0
 def _get_cache_key_for_pk(model, pk):
     return get_cache_key_for_pk(model, pk)
Example #4
0
 def _get_cache_key_for_pk(model, pk):
     return get_cache_key_for_pk(model, pk)