Back to Strawberry

Parser Cache

docs/extensions/parser-cache.md

0.327.01.4 KB
Original Source

ParserCache

This extension adds LRU caching to the parsing step of query execution to improve performance by caching the parsed result in memory.

Usage example:

python
import strawberry
from strawberry.extensions import ParserCache


@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello, world!"


schema = strawberry.Schema(
    Query,
    extensions=[
        ParserCache(),
    ],
)

API reference:

python
class ParserCache(maxsize=128): ...

maxsize: Optional[int] = 128

Set the maxsize of the cache. By default the cache is bounded to 128 entries, with the least recently used entries evicted first. Pass an explicit maxsize=None to let the cache grow without bound; only do this when the set of distinct query texts reaching the server is trusted and bounded, as an unbounded cache lets clients grow the server's memory indefinitely by sending unique query texts.

More info: https://docs.python.org/3/library/functools.html#functools.lru_cache

More examples:

<details> <summary>Using maxsize</summary>
python
import strawberry
from strawberry.extensions import ParserCache


@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello, world!"


schema = strawberry.Schema(
    Query,
    extensions=[
        ParserCache(maxsize=100),
    ],
)
</details>