typerdrive Cache modules
typerdrive.cache.attach
Provide a decorator that attaches the typerdrive cache to a typer command function.
Attributes
Classes
Functions
attach_cache
Attach the typerdrive cache to the decorated typer command function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If set, show the cache after the function runs. |
False
|
Source code in src/typerdrive/cache/attach.py
get_cache_manager
Retrieve the CacheManager from the TyperdriveContext.
Source code in src/typerdrive/cache/attach.py
typerdrive.cache.commands
Provide commands that can be added to a typer app to interact with the cache.
Classes
Functions
add_cache_subcommand
Add all cache subcommands to the given app.
Source code in src/typerdrive/cache/commands.py
add_clear
add_show
clear
clear(
ctx: Context,
group: Annotated[
str | None,
Option(
help="Clear only entries with this group. If not provided, clear entire cache"
),
] = None,
)
Remove multiple entries from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
Annotated[str | None, Option(help='Clear only entries with this group. If not provided, clear entire cache')]
|
If provided, only remove entries with this group. Otherwise, clear entire cache. |
None
|
Source code in src/typerdrive/cache/commands.py
show
show(
ctx: Context,
group: Annotated[
str | None, Option(help="Filter entries by group")
] = None,
stats: Annotated[
bool,
Option(
help="Show cache statistics instead of entries"
),
] = False,
)
Show cache contents or statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
Annotated[str | None, Option(help='Filter entries by group')]
|
Optional group to filter entries |
None
|
stats
|
Annotated[bool, Option(help='Show cache statistics instead of entries')]
|
If True, show statistics instead of entries |
False
|
Source code in src/typerdrive/cache/commands.py
typerdrive.cache.exceptions
Provide exceptions specific to the cache feature of typerdrive.
Classes
CacheClearError
Bases: CacheError
Indicate that there was a problem clearing data from the cache.
Source code in src/typerdrive/cache/exceptions.py
CacheError
Bases: TyperdriveError
The base exception for cache errors.
Source code in src/typerdrive/cache/exceptions.py
CacheInitError
Bases: CacheError
Indicate that there was a problem initializing the cache.
Source code in src/typerdrive/cache/exceptions.py
CacheLoadError
Bases: CacheError
Indicate that there was a problem loading data from the cache.
Source code in src/typerdrive/cache/exceptions.py
CacheStoreError
Bases: CacheError
Indicate that there was a problem storing data in the cache.
Source code in src/typerdrive/cache/exceptions.py
typerdrive.cache.manager
Provide a class for managing the typerdrive cache feature using diskcache.
Classes
CacheManager
Manage the typerdrive cache feature using diskcache library.
This provides a traditional key-value cache with TTL support, eviction policies, and efficient disk-based storage.
Source code in src/typerdrive/cache/manager.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
Attributes
cache
instance-attribute
cache: TypedCache = TypedCache(
directory=str(cache_dir),
size_limit=size_limit,
eviction_policy=value,
)
The underlying TypedCache instance (wrapper around diskcache.Cache).
cache_dir
instance-attribute
The directory where the cache database is stored.
Functions
__init__
__init__(
size_limit: int = 2**30,
eviction_policy: EvictionPolicy = EvictionPolicy.LEAST_RECENTLY_USED,
)
Initialize the cache manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_limit
|
int
|
Maximum size of the cache in bytes (default: 1GB) |
2 ** 30
|
eviction_policy
|
EvictionPolicy
|
Eviction policy to use when size limit is reached |
LEAST_RECENTLY_USED
|
Source code in src/typerdrive/cache/manager.py
clear
Remove items from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str | None
|
If provided, only remove entries with this group. Otherwise, remove all items. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of items removed |
Source code in src/typerdrive/cache/manager.py
delete
Delete a key from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key to delete |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the key was found and deleted |
Source code in src/typerdrive/cache/manager.py
get
Get a value from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
default
|
Any
|
Value to return if key is not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The cached value or default if not found |
Source code in src/typerdrive/cache/manager.py
get_group
Get the group associated with a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The group associated with the key, or None if no group or key doesn't exist |
Source code in src/typerdrive/cache/manager.py
get_ttl
Get the time-to-live for a cache key in human-readable format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable TTL string (e.g., "2 hours", "never"), or "expired" if the key doesn't exist |
Source code in src/typerdrive/cache/manager.py
keys
Get keys in the cache, optionally filtered by pattern and/or group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str | None
|
Optional regex pattern to filter keys |
None
|
group
|
str | None
|
Optional group to filter by |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of matching cache keys |
Source code in src/typerdrive/cache/manager.py
set
Set a value in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
value
|
Any
|
The value to store (must be picklable) |
required |
expire
|
timedelta | None
|
Time until the key expires (None for no expiration) |
None
|
group
|
str | None
|
Optional group for organizing related cache entries |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the value was successfully set |
Source code in src/typerdrive/cache/manager.py
setdefault
setdefault(
key: str,
default: Any = None,
expire: timedelta | None = None,
group: str | None = None,
) -> Any
Get a value from the cache, setting it to default if not found.
This mimics dict.setdefault() behavior: if the key exists in the cache, return its value. Otherwise, set the key to the default value and return it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
default
|
Any
|
Value to set and return if key is not found |
None
|
expire
|
timedelta | None
|
Time until the key expires (None for no expiration) |
None
|
group
|
str | None
|
Optional group for organizing related cache entries |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The cached value if it exists, otherwise the default value (which is also stored) |
Source code in src/typerdrive/cache/manager.py
show
show(
pattern: str | None = None,
group: str | None = None,
show_stats: bool = False,
include_stats: bool = True,
) -> None
Display cache contents or statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str | None
|
Optional regex pattern to filter keys |
None
|
group
|
str | None
|
Optional group to filter entries |
None
|
show_stats
|
bool
|
If True, show statistics instead of entries |
False
|
include_stats
|
bool
|
If True and show_stats is False, include stats summary at bottom of entries |
True
|
Source code in src/typerdrive/cache/manager.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
stats
Get cache statistics.
Returns:
| Type | Description |
|---|---|
CacheStats
|
CacheStats object with cache statistics |