A Redis-backed cache for BigQuery
Project description
bqredis
This library provides functionality to cache calls to bigquery with redis.
The use case which drove creation of this library was a web-based dashboard which pulled data from bigquery. Often queries were inexplicably slow. Even worse, a slow loading page would prompt users to refresh, would would saturate the available threadpools, further slowing the experience.
This is the result: a library which uses redis to cache bigquery results and prevents more than one inflight request from going out from a given cache instance at a time. Enough talking, let's cut to an example:
import bqredis
_QUERY = "SELECT * FROM `bigquery-public-data.country_codes.country_codes` ORDER BY alpha_3_code DESC LIMIT 10"
redis_client = redis.Redis.from_url("redis://localhost:6379")
cache = bqredis.BQRedis(redis_client, redis_cache_ttl_sec=300, redis_background_refresh_ttl_sec=5)
# Subsequent calls to this for the next 300 seconds will be cached.
cache.query_sync(_QUERY)
# Start a background refresh for this query.
promise = cache.submit_background_refresh(_QUERY)
promise.result()
# More calls for refreshing the background cache for the next 5 seconds will
# not start.
Return type
By default, this library will parse results as a pyarrow.RecordBatch. To
instead use a different format, override the convert_arrow_to_output method.
For example, to have results as a polars.DataFrame do the following:
import polars
import pyarrow
class BQRedisPolars(bqredis.BQRedis):
def convert_arrow_to_output_format(self, records: pyarrow.RecordBatch) -> polars.DataFrame:
return polars.from_arrow(records)
pl_cache = BQRedisPolars(redis_client, redis_cache_ttl_sec=300, redis_background_refresh_ttl_sec=5)
pl_cache.query_sync(_QUERY)
We use arrow as the underlying format because it is the direct format being sent by BigQuery. This can be converted with 0-copy to polars as show above.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bqredis-0.0.2-py3-none-any.whl.
File metadata
- Download URL: bqredis-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eaf65b2a12561c28bec9ab0e17af2d9dd0a2dbb4808e0ea52f8707323fcaa7c4
|
|
| MD5 |
b6bf99ea1adfddc238820019725b7bc9
|
|
| BLAKE2b-256 |
85f30da06755f300ff752f5684c844fd4d3359e96abf4a4057f1d81dd8873490
|
Provenance
The following attestation bundles were made for bqredis-0.0.2-py3-none-any.whl:
Publisher:
release.yml on DelfinaCare/bqredis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bqredis-0.0.2-py3-none-any.whl -
Subject digest:
eaf65b2a12561c28bec9ab0e17af2d9dd0a2dbb4808e0ea52f8707323fcaa7c4 - Sigstore transparency entry: 368947440
- Sigstore integration time:
-
Permalink:
DelfinaCare/bqredis@06f01837baa15e8b6f7d52f78a221d18b4088e02 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/DelfinaCare
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@06f01837baa15e8b6f7d52f78a221d18b4088e02 -
Trigger Event:
release
-
Statement type: