Skip to content

Commit fa93fcd

Browse files
committed
new documentaion
1 parent 176daa5 commit fa93fcd

16 files changed

+1452
-0
lines changed

.readthedocs.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "2"
2+
3+
build:
4+
os: "ubuntu-lts-latest"
5+
tools:
6+
python: "3.12"
7+
8+
jobs:
9+
post_create_environment:
10+
- pip install poetry
11+
- poetry config virtualenvs.create false
12+
13+
post_install:
14+
-poetry install --with docs
15+
16+
sphinx:
17+
configuration: docs/source/conf.py
18+
19+
formats:
20+
- pdf
21+
- epub

CHANGES.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Version 0.0.15
2+
--------------
3+
4+
Released 2024/09/07
5+
6+
- Added this documentation!
7+
8+
- changed ``VALKEY_CLIENT_CLASS`` to ``BASE_CLIENT_CLASS`` for clarity
9+
10+
- changed ``CLIENT_KWARGS`` to ``BASE_CLIENT_KWARGS`` for clarity
11+
12+
- added some docstring to compressor classes
13+
14+
- fixed some messages generated by autocomplete

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = source
9+
BUILDDIR = build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/make.bat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
%SPHINXBUILD% >NUL 2>NUL
14+
if errorlevel 9009 (
15+
echo.
16+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
17+
echo.installed, then set the SPHINXBUILD environment variable to point
18+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
19+
echo.may add the Sphinx directory to PATH.
20+
echo.
21+
echo.If you don't have Sphinx installed, grab it from
22+
echo.https://www.sphinx-doc.org/
23+
exit /b 1
24+
)
25+
26+
if "%1" == "" goto help
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

docs/source/changes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=======
2+
Changes
3+
=======
4+
5+
.. include:: ../../CHANGES.rst

docs/source/commands/commands.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
========
2+
Commands
3+
========
4+
5+
.. toctree::
6+
:maxdepth: 2
7+
8+
connection_pool_commands
9+
valkey_native_commands
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
==========================
2+
Access the connection pool
3+
==========================
4+
5+
you can get the connection pool using this code:
6+
7+
.. code-block:: python
8+
9+
from django_valkey import get_valkey_connection
10+
11+
r = get_valkey_connection("default") # use the name defined in ``CACHES`` settings
12+
connection_pool = r.connection_pool
13+
print(f"created connections so far: {connection_pool._created_connections}")
14+
15+
16+
this will verify how many connections the pool has opened.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
===============
2+
valkey commands
3+
===============
4+
5+
if you need to use valkey operations, you can access the client as follows:
6+
7+
.. code-block:: pycon
8+
9+
>>> from django.core.cache import cache
10+
>>> cache.set("key", "value1", nx=True)
11+
True
12+
>>> cache.set("key", "value2", nx=True)
13+
False
14+
>>> cache.get("key")
15+
"value1"
16+
17+
the list of supported commands is very long, if needed you can check the methods at ``django_valkey.cache.ValkeyCache```
18+
19+
Infinite timeout
20+
****************
21+
22+
django-valkey comes with infinite timeouts supported out of the box. And it
23+
behaves in the same way as django backend contract specifies:
24+
25+
- ``timeout=0`` expires the value immediately.
26+
- ``timeout=None`` infinite timeout.
27+
28+
.. code-block:: python
29+
30+
cache.set("key", "value", timeout=None)
31+
32+
33+
Scan and Delete in bulk
34+
***********************
35+
36+
when you need to search for keys that have similar patterns, or delete them, you can use the helper methods that come with django-valkey:
37+
38+
.. code-block:: pycon
39+
40+
>>> from django.core.cache import cache
41+
>>> cache.keys("foo_*")
42+
["foo_1", "foo_2"]
43+
44+
if you are looking for a very large amount of data, this is **not** suitable; instead use ``iter_keys``.
45+
this will return a generator that you can iterate over more efficiently.
46+
47+
.. code-block:: python
48+
49+
>>> from django.core.cache import cache
50+
>>> cache.iter_keys("foo_*")
51+
<generator object algo at 0x7ff432
52+
>>> next(cache.iter_keys("foo_*))
53+
'foo_1'
54+
>>> foos = cache.iter_keys("foo_*")
55+
>>> for i in foos:
56+
print(i)
57+
'foo_1'
58+
'foo_2'
59+
60+
to delete keys, you should use ``delete_pattern`` which has the same glob pattern syntax as ``keys`` and returns the number of deleted keys.
61+
62+
.. code-block:: pycon
63+
64+
>>> from django.core.cache import cache
65+
>>> cache.delete_pattern("foo_*")
66+
2
67+
68+
To achieve the best performance while deleting many keys, you should set ``DJANGO_VALKEY_SCAN_ITERSIZE`` to a relatively
69+
high number (e.g., 100_000) by default in Django settings or pass it directly to the ``delete_pattern``.
70+
71+
.. code-block:: pycon
72+
73+
>>> from django.core.cache import cache
74+
>>> cache.delete_pattern("foo_*", itersize=100_000)
75+
76+
Get ttl (time-to-live) from key
77+
*******************************
78+
79+
with valkey you can access to ttl of any sorted key, to do so, django-valky exposes the ``ttl`` method.
80+
81+
the ttl method returns:
82+
83+
- `0` if key does not exists (or already expired).
84+
- ``None`` for keys that exist but does not have expiration.
85+
- the ttl value for any volatile key (any key that has expiration).
86+
87+
.. code-block:: pycon
88+
89+
>>> from django.core.cache import cache
90+
>>> cache.set("foo", "value", timeout=25)
91+
>>> cache.ttl("foo")
92+
25
93+
>>> cache.ttl("not-exists")
94+
0
95+
96+
you can also access the ttl of any sorted key in milliseconds, use the ``pttl`` method to do so:
97+
98+
.. code-block:: pycon
99+
100+
>>> from django.core.cache import cache
101+
>>> cache.set("foo", "value", timeout=25)
102+
>>> cache.pttl("foo")
103+
25000
104+
>>> cache.pttl("non-existent")
105+
0
106+
107+
Expire & Persist
108+
****************
109+
110+
in addition to the ``ttl`` and ``pttl`` methods, you can use the ``persist`` method so the key would have infinite timout:
111+
112+
.. code-block:: pycon
113+
114+
>>> cache.set("foo", "bar", timeout=22)
115+
>>> cache.ttl("foo")
116+
22
117+
>>> cache.persist("foo")
118+
True
119+
>>> cache.ttl("foo")
120+
None
121+
122+
you can also use ``expire`` to set a new timeout on the key:
123+
124+
.. code-block:: pycon
125+
126+
>>> cache.set("foo", "bar", timeout=22)
127+
>>> cache.expire("foo", timeout=5)
128+
True
129+
>>> cache.ttl("foo")
130+
5
131+
132+
The ``pexpire`` method can be used to set new timeout in millisecond precision:
133+
134+
135+
.. code-block:: pycon
136+
137+
>>> cache.set("foo", "bar", timeout=22)
138+
>>> cache.pexpire("foo", timeout=5505)
139+
True
140+
>>> cache.pttl("foo")
141+
5505
142+
143+
The ``expire_at`` method can be used to make the key expire at a specific moment in time:
144+
145+
.. code-block:: pycon
146+
147+
>>> cache.set("foo", "bar", timeout=22)
148+
>>> cache.expire_at("foo", datetime.now() + timedelta(hours=1))
149+
True
150+
>>> cache.ttl("foo")
151+
3600
152+
153+
The ``pexpire_at`` method can be used to make the key expire at a specific moment in time, with milliseconds precision:
154+
155+
.. code-block:: pycon
156+
157+
>>> cache.set("foo", "bar", timeout=22)
158+
>>> cache.pexpire_at("foo", datetime.now() + timedelta(milliseconds=900, hours=1))
159+
True
160+
>>> cache.ttl("foo")
161+
3601
162+
>>> cache.pttl("foo")
163+
3600900
164+
165+
Locks
166+
*****
167+
168+
django-valkey also supports locks.
169+
valkey has distributed named locks which are identical to ``threading.Lock`` so you can useit as replacement.
170+
171+
.. code-block:: python
172+
173+
with cache.lock("somekey"):
174+
do_something())
175+
176+
Access Raw client
177+
*****************
178+
179+
if the commands provided by django-valkey backend is not enough, or you want to use them in a different way, you can access the underlying client as follows:
180+
181+
.. code-block:: pycon
182+
183+
>>> from django-valkey import get_valkey_connection
184+
>>> con = get_valkey_connection("default")
185+
>>> con
186+
<valkey.client.Valkey object at 0x2dc4510>
187+
188+
**Warning**: not all clients support this feature:
189+
ShardClient will raise an exception if tried to be used like this.

docs/source/conf.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
# -- Project information -----------------------------------------------------
7+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
8+
import sphinx_pdj_theme
9+
10+
11+
project = "django-valkey"
12+
copyright = "2024, amirreza"
13+
author = "amirreza"
14+
release = "0.0.15"
15+
16+
# -- General configuration ---------------------------------------------------
17+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
18+
19+
extensions = []
20+
21+
templates_path = ["_templates"]
22+
exclude_patterns = []
23+
24+
25+
# -- Options for HTML output -------------------------------------------------
26+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
27+
28+
29+
html_theme = "sphinx_pdj_theme"
30+
html_static_path = [sphinx_pdj_theme.get_html_theme_path()]
31+
32+
html_sidebars = {
33+
"**": ["globaltoc.html", "sourcelink.html", "searchbox.html"],
34+
}

0 commit comments

Comments
 (0)