Skip to content

Commit 6414fb4

Browse files
authored
Update README.rst according to the new async features (#334)
* README.rst update according to the new async features * fix: Fix the title underline * feat: Add AsyncMongoClient description to README, and cover async mongo tests with the async Mongo client * fix: Supress the filterwarning for Python 3.12+ * fix: Use simple pytest.skip for compatibility checks * feat: Revert async mongo tests for compatibility
1 parent b3933d5 commit 6414fb4

1 file changed

Lines changed: 74 additions & 6 deletions

File tree

README.rst

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,23 @@ For the latest version supporting Python 2.7 please use:
4343
Features
4444
========
4545

46+
Current features
47+
----------------
48+
4649
* Pure Python.
4750
* Compatible with Python 3.9+ (Python 2.7 was discontinued in version 1.2.8).
4851
* Supported and `tested on Linux, OS X and Windows <https://travis-ci.org/shaypal5/cachier>`_.
4952
* A simple interface.
5053
* Defining "shelf life" for cached values.
51-
* Local caching using pickle files.
52-
* Cross-machine caching using MongoDB.
53-
* Redis-based caching for high-performance scenarios.
54+
* Async and sync functions support.
55+
* Multiple caching backends (cores):
56+
57+
* Local caching using pickle files.
58+
* In-memory caching using the memory core.
59+
* Cross-machine caching using MongoDB.
60+
* SQL-based caching using SQLAlchemy-supported databases.
61+
* Redis-based caching for high-performance scenarios.
62+
5463
* Thread-safety.
5564
* **Per-call max age:** Specify a maximum age for cached values per call.
5665

@@ -362,6 +371,8 @@ MongoDB Core
362371
------------
363372
You can set a MongoDB-based cache by assigning ``mongetter`` with a callable that returns a ``pymongo.Collection`` object with writing permissions:
364373

374+
**Usage Example (MongoDB sync):**
375+
365376
.. code-block:: python
366377
367378
from pymongo import MongoClient
@@ -375,6 +386,26 @@ You can set a MongoDB-based cache by assigning ``mongetter`` with a callable tha
375386
376387
@cachier(mongetter=my_mongetter)
377388
389+
**Usage Example (MongoDB async):**
390+
391+
.. code-block:: python
392+
393+
import asyncio
394+
from pymongo.asynchronous.mongo_client import AsyncMongoClient
395+
from cachier import cachier
396+
397+
client = AsyncMongoClient("mongodb://localhost:27017")
398+
399+
async def my_async_mongetter():
400+
return client["cachier_db"]["someapp_cachier_db"]
401+
402+
@cachier(mongetter=my_async_mongetter)
403+
async def my_async_func(x: int) -> int:
404+
await asyncio.sleep(0.01)
405+
return x * 2
406+
407+
**Note:** An async ``mongetter`` callable is supported only for async cached functions.
408+
378409
This allows you to have a cross-machine, albeit slower, cache. This functionality requires that the installation of the ``pymongo`` python package.
379410

380411
In certain cases the MongoDB backend might leave a deadlock behind, blocking all subsequent requests from being processed. If you encounter this issue, supply the ``wait_for_calc_timeout`` with a reasonable number of seconds; calls will then wait at most this number of seconds before triggering a recalculation.
@@ -404,7 +435,7 @@ SQLAlchemy (SQL) Core
404435

405436
Cachier supports a generic SQL backend via SQLAlchemy, allowing you to use SQLite, PostgreSQL, MySQL, and other databases.
406437

407-
**Usage Example (SQLite in-memory):**
438+
**Usage Example (SQLite in-memory sync):**
408439

409440
.. code-block:: python
410441
@@ -414,15 +445,27 @@ Cachier supports a generic SQL backend via SQLAlchemy, allowing you to use SQLit
414445
def my_func(x):
415446
return x * 2
416447
417-
**Usage Example (PostgreSQL):**
448+
**Usage Example (PostgreSQL sync):**
418449

419450
.. code-block:: python
420451
421452
@cachier(backend="sql", sql_engine="postgresql+psycopg://user:pass@localhost/dbname")
422453
def my_func(x):
423454
return x * 2
424455
425-
**Usage Example (MySQL):**
456+
**Usage Example (PostgreSQL async):**
457+
458+
.. code-block:: python
459+
460+
import asyncio
461+
from cachier import cachier
462+
463+
@cachier(backend="sql", sql_engine="postgresql+psycopg://user:pass@localhost/dbname")
464+
async def my_async_func(x: int) -> int:
465+
await asyncio.sleep(0.01)
466+
return x * 2
467+
468+
**Usage Example (MySQL sync):**
426469

427470
.. code-block:: python
428471
@@ -480,6 +523,31 @@ Cachier supports Redis-based caching for high-performance scenarios. Redis provi
480523
def my_func(x):
481524
return x * 2
482525
526+
**Usage Example (Async Redis client with async function):**
527+
528+
.. code-block:: python
529+
530+
import asyncio
531+
import redis.asyncio as redis
532+
from cachier import cachier
533+
534+
async def get_redis_client():
535+
return redis.Redis(host="localhost", port=6379, db=0)
536+
537+
@cachier(backend="redis", redis_client=get_redis_client)
538+
async def my_async_func(x: int) -> int:
539+
await asyncio.sleep(0.01)
540+
return x * 2
541+
542+
async def main():
543+
val1 = await my_async_func(3)
544+
val2 = await my_async_func(3)
545+
assert val1 == val2
546+
547+
asyncio.run(main())
548+
549+
**Note:** An async ``redis_client`` callable is supported only for async cached functions.
550+
483551
**Configuration Options:**
484552

485553
- ``sql_engine``: SQLAlchemy connection string, Engine, or callable returning an Engine.

0 commit comments

Comments
 (0)