Memcached

This module provides support for connecting to Memcached servers through the aiomcache library.

Usage

First install the memcached extra:

$ pip install zayt[memcached]

Define the configuration properties:

settings = {
    "modules": [
        "zayt.ext.data.memcached", # (1)
    ],
    "memcached": {
        "": { # (2)
            "address": "localhost:11211",
        },
        "other": { # (3)
            "address": "localhost:11212",
        },
    }
}

Note

  1. Activate the module

  2. Connection will be registered without a name

  3. Connection registered with name “other”

Inject the aiomcache.Client service:

from typing import Annotated
from aiomcache 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")]

Example

application/handler.py
from aiomcache 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.incr("number")
    await request.respond_json({"number": number})
configuration/settings.py
settings = {
    "memcached": {
        "": {
            "address": "localhost:11211"
        },
    },
}

Configuration options

The available options are shown below:

settings = {
    "memcached": {
        "": {
            "address": "",
            "options": {
                "pool_size": 10,
                "pool_minsize": 1,
                "get_flat_handler": some_function,
                "set_flat_handler": some_function,
                "conn_args": {},
            },
        },
    },
}