Files
zulip/zerver/lib/singleton_bmemcached.py
Anders Kaseorg c9faefd50e cache: Instantiate only one BMemcached cache backend.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-05-02 17:41:49 -07:00

20 lines
655 B
Python

import pickle
from functools import lru_cache
from typing import Any, Dict
from django_bmemcached.memcached import BMemcached
@lru_cache(None)
def _get_bmemcached(location: str, params: bytes) -> BMemcached:
return BMemcached(location, pickle.loads(params))
def SingletonBMemcached(location: str, params: Dict[str, Any]) -> BMemcached:
# Django instantiates the cache backend per-task to guard against
# thread safety issues, but BMemcached is already thread-safe and
# does its own per-thread pooling, so make sure we instantiate only
# one to avoid extra connections.
return _get_bmemcached(location, pickle.dumps(params))