Skip to content

Commit 210c542

Browse files
committed
add docs
1 parent 7179fec commit 210c542

10 files changed

+111
-5
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ repos:
4444
hooks:
4545
- id: rstcheck
4646
additional_dependencies: [sphinx]
47-
args: ["--ignore-directives=django-admin,fieldlookup,setting", "--ignore-roles=djadmin,lookup,setting"]
47+
args: ["--ignore-directives=django-admin,django-admin-option,fieldlookup,setting", "--ignore-roles=djadmin,lookup,setting"]
4848

4949
# We use the Python version instead of the original version which seems to require Docker
5050
# https://github.com/koalaman/shellcheck-precommit

docs/source/_ext/djangodocs.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from sphinx.domains.std import Cmdoption
2+
3+
14
def setup(app):
25
app.add_object_type(
36
directivename="django-admin",
@@ -14,3 +17,4 @@ def setup(app):
1417
rolename="setting",
1518
indextemplate="pair: %s; setting",
1619
)
20+
app.add_directive("django-admin-option", Cmdoption)

docs/source/_static/custom.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p.admonition-title::after {
2+
/* Remove colon after admonition titles. */
3+
content: none;
4+
}

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@
5252
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
5353

5454
html_theme = "alabaster"
55-
# html_static_path = ["_static"]
55+
html_static_path = ["_static"]

docs/source/index.rst

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Forms
4848

4949
- :doc:`ref/forms`
5050

51+
Core functionalities
52+
====================
53+
54+
- :doc:`topics/cache`
55+
5156
Miscellaneous
5257
=============
5358

docs/source/ref/django-admin.rst

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
===================
2+
Management commands
3+
===================
4+
5+
Django MongoDB Backend includes some :doc:`Django management commands
6+
<django:ref/django-admin>`.
7+
8+
Required configuration
9+
======================
10+
11+
To make these commands available, you must include ``"django_mongodb_backend"``
12+
in the :setting:`INSTALLED_APPS` setting.
13+
14+
Available commands
15+
==================
16+
17+
``createcachecollection``
18+
-------------------------
19+
20+
.. django-admin:: createcachecollection
21+
22+
Creates the cache collection for use with the :doc:`database cache backend
23+
</topics/cache>` using the information from your settings file.
24+
25+
.. django-admin-option:: --database DATABASE
26+
27+
Specifies the database in which the cache collection(s) will be created.
28+
Defaults to ``default``.

docs/source/ref/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ API reference
77

88
models/index
99
forms
10+
django-admin
1011
utils

docs/source/topics/cache.rst

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
================
2+
Database caching
3+
================
4+
5+
.. class:: django_mongodb_backend.cache.MongoDBCache
6+
7+
You can configure :doc:`Django's caching API <django:topics/cache>` to store
8+
its data in MongoDB.
9+
10+
To use a database collection as your cache backend:
11+
12+
* Set :setting:`BACKEND <CACHES-BACKEND>` to
13+
``django_mongodb_backend.cache.MongoDBCache``
14+
15+
* Set :setting:`LOCATION <CACHES-LOCATION>` to ``collection_name``, the name of
16+
the MongoDB collection. This name can be whatever you want, as long as it's a
17+
valid collection name that's not already being used in your database.
18+
19+
In this example, the cache collection's name is ``my_cache_collection``::
20+
21+
CACHES = {
22+
"default": {
23+
"BACKEND": "django_mongodb_backend.cache.MongoDBCache",
24+
"LOCATION": "my_cache_collection",
25+
},
26+
}
27+
28+
Unlike other cache backends, the database cache does not support automatic
29+
culling of expired entries at the database level. Instead, expired cache
30+
entries are culled each time ``add()``, ``set()``, or ``touch()`` is called.
31+
32+
Creating the cache collection
33+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
35+
Before using the database cache, you must create the cache collection with this
36+
command:
37+
38+
.. code-block:: shell
39+
40+
python manage.py createcachecollection
41+
42+
.. admonition:: Didn't work?
43+
44+
If you get the error ``Unknown command: 'createcachecollection'``, ensure
45+
``"django_mongodb_backend"`` is in your :setting:`INSTALLED_APPS` setting.
46+
47+
This creates a collection in your database with the proper indexes. The name of
48+
the collection is taken from :setting:`LOCATION <CACHES-LOCATION>`.
49+
50+
If you are using multiple database caches, :djadmin:`createcachecollection`
51+
creates one collection for each cache.
52+
53+
If you are using multiple databases, :djadmin:`createcachecollection` observes
54+
the ``allow_migrate()`` method of your database routers (see the
55+
`Multiple databases`__ section of Django's caching docs).
56+
57+
.. __: https://docs.djangoproject.com/en/5.1/topics/cache/#multiple-databases
58+
59+
Like :djadmin:`migrate`, :djadmin:`createcachecollection` won't touch an
60+
existing collection. It will only create missing collections.

docs/source/topics/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ know:
88
.. toctree::
99
:maxdepth: 2
1010

11+
cache
1112
embedded-models
1213
known-issues

docs/source/topics/known-issues.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Due to the lack of ability to introspect MongoDB collection schema,
9797
Caching
9898
=======
9999

100-
:ref:`Database caching <database-caching>` is not supported since the built-in
101-
database cache backend requires SQL. A custom cache backend for MongoDB will be
102-
provided in the future.
100+
:doc:`Database caching</topics/cache>` uses this library's
101+
:djadmin:`createcachecollection` command rather Django's SQL-specific
102+
:djadmin:`createcachetable`.
103+
104+
Secondly, you must use the :class:`django_mongodb_backend.cache.MongoDBCache`
105+
backend rather than Django's built-in database cache backend.

0 commit comments

Comments
 (0)