Memcached

Este módulo provê suporte para conectar a servidores Memcached através da biblioteca aiomcache.

Utilização

Primeiro instale o extra memcached:

$ pip install zayt_contrib[memcached]

Defina as propriedades de configuração:

modules = [
    "zayt_contrib.memcached", # (1)
]

[memcached.default] # (2)
nodes = ["localhost:11211"]

[memcached.other] # (3)
nodes = ["unix:///run/memcached.sock"]

Nota

  1. Ativar o módulo

  2. A conexão será registrada sem um nome

  3. Conexão registrada com nome “other”

Injete o serviço emcache.Client:

from typing import Annotated
from emcache import Client as Memcached
from zayt.di import service, Inject


@service
class MyService:
    # default service
    memcached: Annotated[Memcached, Inject]

    # named service
    other_memcached: Annotated[Memcached, Inject("other")]

Exemplo

application/handler.py
from emcache import Client as Memcached
from zayt.web import get

@get
async def index(request, memcached: Memcached):
    if not await memcached.get(b"number"):
        await memcached.set(b"number", b"0")
    number = await memcached.increment(b"number")
    await request.respond_json({"number": number})
configuration/settings.toml
[memcached.default]
nodes = ["localhost:11211"]

Configuração

Memcached can be configured through settings.toml and by providing services.

Services

The extension will look for the following service:

  • emcache.ClusterEvents

Exemplo:

from zayt import di
from emcache import ClusterEvents


class MyClusterEvents(ClusterEvents):
    pass


@di.service
def default_cluster_events() -> ClusterEvents:
    return MyClusterEvents()


@di.service(name="other")
def other_cluster_events() -> ClusterEvents:
    return MyClusterEvents()

Opções de configuração

[memcached.default]
nodes = ["memcached:11211"]
timeout = 1.0
max_connections = 2
min_connections = 1
purge_unused_connections_after = 60.0
connection_timeout = 5.0
purge_unhealthy_nodes = false
autobatching = false
autobatching_max_keys = 32
ssl = false
ssl_verify = true
ssl_extra_ca = ""
username = ""
password = ""
autodiscovery = false
autodiscovery_poll_interval = 60.0
autodiscovery_timeout = 5.0