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.0.1.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.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: toggly_cache-0.0.1.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.0.1.tar.gz
Algorithm Hash digest
SHA256 5749604d7fc8986428c09b8f7fb234a7fb57a445211d1bdd84c965a7f8e4986c
MD5 89e776e04581ea548ce564e9f740621c
BLAKE2b-256 0d41a04999424bc350898df851082de8aca07c60236c7e96bd79eee56bd9fa7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for toggly_cache-0.0.1.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.0.1-py3-none-any.whl.

File metadata

  • Download URL: toggly_cache-0.0.1-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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5888aedf820f1e33a75b29205f4aa75cb2720a469a693b34ee3babf64f843b91
MD5 1b0085e356451bcb074ab41df2014dba
BLAKE2b-256 e837a86f32351a2bc1c15ec0106fb315ffd951e7dcf037841386c0eb15b7a134

See more details on using hashes here.

Provenance

The following attestation bundles were made for toggly_cache-0.0.1-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