Code Monkey home page Code Monkey logo

django-cache-extension's Introduction

===== Django cache extension

Build Status

Add extension methods to cache

Quick start

  1. Install cache extension by pip::

    pip install django-cache-extension

  2. For redis backend use cache like this::

    config your cache file backend to cache_extension:

    CACHES={
          "default": {
              'BACKEND': 'cache_extension.backends.redis.ExtensionRedisBackend',
              'LOCATION': 'redis://redis:6379/0',
              'TIMEOUT': '172800',
              "KEY_PREFIX": "cache_extension",
              'OPTIONS': {
                  "DB": 0,
                  "CLIENT_CLASS": "django_redis.client.DefaultClient",
                  'PARSER_CLASS': 'redis.connection.HiredisParser',
                  'PICKLE_VERSION': 2,
              }
          }
      },
    
  3. For custom cache backend::

    from cache_extension.cache import ExtensionCache
    from some_module import CustomCache
    class ExtensionCustomCache(ExtensionCache, CustomCache):
        pass
    
  4. Use extension cache methods::

    cache.get_model(Article, pk=1)
    cache.get_models(Article, [1,2,3])
    cache.get_model(UserArticle, user_id=1, article_id=1)
    cache.get_model_list(UserArticle, user_id=1)
    
  5. Built in cached fields: ForeignKeyField, OneToOneField. Say you have two models defined as below:

    from django.db import models
    from cache_extension import cached_fields
    
    
    class Foo(models.Model):
        pass
    
    class Bar(models.Model):
        foo = cached_fields.ForeignKeyField(Foo)
    
    

    Then instances of Bar will have a cached_foo property which will try to fetch foo from cache first, if cache misses, will fallback to database.

    bar = Bar.objects.get(pk=1)
    bar.cached_foo # cached foreign field, really handy
    

django-cache-extension's People

Contributors

maoisdamao avatar shellfly avatar stdioa avatar

Watchers

 avatar  avatar

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.