0

我使用 rest_framework_extensions 缓存我的 rest-api,例如:

class UpdatedAtKeyBit(KeyBitBase):
    def get_data(self, **kwargs):
        basename = kwargs['view_instance'].basename.lower()
        key = f'api_updated_at_timestamp_{basename}'
        value = cache.get(key, None)
        if not value:
            value = datetime.datetime.utcnow()
            cache.set(key, value=value)
        return force_text(value)



class CustomListKeyConstructor(DefaultKeyConstructor):
    list_sql = ListSqlQueryKeyBit()
    pagination = PaginationKeyBit()
    updated_at = UpdatedAtKeyBit()


def change_api_updated_at(sender=None, instance=None, *args, **kwargs):
    cache.set(f'api_updated_at_timestamp_{sender.__name__.lower()}', datetime.datetime.utcnow())


for model in [Address, Order, Permission, User]:
    post_save.connect(receiver=change_api_updated_at, sender=model)
    post_delete.connect(receiver=change_api_updated_at, sender=model)

这很完美!我缓存来自模型的 GET 列表响应。现在我想在更新或保存后自动刷新这些视图。这样下面的请求就已经在缓存中了。我该如何解决这个问题?

4

0 回答 0