Metadata-Version: 2.1
Name: django-cache-pydantic
Version: 0.0.4
Summary: Django application to integrate pydantic models into django cache with an orm interface.
Author-email: Ali Abharya <abharya.dev@gmail.com>
Maintainer-email: Ali Abharya <abharya.dev@gmail.com>
License: MIT
Project-URL: Changelog, https://github.com/bindruid/django-cache-pydantic/blob/master/CHANGELOG.rst
Project-URL: Repository, https://github.com/bindruid/django-cache-pydantic
Keywords: cache,django,pydantic,orm
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.2
Classifier: Framework :: Django :: 4.0
Classifier: Framework :: Django :: 4.1
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Typing :: Typed
Requires-Python: >=3.8
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: Pydantic>=2.6
Requires-Dist: Django>=3.2

Django Cache Pydantic
=========================

Django Cache Pydantic is a minimal wrapper around django cache framework which allows you
to create pydantic instances directly inside your django project cache and retrieve them
using a similar interface like django orm.

Status
------

.. image:: https://github.com/bindruid/django-cache-pydantic/workflows/Test/badge.svg?branch=master
   :target: https://github.com/bindruid/django-cache-pydantic/actions

.. image:: https://img.shields.io/pypi/v/django-cache-pydantic.svg
   :target: https://pypi.python.org/pypi/django-cache-pydantic

.. image:: https://img.shields.io/pypi/pyversions/django-cache-pydantic.svg
   :target: https://pypi.org/project/django-cache-pydantic

.. image:: https://img.shields.io/pypi/djversions/django-cache-pydantic.svg
   :target: https://pypi.org/project/django-cache-pydantic/

Dependencies
------------

-  Pydantic >= 2.6
-  Django >= 3.2

Install
-------

.. code-block:: bash

   pip install django-cache-pydantic

Usage
-----

1. Edit settings.py and add `django_cache_pydantic` to your `INSTALLED_APPS` (also config `CACHES` setting).

2. Inherit your pydantic model from `PydanticCachedModel`.

.. code-block:: python

    from pydantic import Field, Optional
    from datetime import datetime
    from django_cache_pydantic import PydanticCachedModel


    class Person(PydanticCachedModel):
        national_id: str = Field(max_length=10, pattern=r'^\d*$')
        first_name: Optional[str] = None
        last_name: Optional[str] = None
        mobile_number: str = Field(max_length=11, pattern=r'^\d*$')
        created_at: datetime = Field(default_factory=lambda: datetime.now())

        class CacheMeta:
            ttl = 5 * 60
            primary_key_field = 'national_id'

3. Create your model instance directly into the cache via calling to `save` method or object manager `create` method.

.. code-block:: python

    # some where in your views
    person = Person(national_id='123456789', mobile_number='0930444444')
    person.save()  # will save the instance into project default cache

.. code-block:: python

    # some where in your views
    Person.objects.create(national_id='123456789', mobile_number='0930444444')  # will save the instance into project default cache

4. Retrieve your model instance from the cache via calling to object manager `get` method.

.. code-block:: python

    # some where in your views
    person = Person.objects.get(pk='123456789')
    if person is not None:
        # do some stuff

Cache pydantic meta class
---------------------------

- You can control cache pydantic models behavior using a custom meta class called `CacheMeta`.

.. code-block:: python

    class CacheMeta:
        cache_backend: str  # refers to a predefined cache settings
        ttl: int  # default timeout for instance to live in cache
        primary_key_field: str  # could be set to be used as cache key
        verbose: str  # verbose name of base model

Cache pydantic Project Settings
----------------------------------

- Default cache to save pydantic models into.

.. code-block:: python

    CACHE_PYDANTIC_DEFAULT_CACHE

- Default time to live of the pydantic cached models.

.. code-block:: python

    CACHE_PYDANTIC_DEFAULT_TTL
