Skip to content

Add hash field expiration support for HashModel (Redis 7.4+) #750

@abrookins

Description

@abrookins

Summary

Add support for Redis 7.4+ hash field expiration to HashModel. This allows setting TTL on individual hash fields rather than the entire key.

Redis Commands (7.4+)

  • HEXPIRE / HPEXPIRE - Set TTL on hash fields (seconds/milliseconds)
  • HEXPIREAT / HPEXPIREAT - Set expiration at Unix timestamp
  • HTTL / HPTTL - Get TTL of hash fields
  • HPERSIST - Remove expiration from hash fields

redis-py Support

These commands are available in redis-py >= 5.1.0 (PR #3218, merged June 2024).

Proposed API

Declarative (field-level default TTL)

class Session(HashModel):
    user_id: str
    token: str = Field(expire=3600)  # Default TTL of 1 hour

Runtime methods

session = Session(user_id="123", token="abc")
await session.save()

# Set/update field expiration
await session.expire_field("token", 3600)

# Get remaining TTL
ttl = await session.field_ttl("token")

# Remove expiration
await session.persist_field("token")

Per-save TTL

await session.save(field_expirations={"token": 3600})

Implementation Checklist

  • Extend FieldInfo and Field() with expire parameter
  • Add instance methods: expire_field(), field_ttl(), persist_field()
  • Modify HashModel.save() to apply field expirations
  • Add runtime check for redis-py version / Redis server version
  • Tests with Redis 7.4+
  • Documentation updates

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions