API Reference
init_fast_query

def init_fast_query(prefix: str)

Initialize the fast query caching system using a specified prefix.

This function designates a specific prefix to act as a cache for computationally intensive queries, most notably dbzero.group_by. It's especially useful when your primary data is accessed in a read-only mode, as the cache prefix can remain writable.

Subsequent calls to cache-aware functions like dbzero.group_by will automatically store their results in this prefix. If a query is executed again with the same parameters, its results are retrieved from the cache instead of being recomputed, leading to faster execution.

Parameters

  • prefix str
    The name of the prefix to use for storing cached query results. This prefix must be opened in read-write ("rw") mode.

Returns

This method does not return any value.


Examples

Here's how you can set up a read-only data prefix and a separate, writable prefix for caching query results.

import dbzero as db0
from my_models import Book
 
# Initialize dbzero in a directory
db0.init(dbzero_root="/app/data")
 
# Open the main data prefix as read-only
db0.open("books_data", "r")
 
# Open another prefix for caching in read-write mode
db0.open("query_cache", "rw")
 
# Set 'query_cache' as the prefix for fast query caching
db0.init_fast_query(prefix="query_cache")
 
# The first time this query runs, it computes the result and saves it
# to the 'query_cache' prefix.
authors_book_count = db0.group_by(lambda b: b.author, db0.find(Book))
 
# Subsequent calls to the same query will be much faster as they
# read directly from the cache.
authors_book_count_fast = db0.group_by(lambda b: b.author, db0.find(Book))
 
db0.close()