Schema Registries¶
The schema registry caches index field types loaded from Redis via FT.INFO.
There are sync and async variants. Conceptual background is in
Schema-aware translation.
| Class | Description |
|---|---|
SchemaRegistry |
Sync registry. Lazy and eager loading, polling for index changes. |
AsyncSchemaRegistry |
Async registry. Coalesced concurrent loads, cancellation-safe. |
SchemaRegistry¶
SchemaRegistry
¶
Loads and caches index schemas from Redis.
Supports automatic schema refresh via Redis keyspace notifications.
Source code in sql_redis/schema.py
load_all
¶
Load schemas for all indexes on the server.
Source code in sql_redis/schema.py
get_field_type
¶
Get field type for a given index and field.
Lazily loads the index schema if not already cached. Returns None if index doesn't exist or field is unknown.
Source code in sql_redis/schema.py
get_schema
¶
Get full schema for an index, loading lazily if not cached.
On first access for a given index, issues a single FT.INFO call to Redis. Subsequent calls return the cached schema with no I/O.
Returns empty dict if index does not exist in Redis.
Source code in sql_redis/schema.py
invalidate
¶
Invalidate cached schema(s), forcing reload on next access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
str | None
|
Specific index to invalidate. If None, invalidates all. |
None
|
Source code in sql_redis/schema.py
refresh
¶
Refresh schema for a single index.
If the index no longer exists, removes it from the registry. If the index is new or changed, updates its cached schema.
Source code in sql_redis/schema.py
start_watching
¶
Start watching for index changes.
Since RediSearch doesn't emit keyspace notifications for FT commands, this uses polling via FT._LIST to detect changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_change
|
Callable[[str, str], None] | None
|
Optional callback invoked with (event_type, index_name) when an index is created, dropped, or altered. |
None
|
Source code in sql_redis/schema.py
stop_watching
¶
process_pending_events
¶
Process any pending index change events.
Since RediSearch doesn't emit keyspace notifications, this polls FT._LIST to detect new and deleted indexes. Call this periodically.
Source code in sql_redis/schema.py
AsyncSchemaRegistry¶
AsyncSchemaRegistry
¶
Async version of SchemaRegistry for use with redis.asyncio clients.
Loads and caches index schemas from Redis asynchronously.
Initialize with an async Redis client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_client
|
'async_redis.Redis'
|
An async Redis client (redis.asyncio.Redis). |
required |
Source code in sql_redis/schema.py
load_all
async
¶
Load schemas for all indexes on the server.
Uses asyncio.gather() to load all index schemas concurrently. Cancels any in-flight ensure_schema() tasks first.
Source code in sql_redis/schema.py
get_field_type
¶
Get field type for a given index and field.
Note: For async lazy loading, call ensure_schema() first. Returns None if index or field is unknown.
Source code in sql_redis/schema.py
get_schema
¶
Get full schema for an index (sync access to cache).
Returns empty dict if index is not cached. Use ensure_schema() to load lazily in async contexts.
Source code in sql_redis/schema.py
ensure_schema
async
¶
Ensure schema for an index is loaded, fetching lazily if needed.
This is the async equivalent of the sync get_schema() lazy path. On first access for a given index, issues a single FT.INFO call. Subsequent calls return the cached schema with no I/O.
Concurrent calls for the same index share a single in-flight FT.INFO task instead of issuing duplicate requests.
If the in-flight task is cancelled (e.g. by invalidate()), the current cache state is returned instead of propagating CancelledError to callers.
Returns empty dict if index does not exist in Redis.
Source code in sql_redis/schema.py
invalidate
¶
Invalidate cached schema(s), forcing reload on next access.
Also cancels any in-flight ensure_schema() tasks for the invalidated index(es) to prevent stale data from being written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
str | None
|
Specific index to invalidate. If None, invalidates all. |
None
|
Source code in sql_redis/schema.py
refresh
async
¶
Refresh schema for a single index.
Cancels any in-flight ensure_schema() task for this index first. If the index no longer exists, removes it from the registry.