def cache_results(self, results): """ Create invalidation signals for these results in the form of CacheBotSignals. A CacheBotSignal stores a model and it's accessor path to self.queryset.model. """ # cache the results invalidation_dict = {} if cache.add(self.result_key, results, conf.CACHE_SECONDS): invalidation_dict.update(dict([(key, self.result_key) for key in self.get_invalidation_keys(results)])) for child, negate in self.queryset._get_where_clause(self.queryset.query.where): constraint, lookup_type, value_annotation, params = child for model_class, accessor_path in self._get_join_paths(constraint.alias, constraint.col): if self._is_valid_flush_path(accessor_path): invalidation_key = self._register_signal(model_class, accessor_path, lookup_type, negate, params) invalidation_dict[invalidation_key] = self.result_key for join_tuple in self.queryset.query.join_map.keys(): if join_tuple[0] == model_class._meta.db_table and self._is_valid_flush_path(accessor_path): model_klass, m2m = self.queryset._get_model_class_from_table(join_tuple[1]) invalidation_key = self._register_signal(model_klass, join_tuple[3], lookup_type, negate, params) invalidation_dict[invalidation_key] = self.result_key # need to add and append to prevent race conditions # replace this with batch operations later for flush_key, flush_list in invalidation_dict.iteritems(): added = cache.add(flush_key, self.result_key, 0) if not added: cache.append(flush_key, ',%s' % self.result_key)
def cache_results(self, results): """ Create invalidation signals for these results in the form of CacheBotSignals. A CacheBotSignal stores a model and it's accessor path to self.queryset.model. """ # cache the results invalidation_dict = {} if results: added_to_cache = cache.add(self.result_key, results, CACHE_SECONDS) else: added_to_cache = cache.add(self.result_key, None, CACHE_SECONDS) if added_to_cache: invalidation_dict.update(dict([(key,None) for key in self.get_invalidation_keys(results)])) invalidation_dict.update(cache.get_many(invalidation_dict.keys())) for child, negate in self.queryset._get_where_clause(self.queryset.query.where): (table_alias, field_name, db_type), lookup_type, value_annotation, params = child for model_class, accessor_path in self._get_join_paths(table_alias, field_name): if model_class is None: continue if self._is_valid_flush_path(accessor_path): cache_signals.register(model_class, accessor_path, lookup_type, negate=negate) invalidation_key = get_invalidation_key( model_class._meta.db_table, accessor_path = accessor_path, lookup_type = lookup_type, negate = negate, value = params, save=True) invalidation_dict[invalidation_key] = None join_to_tables = ifilter(lambda x: x[0] == model_class._meta.db_table, self.queryset.query.join_map.keys()) for join_tuple in join_to_tables: if self._is_valid_flush_path(accessor_path): model_class = self.queryset._get_model_class_from_table(join_tuple[1]) cache_signals.register(model_class, join_tuple[3], lookup_type, negate=negate) invalidation_key = get_invalidation_key( model_class._meta.db_table, accessor_path = join_tuple[3], lookup_type = lookup_type, negate = negate, value = params, save=True) invalidation_dict[invalidation_key] = None for flush_key, flush_list in invalidation_dict.iteritems(): # need to add and append to prevent race conditions cache.add(flush_key, self.result_key, CACHE_SECONDS) if flush_list is None or flush_key not in flush_list.split(','): cache.append(flush_key, ',%s' % self.result_key)