function get_cache_buster
Returns a cache-busting string that varies based on the application mode: current timestamp in debug mode or a static version string in production mode.
/tf/active/vicechatdev/vice_ai/complex_app.py
424 - 426
simple
Purpose
This function provides a cache-busting mechanism for static assets (CSS, JavaScript, images) in web applications. In debug mode, it returns the current Unix timestamp to ensure assets are never cached during development. In production mode, it returns a static version string (CACHE_BUST_VERSION) that only changes when the application restarts, allowing for efficient caching while still enabling cache invalidation when needed.
Source Code
def get_cache_buster():
"""Get a cache busting string - timestamp in debug, startup time in production"""
return str(int(time.time())) if DEBUG_MODE else CACHE_BUST_VERSION
Return Value
Returns a string representation of either the current Unix timestamp (integer converted to string) if DEBUG_MODE is True, or the value of CACHE_BUST_VERSION constant if DEBUG_MODE is False. The returned string is typically appended to asset URLs as a query parameter (e.g., '/static/style.css?v=1234567890').
Dependencies
time
Required Imports
import time
Usage Example
# Setup required constants
DEBUG_MODE = False
CACHE_BUST_VERSION = '1.0.0'
import time
def get_cache_buster():
return str(int(time.time())) if DEBUG_MODE else CACHE_BUST_VERSION
# Usage in template URL generation
cache_buster = get_cache_buster()
static_url = f'/static/css/style.css?v={cache_buster}'
print(static_url) # Output: /static/css/style.css?v=1.0.0
# In debug mode
DEBUG_MODE = True
cache_buster = get_cache_buster()
static_url = f'/static/js/app.js?v={cache_buster}'
print(static_url) # Output: /static/js/app.js?v=1703123456 (current timestamp)
Best Practices
- Ensure DEBUG_MODE and CACHE_BUST_VERSION are defined as global constants before calling this function
- Set CACHE_BUST_VERSION at application startup (e.g., using timestamp or version number) to ensure consistency across all requests in production
- Use this function consistently across all static asset references in templates to ensure proper cache invalidation
- In production, update CACHE_BUST_VERSION when deploying new versions to force clients to fetch updated assets
- Consider using application version numbers or git commit hashes for CACHE_BUST_VERSION for better traceability
- Avoid calling this function repeatedly for the same asset within a single request; cache the result if needed
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_file_version 76.7% similar
-
function debug_cache_info 72.2% similar
-
function get_file_version_v1 69.9% similar
-
function inject_cache_buster 66.1% similar
-
function check_debug_endpoint 58.0% similar