Skip to main content

varco-memcached

PyPI version Memcached cache backend for the varco framework.

MemcachedCache implements varco_core.cache.CacheBackend on top of aiomcache. Values are serialized (JSON by default) and stored in Memcached; TTL is enforced natively via Memcached's exptime.


Installation

uv add varco-memcached
# or: pip install varco-memcached

Requires a running Memcached server (memcached -p 11211).


Quick start

from varco_memcached import MemcachedCache, MemcachedCacheSettings

settings = MemcachedCacheSettings(host="localhost", port=11211, key_prefix="myapp:")

cache = MemcachedCache(settings)
await cache.start()

await cache.set("user:42", {"name": "Ada"}, ttl=300)
value = await cache.get("user:42")  # {"name": "Ada"}

await cache.stop()

Configuration

MemcachedCacheSettings reads from environment variables under the VARCO_MEMCACHED_CACHE_ prefix (kept separate from any Redis settings in the same process):

Field Default Env var
host "localhost" VARCO_MEMCACHED_CACHE_HOST
port 11211 VARCO_MEMCACHED_CACHE_PORT
pool_size 2 VARCO_MEMCACHED_CACHE_POOL_SIZE
key_prefix "" VARCO_MEMCACHED_CACHE_KEY_PREFIX
settings = MemcachedCacheSettings.from_env()

Key differences from varco_redis.RedisCache

  • Keys must be bytesaiomcache requires byte keys; varco_memcached encodes the prefixed key as UTF-8 internally.
  • No native key enumeration. Memcached has no SCAN-equivalent command. clear() only removes keys written by this MemcachedCache instance in the current process (tracked in an in-process registry) — keys written by other processes, or surviving a restart, are unaffected and expire via TTL. Use the server's flush_all() for a full flush (affects every client sharing that server).
  • No native exists(). It performs a get and checks for None — a full round trip, though deserialization is skipped.
  • TTL semantics differ. Memcached's exptime of 0 means "never expire" — the opposite convention from a bare Redis SET with no TTL.
  • Key length limit. Memcached rejects keys over 250 bytes or containing whitespace/control characters — MemcachedCacheSettings.memcached_key() does not validate this; callers are responsible for keeping keys short.

DI integration

Unlike scan-only event bus backends, MemcachedCache needs an async start() call to open its connection pool — wiring goes through MemcachedCacheConfiguration, a providify @Configuration:

from providify import DIContainer
from varco_core.cache import CacheBackend
from varco_memcached.di import async_bootstrap

container = await async_bootstrap()  # installs MemcachedCacheConfiguration
cache = await container.aget(CacheBackend)  # started MemcachedCache singleton
await cache.set("key", "value", ttl=60)
await container.ashutdown()  # stops the cache via @PreDestroy

Or manually:

from providify import DIContainer
from varco_memcached.cache import MemcachedCacheConfiguration

container = DIContainer()
await container.ainstall(MemcachedCacheConfiguration)
cache = await container.aget(CacheBackend)

Override settings by registering a higher-priority @Provider before ainstall() (equal-priority bindings resolve first-registered):

from providify import Provider


@Provider(singleton=True)
def memcached_cache_settings() -> MemcachedCacheSettings:
    return MemcachedCacheSettings(host=os.environ["MEMCACHED_HOST"], key_prefix="myapp:")


container.provide(memcached_cache_settings)
await container.ainstall(MemcachedCacheConfiguration)

Running tests

uv run pytest varco_memcached/tests/

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

varco_memcached-3.2.0.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

varco_memcached-3.2.0-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file varco_memcached-3.2.0.tar.gz.

File metadata

  • Download URL: varco_memcached-3.2.0.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for varco_memcached-3.2.0.tar.gz
Algorithm Hash digest
SHA256 cf45f4e9094518d14386a48cacffd354f4fd63355556c770f1bfcfc0e927c473
MD5 a3b0b3152b8d3b126a092ba2b3c9a046
BLAKE2b-256 0bdf58e9abd223eac52de43b8236675a6a7e362ffce1e617dd38e58eb9d6cfe9

See more details on using hashes here.

Provenance

The following attestation bundles were made for varco_memcached-3.2.0.tar.gz:

Publisher: release.yml on edoardoscarpaci/varco

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file varco_memcached-3.2.0-py3-none-any.whl.

File metadata

  • Download URL: varco_memcached-3.2.0-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for varco_memcached-3.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d57bc2eab7c13cc8b827232893210e21d420e0401915f41787713e856e7aec87
MD5 3ea98890b1d80fe5fa72317c466d1d19
BLAKE2b-256 40c8520d5d4aab401a78c979a1d9ccb824ac2dfd2ad70e2f6be0fce25181054e

See more details on using hashes here.

Provenance

The following attestation bundles were made for varco_memcached-3.2.0-py3-none-any.whl:

Publisher: release.yml on edoardoscarpaci/varco

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

3.2.0 This release

2 files

3.1.0

2 files

3.0.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.6

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page