Giter VIP home page Giter VIP logo

Comments (6)

chibisov avatar chibisov commented on August 12, 2024

Hi.

I am wondering why the KeyConstructor bits are class variables not instance?

Why? Instances of what? As I see they are instances. For example:

class DefaultKeyConstructor(KeyConstructor):
    unique_method_id = bits.UniqueMethodIdKeyBit()
    format = bits.FormatKeyBit()
    language = bits.LanguageKeyBit()

unique_method_id - is instance of bits.UniqueMethodIdKeyBit.

I think it's a good idea to allow to send custom params attribute in KeyConstructor initialization method, but it brings some complexity. For example, if you send params={'request_meta': ['GEOIP_COUNTRY']}, what about other key bits? Should they use default params, or fail without their custom params?

You can rewrite your example code with simple class inheritance:

class CityView(views.APIView):
    @cache_response(key_func=CityGetKeyConstructor())
    def get(self, request, *args, **kwargs):
        ...

class CountryGetKeyConstructor(CityView):
    request_meta = RequestMetaKeyBit(['GEOIP_COUNTRY'])

class CountryView(views.APIView):
    @cache_response(key_func=CountryGetKeyConstructor())
    def get(self, request, *args, **kwargs):
        ...

But anyway, i think it's a good idea that needs more planning. I would love to review pull request or see more of your use cases.

from drf-extensions.

georgewhewell avatar georgewhewell commented on August 12, 2024

Yes, but each instance of DefaultKeyConstructor shares the same instances of the KeyBits. If you use the same KeyConstructor in two different places, you cant change the parameters of it's KeyBits.

My actual use case is a little different (similar to the CustomListKeyConstructor in your docs).

class CustomListKeyConstructor(DefaultKeyConstructor):
    def __init__(self, models):
        self.model_bit = UpdatedAtKeyBit(models)
        super(CustomListKeyConstructor, self).__init__()

Where UpdatedAtKeyBit checks a updated_at key. Currently this will not work because DefaultKeyConstructor only looks in dir(self.__class__), and I wondered if this was by design

Thanks

from drf-extensions.

chibisov avatar chibisov commented on August 12, 2024

You can try to alter self.bits in __init__ method:

class CustomListKeyConstructor(DefaultKeyConstructor):
    def __init__(self, models):
        super(CustomListKeyConstructor, self).__init__()
        self.bits['model_bit'] = UpdatedAtKeyBit(models)

from drf-extensions.

georgewhewell avatar georgewhewell commented on August 12, 2024

Great, thanks. Do you think caching based on model last-modified is a common enough use case to provide a KeyConstuctor?

from drf-extensions.

chibisov avatar chibisov commented on August 12, 2024

Absolutely. I'm going to implement KeyConstructor params attribute in next two days and i think it will be a better approach than such kind of __init__ redefinition.

By the way, don't forget to use *args and **kwargs in super invocation.

class CustomListKeyConstructor(DefaultKeyConstructor):
    def __init__(self, models, *args, **kwargs):
        super(CustomListKeyConstructor, self).__init__(*args, **kwargs)
        self.bits['model_bit'] = UpdatedAtKeyBit(models)

from drf-extensions.

chibisov avatar chibisov commented on August 12, 2024

I've implemented KeyConstructor params attribute http://chibisov.github.io/drf-extensions/docs/#key-constructor-params

from drf-extensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.