Skip to content

Commit

Permalink
new documentaion
Browse files Browse the repository at this point in the history
  • Loading branch information
amirreza8002 committed Sep 7, 2024
1 parent 176daa5 commit 28c956b
Show file tree
Hide file tree
Showing 16 changed files with 1,449 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "2"

build:
os: "ubuntu-lts-latest"
tools:
python: "3.12"

jobs:
pre_build:
- pip install poetry
- poetry export --without-hashes -f requirements.txt -o requirements.txt --only docs
- pip install -r requirements.txt
sphinx:
configuration: docs/source/conf.py

formats:
- pdf
- epub
14 changes: 14 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Version 0.0.15
--------------

Released 2024/09/07

- Added this documentation!

- changed ``VALKEY_CLIENT_CLASS`` to ``BASE_CLIENT_CLASS`` for clarity

- changed ``CLIENT_KWARGS`` to ``BASE_CLIENT_KWARGS`` for clarity

- added some docstring to compressor classes

- fixed some messages generated by autocomplete
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
5 changes: 5 additions & 0 deletions docs/source/changes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=======
Changes
=======

.. include:: ../../CHANGES.rst
9 changes: 9 additions & 0 deletions docs/source/commands/commands.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
========
Commands
========

.. toctree::
:maxdepth: 2

connection_pool_commands
valkey_native_commands
16 changes: 16 additions & 0 deletions docs/source/commands/connection_pool_commands.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
==========================
Access the connection pool
==========================

you can get the connection pool using this code:

.. code-block:: python
from django_valkey import get_valkey_connection
r = get_valkey_connection("default") # use the name defined in ``CACHES`` settings
connection_pool = r.connection_pool
print(f"created connections so far: {connection_pool._created_connections}")
this will verify how many connections the pool has opened.
189 changes: 189 additions & 0 deletions docs/source/commands/valkey_native_commands.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
===============
valkey commands
===============

if you need to use valkey operations, you can access the client as follows:

.. code-block:: pycon
>>> from django.core.cache import cache
>>> cache.set("key", "value1", nx=True)
True
>>> cache.set("key", "value2", nx=True)
False
>>> cache.get("key")
"value1"
the list of supported commands is very long, if needed you can check the methods at ``django_valkey.cache.ValkeyCache```

Infinite timeout
****************

django-valkey comes with infinite timeouts supported out of the box. And it
behaves in the same way as django backend contract specifies:

- ``timeout=0`` expires the value immediately.
- ``timeout=None`` infinite timeout.

.. code-block:: python
cache.set("key", "value", timeout=None)
Scan and Delete in bulk
***********************

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:

.. code-block:: pycon
>>> from django.core.cache import cache
>>> cache.keys("foo_*")
["foo_1", "foo_2"]
if you are looking for a very large amount of data, this is **not** suitable; instead use ``iter_keys``.
this will return a generator that you can iterate over more efficiently.

.. code-block:: python
>>> from django.core.cache import cache
>>> cache.iter_keys("foo_*")
<generator object algo at 0x7ff432
>>> next(cache.iter_keys("foo_*))
'foo_1'
>>> foos = cache.iter_keys("foo_*")
>>> for i in foos:
print(i)
'foo_1'
'foo_2'
to delete keys, you should use ``delete_pattern`` which has the same glob pattern syntax as ``keys`` and returns the number of deleted keys.
.. code-block:: pycon
>>> from django.core.cache import cache
>>> cache.delete_pattern("foo_*")
2
To achieve the best performance while deleting many keys, you should set ``DJANGO_VALKEY_SCAN_ITERSIZE`` to a relatively
high number (e.g., 100_000) by default in Django settings or pass it directly to the ``delete_pattern``.
.. code-block:: pycon
>>> from django.core.cache import cache
>>> cache.delete_pattern("foo_*", itersize=100_000)
Get ttl (time-to-live) from key
*******************************
with valkey you can access to ttl of any sorted key, to do so, django-valky exposes the ``ttl`` method.
the ttl method returns:
- `0` if key does not exists (or already expired).
- ``None`` for keys that exist but does not have expiration.
- the ttl value for any volatile key (any key that has expiration).
.. code-block:: pycon
>>> from django.core.cache import cache
>>> cache.set("foo", "value", timeout=25)
>>> cache.ttl("foo")
25
>>> cache.ttl("not-exists")
0
you can also access the ttl of any sorted key in milliseconds, use the ``pttl`` method to do so:
.. code-block:: pycon
>>> from django.core.cache import cache
>>> cache.set("foo", "value", timeout=25)
>>> cache.pttl("foo")
25000
>>> cache.pttl("non-existent")
0
Expire & Persist
****************
in addition to the ``ttl`` and ``pttl`` methods, you can use the ``persist`` method so the key would have infinite timout:
.. code-block:: pycon
>>> cache.set("foo", "bar", timeout=22)
>>> cache.ttl("foo")
22
>>> cache.persist("foo")
True
>>> cache.ttl("foo")
None
you can also use ``expire`` to set a new timeout on the key:
.. code-block:: pycon
>>> cache.set("foo", "bar", timeout=22)
>>> cache.expire("foo", timeout=5)
True
>>> cache.ttl("foo")
5
The ``pexpire`` method can be used to set new timeout in millisecond precision:
.. code-block:: pycon
>>> cache.set("foo", "bar", timeout=22)
>>> cache.pexpire("foo", timeout=5505)
True
>>> cache.pttl("foo")
5505
The ``expire_at`` method can be used to make the key expire at a specific moment in time:
.. code-block:: pycon
>>> cache.set("foo", "bar", timeout=22)
>>> cache.expire_at("foo", datetime.now() + timedelta(hours=1))
True
>>> cache.ttl("foo")
3600
The ``pexpire_at`` method can be used to make the key expire at a specific moment in time, with milliseconds precision:
.. code-block:: pycon
>>> cache.set("foo", "bar", timeout=22)
>>> cache.pexpire_at("foo", datetime.now() + timedelta(milliseconds=900, hours=1))
True
>>> cache.ttl("foo")
3601
>>> cache.pttl("foo")
3600900
Locks
*****
django-valkey also supports locks.
valkey has distributed named locks which are identical to ``threading.Lock`` so you can useit as replacement.
.. code-block:: python
with cache.lock("somekey"):
do_something())
Access Raw client
*****************
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:
.. code-block:: pycon
>>> from django-valkey import get_valkey_connection
>>> con = get_valkey_connection("default")
>>> con
<valkey.client.Valkey object at 0x2dc4510>
**Warning**: not all clients support this feature:
ShardClient will raise an exception if tried to be used like this.
34 changes: 34 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import sphinx_pdj_theme


project = "django-valkey"
copyright = "2024, amirreza"
author = "amirreza"
release = "0.0.15"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = []

templates_path = ["_templates"]
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output


html_theme = "sphinx_pdj_theme"
html_static_path = [sphinx_pdj_theme.get_html_theme_path()]

html_sidebars = {
"**": ["globaltoc.html", "sourcelink.html", "searchbox.html"],
}
Loading

0 comments on commit 28c956b

Please sign in to comment.