Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
dj-kaos-utils / drf / cache.py
Size: Mime:
import datetime

from django.core.cache import cache
from django.utils.encoding import force_text
from rest_framework_extensions.key_constructor.bits import (
    KeyBitBase,
    ListSqlQueryKeyBit,
    PaginationKeyBit)
from rest_framework_extensions.key_constructor.constructors import (
    DefaultKeyConstructor
)


class UpdatedAtKeyBit(KeyBitBase):
    def __init__(self, key, *args, **kwargs):
        assert key
        super().__init__(*args, **kwargs)
        self.key = key

    def get_data(self, **kwargs):
        value = cache.get(self.key, None)
        if not value:
            value = datetime.datetime.utcnow()
            cache.set(self.key, value=value)
        return force_text(value)


def list_cache_update_invalidator_factory(key):
    class UpdateInvalidatorListKeyConstructor(DefaultKeyConstructor):
        list_sql = ListSqlQueryKeyBit()
        pagination = PaginationKeyBit()
        updated_at = UpdatedAtKeyBit(key)

    return UpdateInvalidatorListKeyConstructor