django-cacheable-model


Namedjango-cacheable-model JSON
Version 1.2.7 PyPI version JSON
download
home_pageNone
SummaryA cacheable Django model
upload_time2024-10-21 02:02:30
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7.4
licenseNone
keywords
VCS
bugtrack_url
requirements asgiref Django sqlparse djangorestframework
Travis-CI No Travis.
coveralls test coverage
            # django cacheable model
A cacheable model for django.

* A generic way of creating cache keys from Django model fields
* Retrieve django models from cache with field values (cache on the way if cache missed)
* Retrieve all the model instances (suitable for small set of models)
* Support for cache aliases

See usage example below

# 1. Install
pip install django_cacheable_model

# 2. Configuration
* `DEFAULT_CACHE_ALIAS` the default cache name to use from `settings.CACHES`. 
   If not set default alias is name `"default"` and must be configured in CACHES.
* `CACHE_SET_MANY_LIMIT` is chunk size for calls to `cache.set_many`.  
   when `all_ins_from_cache` brings in all entries from cache, it will set each object  
   in chunks to control request size. Default is `5` i.e if there are 10 instances of a model  
   from db this config will set each of the models to the cache in two groups of `5`
```python
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

CACHE_DEFAULT_ALIAS = 'example_cache_alias'
CACHE_SET_MANY_LIMIT = 10
CACHES = {
   ...,
    'example_cache_alias': {
        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
        'LOCATION': 'memcached:11211',
    },
    ...
}
```

# 3. Usage

See samples in  `example_django_project` views.py and models.py.

### 3.1. Create a model that inherits from CacheableModel
```python
class Question(CacheableModel):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(CacheableModel):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
```

### 3.2. Use cache operations from django_cacheable_model.utils

```python
from django_cacheable_model.utils import all_ins_from_cache, model_ins_from_cache

# Get all instances of model from cache (use for smaller set of models)
context['choices'] = all_ins_from_cache(Choice)

# Get all instances with select_related and order_by
choices = all_ins_from_cache(Choice,
                             select_related=('question',),
                             order_by_fields=('-id',))

# Get a single model. Note this method returns a list of matching objects
context['choice'] = model_ins_from_cache(Choice, {'id': 5})[-1]
```

# 4. License
Apache2 License

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-cacheable-model",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7.4",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "\"Harisankar Krishna Swamy @ Vevde\" <harisankar.krishna@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/31/ad/74cd28aa3a361114ddca4a47e8494f88f0b94168584b4a644bbd87cd525a/django_cacheable_model-1.2.7.tar.gz",
    "platform": null,
    "description": "# django cacheable model\nA cacheable model for django.\n\n* A generic way of creating cache keys from Django model fields\n* Retrieve django models from cache with field values (cache on the way if cache missed)\n* Retrieve all the model instances (suitable for small set of models)\n* Support for cache aliases\n\nSee usage example below\n\n# 1. Install\npip install django_cacheable_model\n\n# 2. Configuration\n* `DEFAULT_CACHE_ALIAS` the default cache name to use from `settings.CACHES`. \n   If not set default alias is name `\"default\"` and must be configured in CACHES.\n* `CACHE_SET_MANY_LIMIT` is chunk size for calls to `cache.set_many`.  \n   when `all_ins_from_cache` brings in all entries from cache, it will set each object  \n   in chunks to control request size. Default is `5` i.e if there are 10 instances of a model  \n   from db this config will set each of the models to the cache in two groups of `5`\n```python\nDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'\n\nCACHE_DEFAULT_ALIAS = 'example_cache_alias'\nCACHE_SET_MANY_LIMIT = 10\nCACHES = {\n   ...,\n    'example_cache_alias': {\n        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',\n        'LOCATION': 'memcached:11211',\n    },\n    ...\n}\n```\n\n# 3. Usage\n\nSee samples in  `example_django_project` views.py and models.py.\n\n### 3.1. Create a model that inherits from CacheableModel\n```python\nclass Question(CacheableModel):\n    question_text = models.CharField(max_length=200)\n    pub_date = models.DateTimeField('date published')\n\n\nclass Choice(CacheableModel):\n    question = models.ForeignKey(Question, on_delete=models.CASCADE)\n    choice_text = models.CharField(max_length=200)\n    votes = models.IntegerField(default=0)\n```\n\n### 3.2. Use cache operations from django_cacheable_model.utils\n\n```python\nfrom django_cacheable_model.utils import all_ins_from_cache, model_ins_from_cache\n\n# Get all instances of model from cache (use for smaller set of models)\ncontext['choices'] = all_ins_from_cache(Choice)\n\n# Get all instances with select_related and order_by\nchoices = all_ins_from_cache(Choice,\n                             select_related=('question',),\n                             order_by_fields=('-id',))\n\n# Get a single model. Note this method returns a list of matching objects\ncontext['choice'] = model_ins_from_cache(Choice, {'id': 5})[-1]\n```\n\n# 4. License\nApache2 License\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A cacheable Django model",
    "version": "1.2.7",
    "project_urls": {
        "Homepage": "https://github.com/harisankar-krishna-swamy/django_cacheable_model.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "aa782caae61d993ede91d2f403802caf495e288a4325d4ef30ac8c5545df30ba",
                "md5": "d467f2712b3ec87d163970b43d51954e",
                "sha256": "a9d71f1b5e4c14b8e6090e9b266e10090725ff2c20600e8bc08d05b790c4caff"
            },
            "downloads": -1,
            "filename": "django_cacheable_model-1.2.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d467f2712b3ec87d163970b43d51954e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.4",
            "size": 5985,
            "upload_time": "2024-10-21T02:02:27",
            "upload_time_iso_8601": "2024-10-21T02:02:27.132689Z",
            "url": "https://files.pythonhosted.org/packages/aa/78/2caae61d993ede91d2f403802caf495e288a4325d4ef30ac8c5545df30ba/django_cacheable_model-1.2.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "31ad74cd28aa3a361114ddca4a47e8494f88f0b94168584b4a644bbd87cd525a",
                "md5": "0cc5b9c202fbcb77b20e49f43fa1d601",
                "sha256": "56925ea7ccafe66ec033edcbb3fb95d99f77a4940c052dc8fca6ba3bb2c02fbd"
            },
            "downloads": -1,
            "filename": "django_cacheable_model-1.2.7.tar.gz",
            "has_sig": false,
            "md5_digest": "0cc5b9c202fbcb77b20e49f43fa1d601",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.4",
            "size": 5099,
            "upload_time": "2024-10-21T02:02:30",
            "upload_time_iso_8601": "2024-10-21T02:02:30.212937Z",
            "url": "https://files.pythonhosted.org/packages/31/ad/74cd28aa3a361114ddca4a47e8494f88f0b94168584b4a644bbd87cd525a/django_cacheable_model-1.2.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-21 02:02:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "harisankar-krishna-swamy",
    "github_project": "django_cacheable_model",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "requirements": [
        {
            "name": "asgiref",
            "specs": [
                [
                    "==",
                    "3.6.0"
                ]
            ]
        },
        {
            "name": "Django",
            "specs": [
                [
                    "~=",
                    "4.2.16"
                ]
            ]
        },
        {
            "name": "sqlparse",
            "specs": [
                [
                    "==",
                    "0.5.0"
                ]
            ]
        },
        {
            "name": "djangorestframework",
            "specs": [
                [
                    "==",
                    "3.15.2"
                ]
            ]
        }
    ],
    "lcname": "django-cacheable-model"
}
        
Elapsed time: 0.47123s