django-cache-mock


Namedjango-cache-mock JSON
Version 0.0.3 PyPI version JSON
download
home_page
Summary
upload_time2023-01-14 19:22:26
maintainer
docs_urlNone
authorIuri de Silvio
requires_python>=3.8,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-cache-mock

Use in-process mocks to avoid setting up external caches for Django during
development.

Django has a limited built-in `django.core.cache.backends.locmem.LocMemCache`,
to help development, but Django do some magic to always give you a working
connection.

I have some reasons to abuse Django cache this way:

* Thread safety: Django spin one connection per thread to avoid issues with
thread unsafe drivers.
* Good defaults: Django run connections with good defaults.
* Connection reuse: Django already have a pool running and in most cases it is
better to use it.

## Install

```shell
$ pip install django-cache-mock
```

Also, it is possible to install with the backends you want.

For `mockcache`, it installs [`mockcache3`](https://pypi.org/project/mockcache3/),
a fork from original package because it doesn´t work for new versions of Python.

```shell
$ pip install django-cache-mock[mockcache]
$ pip install django-cache-mock[fakeredis]
$ pip install django-cache-mock[redislite]
```

## How to use

In your Django settings you already have `CACHES` defined.

For `memcached`, it's something like that:

```python
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
        "LOCATION": os.getenv("MEMCACHED_HOSTS"),
        "OPTIONS": {
            "no_delay": True,
            "ignore_exc": True,
            "max_pool_size": 4,
            "use_pooling": True,
        },
    },
}
```

Just make a call to `django_cache_mock.patch` to replace with a mock backend.

**The lib will patch only when cache LOCATION is not defined.**

```python
import django_cache_mock

if DEBUG:  # Apply it only in debug mode to be extra careful.
    django_cache_mock.patch(CACHES, "default", "mockcache")
```

This patch replace cache with a mocked one. For mockcache,

## Custom cache options

The `patch` function accepts custom params. It can be used to override mock
behaviours, like the db file `redislite` will use, defined by `LOCATION`:

```python
django_cache_mock.patch(CACHES, "default", "redislite", {"LOCATION": "data/redis.db"})
```

## Redis backends

Redis has several options to run. This lib implements `fakeredis` and `redislite`,
with `django.core.cache` or `django-redis`.

By default, the lib try to maintain the same behavior of the original implementation.
If config uses `django-redis`, when you set use backend `fakeredis`, it will use
it as `fakeredis[django-redis]`.

```python
# Force to use django-redis. It is not necessary, the lib already try to use
# django-redis if cache uses `django_redis.cache.cache.RedisCache`.
django_cache_mock.patch(CACHES, "redis", "fakeredis[django-redis]")
django_cache_mock.patch(CACHES, "redis", "redislite[django-redis]")
```

## How to access connections

To get Django memcached and redis clients from cache:

```python
from django.core.cache import caches

def give_me_memcached():
    return caches["memcached"]._cache

# for django.core.cache.backends.redis
def give_me_primary_redis():
    return caches["redis"]._cache.get_client(write=True)

def give_me_secondary_redis():
    return caches["redis"]._cache.get_client()

# for django-redis
def give_me_primary_redis():
    return caches["redis"].client.get_client()

def give_me_secondary_redis():
    return caches["redis"].client.get_client(write=False)

# Yes, django and django-redis have different write flag defaults.
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-cache-mock",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Iuri de Silvio",
    "author_email": "iurisilvio@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/67/2c/4551dcf3478879496a333641573f2706301a56c2d5a6da98b1c8ef5570c4/django_cache_mock-0.0.3.tar.gz",
    "platform": null,
    "description": "# django-cache-mock\n\nUse in-process mocks to avoid setting up external caches for Django during\ndevelopment.\n\nDjango has a limited built-in `django.core.cache.backends.locmem.LocMemCache`,\nto help development, but Django do some magic to always give you a working\nconnection.\n\nI have some reasons to abuse Django cache this way:\n\n* Thread safety: Django spin one connection per thread to avoid issues with\nthread unsafe drivers.\n* Good defaults: Django run connections with good defaults.\n* Connection reuse: Django already have a pool running and in most cases it is\nbetter to use it.\n\n## Install\n\n```shell\n$ pip install django-cache-mock\n```\n\nAlso, it is possible to install with the backends you want.\n\nFor `mockcache`, it installs [`mockcache3`](https://pypi.org/project/mockcache3/),\na fork from original package because it doesn\u00b4t work for new versions of Python.\n\n```shell\n$ pip install django-cache-mock[mockcache]\n$ pip install django-cache-mock[fakeredis]\n$ pip install django-cache-mock[redislite]\n```\n\n## How to use\n\nIn your Django settings you already have `CACHES` defined.\n\nFor `memcached`, it's something like that:\n\n```python\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django.core.cache.backends.memcached.PyMemcacheCache\",\n        \"LOCATION\": os.getenv(\"MEMCACHED_HOSTS\"),\n        \"OPTIONS\": {\n            \"no_delay\": True,\n            \"ignore_exc\": True,\n            \"max_pool_size\": 4,\n            \"use_pooling\": True,\n        },\n    },\n}\n```\n\nJust make a call to `django_cache_mock.patch` to replace with a mock backend.\n\n**The lib will patch only when cache LOCATION is not defined.**\n\n```python\nimport django_cache_mock\n\nif DEBUG:  # Apply it only in debug mode to be extra careful.\n    django_cache_mock.patch(CACHES, \"default\", \"mockcache\")\n```\n\nThis patch replace cache with a mocked one. For mockcache,\n\n## Custom cache options\n\nThe `patch` function accepts custom params. It can be used to override mock\nbehaviours, like the db file `redislite` will use, defined by `LOCATION`:\n\n```python\ndjango_cache_mock.patch(CACHES, \"default\", \"redislite\", {\"LOCATION\": \"data/redis.db\"})\n```\n\n## Redis backends\n\nRedis has several options to run. This lib implements `fakeredis` and `redislite`,\nwith `django.core.cache` or `django-redis`.\n\nBy default, the lib try to maintain the same behavior of the original implementation.\nIf config uses `django-redis`, when you set use backend `fakeredis`, it will use\nit as `fakeredis[django-redis]`.\n\n```python\n# Force to use django-redis. It is not necessary, the lib already try to use\n# django-redis if cache uses `django_redis.cache.cache.RedisCache`.\ndjango_cache_mock.patch(CACHES, \"redis\", \"fakeredis[django-redis]\")\ndjango_cache_mock.patch(CACHES, \"redis\", \"redislite[django-redis]\")\n```\n\n## How to access connections\n\nTo get Django memcached and redis clients from cache:\n\n```python\nfrom django.core.cache import caches\n\ndef give_me_memcached():\n    return caches[\"memcached\"]._cache\n\n# for django.core.cache.backends.redis\ndef give_me_primary_redis():\n    return caches[\"redis\"]._cache.get_client(write=True)\n\ndef give_me_secondary_redis():\n    return caches[\"redis\"]._cache.get_client()\n\n# for django-redis\ndef give_me_primary_redis():\n    return caches[\"redis\"].client.get_client()\n\ndef give_me_secondary_redis():\n    return caches[\"redis\"].client.get_client(write=False)\n\n# Yes, django and django-redis have different write flag defaults.\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "",
    "version": "0.0.3",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9909f45db26ca9c272ef1c53b6c9b9e1413a484bc9db25781cf44f0dced8fe7f",
                "md5": "1edf8381ad18e119d66f98bece1037ba",
                "sha256": "7bf0a8fde24452bf91fc60590ec68f4ce8c506275a6a59b1c18c91464845a954"
            },
            "downloads": -1,
            "filename": "django_cache_mock-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1edf8381ad18e119d66f98bece1037ba",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6077,
            "upload_time": "2023-01-14T19:22:24",
            "upload_time_iso_8601": "2023-01-14T19:22:24.704647Z",
            "url": "https://files.pythonhosted.org/packages/99/09/f45db26ca9c272ef1c53b6c9b9e1413a484bc9db25781cf44f0dced8fe7f/django_cache_mock-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "672c4551dcf3478879496a333641573f2706301a56c2d5a6da98b1c8ef5570c4",
                "md5": "ce936cc49b1dfc6e8543bef70bc10a92",
                "sha256": "cadcaf6f83db551dbe19be503012f6a3c77035d69d709d377806c8d60340feef"
            },
            "downloads": -1,
            "filename": "django_cache_mock-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "ce936cc49b1dfc6e8543bef70bc10a92",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 5476,
            "upload_time": "2023-01-14T19:22:26",
            "upload_time_iso_8601": "2023-01-14T19:22:26.175831Z",
            "url": "https://files.pythonhosted.org/packages/67/2c/4551dcf3478879496a333641573f2706301a56c2d5a6da98b1c8ef5570c4/django_cache_mock-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-14 19:22:26",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "django-cache-mock"
}
        
Elapsed time: 0.07656s