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[memcached]
Defina as propriedades de configuração:
settings = {
"modules": [
"zayt.ext.data.memcached", # (1)
],
"memcached": {
"": { # (2)
"address": "localhost:11211",
},
"other": { # (3)
"address": "localhost:11212",
},
}
}
Nota
Ativar o módulo
Connection will be registered without a name
Conexão registrada com nome “other”
Injete o serviço aiomcache.Client:
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")]
Exemplo¶
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"
},
},
}
Opções de configuração¶
As opções disponíveis são mostradas abaixo:
settings = {
"memcached": {
"": {
"address": "",
"options": {
"pool_size": 10,
"pool_minsize": 1,
"get_flat_handler": some_function,
"set_flat_handler": some_function,
"conn_args": {},
},
},
},
}