🔍 Code Extractor

function get_cache_buster

Maturity: 33

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.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
424 - 426
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_file_version 76.7% similar

    Generates a version string for static files to enable cache busting, using current time in debug mode or file modification time in production.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function debug_cache_info 72.2% similar

    Flask debug endpoint that provides comprehensive cache busting information including static file versions, modification times, and cache buster values.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function get_file_version_v1 69.9% similar

    Generates a version string for static files based on their modification time, used for cache busting in web applications.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function inject_cache_buster 66.1% similar

    Flask context processor that injects cache busting variables into all Jinja2 templates to prevent browser caching of static assets.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function check_debug_endpoint 58.0% similar

    Queries a debug endpoint to retrieve and display cache information from a web server, including debug mode status, cache buster values, and static file versions.

    From: /tf/active/vicechatdev/vice_ai/dev_tools.py
← Back to Browse