Skip to main content

Caching providers for Toggly feature flag management (Redis, Memcached)

Project description

Toggly Cache

Caching providers for Toggly feature flag management. This package provides Redis and Memcached snapshot providers for distributed caching of feature flag definitions.

Installation

# Install with Redis support
pip install toggly-cache[redis]

# Install with Memcached support
pip install toggly-cache[memcached]

# Install with both
pip install toggly-cache[all]

Quick Start

Redis Provider

from toggly import TogglyClient, TogglyConfig
from toggly_cache import RedisSnapshotProvider

# Create a Redis snapshot provider
provider = RedisSnapshotProvider(
    host="localhost",
    port=6379,
    db=0,
    prefix="toggly:",
    ttl=3600,  # Cache for 1 hour
)

# Use with TogglyClient
config = TogglyConfig(
    app_key="your-app-key",
    environment="production",
    snapshot_provider=provider,
)

client = TogglyClient(config)

Memcached Provider

from toggly import TogglyClient, TogglyConfig
from toggly_cache import MemcachedSnapshotProvider

# Create a Memcached snapshot provider
provider = MemcachedSnapshotProvider(
    servers=[("localhost", 11211)],
    prefix="toggly:",
    ttl=3600,  # Cache for 1 hour
)

# Use with TogglyClient
config = TogglyConfig(
    app_key="your-app-key",
    environment="production",
    snapshot_provider=provider,
)

client = TogglyClient(config)

Configuration

Redis Provider Options

Option Type Default Description
client Redis None Existing Redis client instance
host str "localhost" Redis host
port int 6379 Redis port
db int 0 Redis database number
password str None Redis password
prefix str "toggly:" Key prefix for Redis keys
ttl int None TTL in seconds (None = no expiry)

Memcached Provider Options

Option Type Default Description
client Client None Existing pymemcache client
servers list[tuple] None List of (host, port) tuples
prefix str "toggly:" Key prefix for Memcached keys
ttl int 0 TTL in seconds (0 = no expiry)

Using Existing Clients

You can pass an existing Redis or Memcached client:

# Redis
import redis
redis_client = redis.Redis(host="localhost", port=6379)
provider = RedisSnapshotProvider(client=redis_client)

# Memcached
from pymemcache.client.base import Client
memcached_client = Client(("localhost", 11211))
provider = MemcachedSnapshotProvider(client=memcached_client)

Cluster Support

Redis Cluster

from redis.cluster import RedisCluster
from toggly_cache import RedisSnapshotProvider

cluster = RedisCluster(
    host="cluster-node-1",
    port=6379,
)
provider = RedisSnapshotProvider(client=cluster)

Memcached with Multiple Servers

from toggly_cache import MemcachedSnapshotProvider

# Automatically uses HashClient for consistent hashing
provider = MemcachedSnapshotProvider(
    servers=[
        ("memcached-1", 11211),
        ("memcached-2", 11211),
        ("memcached-3", 11211),
    ],
)

How It Works

Cache providers implement the SnapshotProvider interface from the core toggly package. When the TogglyClient loads feature definitions:

  1. It first tries to load from the snapshot provider (cache)
  2. If not found, it fetches from the Toggly API
  3. After fetching, it saves the definitions to the snapshot provider

This enables:

  • Faster startup: Cached definitions are loaded immediately
  • Offline resilience: App works even if Toggly API is temporarily unavailable
  • Reduced API calls: Definitions are cached between restarts
  • Distributed caching: Multiple app instances share the same cache

Requirements

  • Python 3.8+
  • toggly>=0.1.0
  • redis>=4.0.0 (for Redis provider)
  • pymemcache>=4.0.0 (for Memcached provider)

License

MIT License - see LICENSE for details.

Links

Project details


Download files

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

Source Distribution

toggly_cache-0.1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

toggly_cache-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file toggly_cache-0.1.0.tar.gz.

File metadata

  • Download URL: toggly_cache-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for toggly_cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 36633f1bb329b80040dbebcb2df27327241f54b4ee3698dc952bf3809dabce8f
MD5 d96f22803506e97e1bf8b9fd4d1c7581
BLAKE2b-256 7d8f362b13b8aa3e48f15605f52d68e91b70565e9d6993cff35e0bd49ecf071f

See more details on using hashes here.

Provenance

The following attestation bundles were made for toggly_cache-0.1.0.tar.gz:

Publisher: sdk-python-release.yml on ops-ai/Toggly.FeatureManagement

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

File details

Details for the file toggly_cache-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: toggly_cache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for toggly_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 519dabc215ae495b30fcedc3a099dd32b670b1a54523f18f336316eefd5f961d
MD5 906c25fd194245210368ae3dfc935fb0
BLAKE2b-256 d90f5623c6276a33f311717d92e9275e3428749798a3440b354856366af7bdce

See more details on using hashes here.

Provenance

The following attestation bundles were made for toggly_cache-0.1.0-py3-none-any.whl:

Publisher: sdk-python-release.yml on ops-ai/Toggly.FeatureManagement

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page