Skip to main content

Django Models Redis Cache (DMoReCa), library that gives your specified Django models regular caching via Redis

Project description

django-models-redis-cache

Django Models Redis Cache (DMoReCa), library that gives your specified Django models regular caching via Redis

For one project, I needed to work with redis, but redis-py provides a minimum level of work with redis. I didn't find any Django-like ORM for redis, so I wrote library python-redis-orm (PyPI).

Also, if you are searching for just django-like redis ORM, please check django-redis-orm (PyPI).

And this library is port to Django that provides easy-to-use Django models caching via Redis.

Working with this library, you are expected:

  • Fully works in 2021
  • Easy adaptation to your needs
  • Adequate informational messages and error messages
  • Built-in RedisRoot class that stores specified models, with:
    • redis_instance setting - your redis connection (from redis-py)
    • prefix setting - prefix of this RedisRoot to be stored in redis
    • ignore_deserialization_errors setting - do not raise errors, while deserealizing data
    • save_consistency setting - show structure-first data
    • economy setting - to not return full data and save some requests (usually, speeds up your app on 80%)
  • CRUD (Create Read Update Delete), in our variation: save, get, filter, order, update, delete:
    • example_instance = ExampleModel(example_field='example_data').save() - to create an instance and get its data dict
    • filtered_example_instances = redis_root.get(ExampleModel, example_field='example_data') - to get all ExampleModel instances with example_field filter and get its data dict
    • ordered_instances = redis_root.order(filtered_example_instances, '-id') - to get ordered filtered_example_instances by id ('-' for reverse)
    • updated_example_instances = redis_root.update(ExampleModel, ordered_instances, example_field='another_example_data') - to update all ordered_instances example_field with value 'another_example_data' and get its data dict
    • redis_root.delete(ExampleModel, updated_example_instances) - to delete updated_example_instances

Installation

pip install django-models-redis-cache

Here is PyPI

Add "django_models_redis_cache" to your INSTALLED_APPS setting like this::

INSTALLED_APPS = [
    ...
    'django_models_redis_cache',
]

Usage

You can set this part in your project settings.py

from django_models_redis_cache.core import *


def get_connection_pool():
    host = 'localhost'
    port = 6379
    db = 0
    connection_pool = redis.ConnectionPool(
        decode_responses=True,
        host=host,
        port=port,
        db=db,
    )
    return connection_pool


REDIS_ROOTS = {
    'test_caching_root': RedisRoot(
        prefix='test_caching',
        connection_pool=get_connection_pool(),
        ignore_deserialization_errors=True,
        economy=True
    )
}

Run in the background

You can just copy it to:

app/management/commands/command_name.py

And just run with:

python manage.py command_name

Help:

Django custom management commands

How to import something from settings

if redis_roots:
    if type(redis_roots) == dict:
        some_caching_redis_root = redis_roots['test_caching_root']
        some_caching_redis_root.register_django_models({
            DjangoModelToCache1: {
                'enabled': True,
                'ttl': 60 * 5,  # Cache every 5 mins
                'prefix': f'test_caching_root-DjangoModelToCache1-cache', # please make it unique
            },
            # DjangoModelToCache2: {
            #     'enabled': True,
            #     'ttl': 60 * 10,  # Cache every 10 mins
            #     'prefix': f'test_caching_root-DjangoModelToCache2-cache', # please make it unique
            # },
            # ...
        })
        # another_caching_redis_root = redis_roots['another_test_caching_root']
        # some_caching_redis_root.registered_django_models({...})
        roots_to_cache = [
            some_caching_redis_root,
            # another_caching_redis_root
        ]
        print('STARTING CACHING')
        while True:
            for redis_root in roots_to_cache:
                redis_root.check_cache()
    else:
        raise Exception('redis_roots must be dict')
else:
    raise Exception('No redis_roots')

Release history Release notifications | RSS feed

This version

0.8.4

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-models-redis-cache-0.8.4.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_models_redis_cache-0.8.4-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file django-models-redis-cache-0.8.4.tar.gz.

File metadata

  • Download URL: django-models-redis-cache-0.8.4.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.3 Windows/10

File hashes

Hashes for django-models-redis-cache-0.8.4.tar.gz
Algorithm Hash digest
SHA256 c519b42db749dd87a2596bf9a3f785d248cf87d725f6461f8536b081fffbc6b2
MD5 985eb25fa5683188e8630020d681de63
BLAKE2b-256 d7537eb27477cfda5c51f39eb76ff5f633bbee2509aeac2cbd9228cc730869ad

See more details on using hashes here.

File details

Details for the file django_models_redis_cache-0.8.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_models_redis_cache-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e0f10da59d4ef7ef2bfe30f38fe3b9b6e319877c85d6f76676e7ed39ef8a0599
MD5 5da6b8abd3016fe5ebaf0efc410b2da0
BLAKE2b-256 4e6844eb9a0aeab6c220d9124382e8335b534b82f5a4fce77622383a4ec05f1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page