Coverage for src / lexigram / contracts / infra / state.py: 0%
13 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
1"""State storage protocols for Lexigram Framework."""
3from __future__ import annotations
5from typing import Any, Protocol, runtime_checkable
8@runtime_checkable
9class StateStoreProtocol(Protocol):
10 """Persistent key-value store for arbitrary application state.
12 Supports bulk read/write operations on top of the basic get/set/delete
13 primitives. Implementations may use any persistent backend (SQL, Redis,
14 blob storage) that satisfies the interface.
16 Typical usage::
18 state = await container.resolve(StateStoreProtocol)
19 await state.set("session:abc123", {"user_id": "u-1"}, ttl=3600)
20 data = await state.get("session:abc123")
21 """
23 async def get(self, key: str) -> Any | None:
24 """Get a value by key.
26 Args:
27 key: The key to retrieve.
29 Returns:
30 The value if found, None otherwise.
31 """
32 ...
34 async def set(self, key: str, value: Any, ttl: int | None = None) -> None:
35 """Set a value with optional TTL.
37 Args:
38 key: The key to set.
39 value: The value to store.
40 ttl: Optional time-to-live in seconds.
41 """
42 ...
44 async def delete(self, key: str) -> bool:
45 """Delete a key.
47 Args:
48 key: The key to delete.
50 Returns:
51 True if deleted, False if not found.
52 """
53 ...
55 async def exists(self, key: str) -> bool:
56 """Check if a key exists.
58 Args:
59 key: The key to check.
61 Returns:
62 True if exists, False otherwise.
63 """
64 ...
66 async def expire(self, key: str, ttl: int) -> bool:
67 """Set expiration on a key.
69 Args:
70 key: The key to expire.
71 ttl: Time-to-live in seconds.
73 Returns:
74 True if timeout was set, False if key doesn't exist.
75 """
76 ...
78 async def ttl(self, key: str) -> int:
79 """Get remaining TTL for a key.
81 Args:
82 key: The key to check.
84 Returns:
85 TTL in seconds, -1 if no expiry, -2 if key doesn't exist.
86 """
87 ...
89 async def get_many(self, keys: list[str]) -> dict[str, Any]:
90 """Return a mapping of *keys* → values for all keys that exist.
92 Args:
93 keys: List of storage keys to fetch.
95 Returns:
96 Dict containing only keys that were found; absent keys are omitted.
97 """
98 ...
100 async def set_many(self, items: dict[str, Any], ttl: int | None = None) -> None:
101 """Persist multiple key-value pairs in a single operation.
103 Args:
104 items: Mapping of storage keys to JSON-serializable values.
105 ttl: Optional time-to-live in seconds applied to all entries.
106 """
107 ...
110__all__ = ["StateStoreProtocol"]