CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-google-cloud-ndb-redis

Type stubs for Google Cloud NDB Redis caching functionality - provides Redis cache implementation for NDB operations

Overview
Eval results
Files

redis-cache-operations.mddocs/

Redis Cache Operations

Redis-backed global cache implementation for Google Cloud NDB operations. Provides efficient caching with Redis as the backend storage for NDB entities and query results.

Capabilities

Cache Initialization

Create and configure Redis cache instances for NDB global caching.

class RedisCache(GlobalCache):
    def __init__(self, redis, strict_read: bool = False, strict_write: bool = False) -> None:
        """
        Initialize Redis cache with a Redis client instance.
        
        Parameters:
        - redis: Redis client instance (from redis-py library)
        - strict_read: If True, raises exceptions on read errors instead of treating as cache miss
        - strict_write: If True, raises exceptions on write errors instead of ignoring
        """

    @classmethod
    def from_environment(cls, strict_read: bool = False, strict_write: bool = False) -> Self:
        """
        Create Redis cache instance using environment variables for configuration.
        
        Parameters:
        - strict_read: If True, raises exceptions on read errors
        - strict_write: If True, raises exceptions on write errors
        
        Returns:
        RedisCache instance configured from environment
        """

Cache Operations

Core caching operations for storing, retrieving, and managing cached data.

def get(self, keys):
    """
    Retrieve cached values for the given keys.
    
    Parameters:
    - keys: List of cache keys to retrieve
    
    Returns:
    Dictionary mapping keys to cached values, or None for missing keys
    """

def set(self, items, expires: Incomplete | None = None) -> None:
    """
    Store items in the cache with optional expiration.
    
    Parameters:
    - items: Dictionary of key-value pairs to cache
    - expires: Optional expiration time for cached items
    """

def delete(self, keys) -> None:
    """
    Remove items from the cache.
    
    Parameters:
    - keys: List of cache keys to delete
    """

def clear(self) -> None:
    """
    Clear all items from the cache.
    """

Watch and Compare-and-Swap Operations

Advanced cache operations for optimistic concurrency control.

def watch(self, items) -> None:
    """
    Watch cache keys for changes to support compare-and-swap operations.
    
    Parameters:
    - items: Dictionary of keys and expected values to watch
    """

def unwatch(self, keys) -> None:
    """
    Stop watching the specified cache keys.
    
    Parameters:
    - keys: List of keys to stop watching
    """

def compare_and_swap(self, items, expires: Incomplete | None = None):
    """
    Atomically update cache items only if they haven't changed since being watched.
    
    Parameters:
    - items: Dictionary of key-value pairs to update
    - expires: Optional expiration time for updated items
    
    Returns:
    Success status of the compare-and-swap operation
    """

Pipeline Support

Access to Redis pipeline functionality for batch operations.

@property
def pipes(self):
    """
    Access to Redis pipeline objects for efficient batch operations.
    
    Returns:
    Redis pipeline objects for batching commands
    """

Usage Examples

Basic Redis Cache Setup

from google.cloud.ndb import RedisCache
import redis

# Create Redis client
redis_client = redis.Redis(
    host='localhost',
    port=6379,
    db=0,
    decode_responses=True
)

# Create Redis cache with strict error handling
cache = RedisCache(
    redis_client,
    strict_read=True,
    strict_write=True
)

Using with NDB Context

from google.cloud import ndb
from google.cloud.ndb import RedisCache
import redis

# Setup Redis cache
redis_client = redis.Redis(host='localhost', port=6379, db=0)
cache = RedisCache(redis_client)

# Use with NDB client
client = ndb.Client()
with client.context(global_cache=cache):
    # All NDB operations will use Redis caching
    class MyModel(ndb.Model):
        name = ndb.StringProperty()
    
    # These operations will be cached in Redis
    entity = MyModel(name="example")
    key = entity.put()
    retrieved = key.get()

Environment-based Configuration

from google.cloud.ndb import RedisCache

# Create Redis cache from environment variables
# Expects REDIS_HOST, REDIS_PORT, etc. to be configured
cache = RedisCache.from_environment(
    strict_read=False,
    strict_write=True
)

Install with Tessl CLI

npx tessl i tessl/pypi-google-cloud-ndb-redis

docs

index.md

redis-cache-operations.md

tile.json