Skip to content

Commit c6dc165

Browse files
committed
Caching: Optionally configure TTL using environment variable CACHE_TTL
1 parent ac70cec commit c6dc165

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ in progress
77
===========
88
- Explore dashboards: Ignore ``-- Mixed --`` data sources
99
- Caching: Increase default cache TTL to five minutes again
10+
- Caching: Optionally configure TTL using environment variable ``CACHE_TTL``
1011

1112
2023-03-05 0.14.1
1213
=================

grafana_wtf/commands.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ def run():
105105
export GRAFANA_TOKEN=eyJrIjoiWHg...dGJpZCI6MX0=
106106
grafana-wtf find luftdaten
107107
108-
# Use infinite cache expiration time, essentially caching forever.
109-
grafana-wtf find '#299c46' --cache-ttl=inf
110-
111-
# Set cache expiration time to zero, essentially disabling the cache.
112-
grafana-wtf find geohash --cache-ttl=0
113-
114108
# Search keyword within list of specific dashboards.
115109
grafana-wtf --select-dashboard=_JJ22OZZk,5iGTqNWZk find grafana-worldmap
116110
@@ -139,6 +133,18 @@ def run():
139133
# Output full history table in Markdown format
140134
grafana-wtf log --format=tabular:pipe
141135
136+
Cache control:
137+
138+
# Use infinite cache expiration time, essentially caching forever.
139+
grafana-wtf find '#299c46' --cache-ttl=inf
140+
141+
# Set cache expiration time to zero, essentially disabling the cache.
142+
grafana-wtf find geohash --cache-ttl=0
143+
144+
# Setting `--cache-ttl` per environment variable `CACHE_TTL` is also possible
145+
export CACHE_TTL=infinite
146+
grafana-wtf find geohash
147+
142148
"""
143149

144150
# Parse command line arguments
@@ -159,11 +165,17 @@ def run():
159165
grafana_url = options["grafana-url"] or os.getenv("GRAFANA_URL")
160166
grafana_token = options["grafana-token"] or os.getenv("GRAFANA_TOKEN")
161167

168+
# Read cache expiration time setting, environment variable takes precedence.
169+
if "CACHE_TTL" in os.environ:
170+
cache_ttl = os.getenv("CACHE_TTL")
171+
else:
172+
cache_ttl = options["cache-ttl"]
173+
162174
# Compute cache expiration time.
163175
try:
164-
cache_ttl = int(options["cache-ttl"])
165-
except:
166-
if not options["cache-ttl"] or "infinite".startswith(options["cache-ttl"].lower()):
176+
cache_ttl = int(cache_ttl)
177+
except ValueError:
178+
if not cache_ttl or "infinite".startswith(cache_ttl.lower()):
167179
cache_ttl = None
168180
else:
169181
raise

grafana_wtf/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ def __init__(self, grafana_url, grafana_token):
5050

5151
def enable_cache(self, expire_after=60, drop_cache=False):
5252
if expire_after is None:
53-
log.info(f"Configure response cache to never expire (infinite caching)")
53+
log.info(f"Response cache will never expire (infinite caching)")
54+
elif expire_after == 0:
55+
log.info(f"Response cache will expire immediately (expire_after=0)")
5456
else:
55-
log.info(f"Configure response cache to expire after {expire_after} seconds")
57+
log.info(f"Response cache will expire after {expire_after} seconds")
5658
requests_cache.install_cache(expire_after=expire_after, use_cache_dir=True)
5759
cache_database_file = requests_cache.get_cache().db_path
5860
log.info(f"Response cache database location is {cache_database_file}")

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
# Make sure development or production settings don't leak into the test suite.
2424
def clean_environment():
25-
for envvar in ["GRAFANA_URL", "GRAFANA_TOKEN"]:
25+
for envvar in ["GRAFANA_URL", "GRAFANA_TOKEN", "CACHE_TTL"]:
2626
try:
2727
del os.environ[envvar]
2828
except KeyError:

0 commit comments

Comments
 (0)