Memcached
This extension provides support for connecting to Memcached servers. It registers the
aiomcache.Client
service.
Usage
First install the memcached
extra:
Define the configuration properties:
Inject the aiomcache.Client
service:
from typing import Annotated
from aiomcache import Client as Memcached
from selva.di import service, Inject
@service
class MyService:
# default service
memcached: Annotated[Memcached, Inject]
# named service
other_memcached: Annotated[Memcached, Inject(name="other")]
Using environment variables
Example
from typing import Annotated
from aiomcache import Client as Memcached
from asgikit.responses import respond_json
from selva.di import Inject
from selva.web import get
@get
async def index(request, memcached: Annotated[Memcached, Inject]):
if not await memcached.get(b"number"):
await memcached.set(b"number", b"0")
number = await memcached.incr("number")
await respond_json(request.response, {"number": number})
Configuration options
Selva offers several options to configure Memcached. If you need more control over
the Memcached service, you can create your own aiomcache.Client
service.
The available options are shown below:
data:
memcached:
default:
address: ""
options:
pool_size: 10
pool_minsize: 1
get_flat_handler: "package.module.function" # (1)
set_flat_handler: "package.module.function" # (2)
conn_args: "package.module:variable" # (3)
- dotted path to a python function
- dotted path to a python function
- dotted path to a python variable