Implementation:HKUDS AI Trader CacheManager
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Caching, Performance |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
Frontend JavaScript class that manages browser localStorage caching of pre-computed trading data for faster dashboard page loads.
Description
The CacheManager class implements a client-side caching layer that stores pre-computed agent trading data in browser localStorage. It supports cache versioning, time-based expiry (configurable, default 7 days), and performance metrics tracking. Cache can be bypassed via URL parameters (?nocache=1), localStorage overrides, or YAML configuration. It loads pre-computed JSON files from the server when available, falling back to live data computation.
Usage
Import this script before data-loader.js in the dashboard HTML. The DataLoader constructor creates a CacheManager instance and uses it to accelerate data loading.
Code Reference
Source Location
- Repository: HKUDS_AI_Trader
- File: docs/assets/js/cache-manager.js
- Lines: 1-441
Signature
class CacheManager {
constructor()
isCacheEnabled() // Check if caching is enabled (URL > localStorage > config)
async loadCachedData(market) // Load data from cache or pre-computed files
saveToCache(market, data, version) // Save data to localStorage with version tag
clearCache() // Clear all AI Trader cache entries
getCacheInfo() // Get cache metadata (size, age, version)
getPerformanceMetrics() // Get last load time and cache hit stats
}
Import
<script src="assets/js/cache-manager.js"></script>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| market | string | Yes | Market identifier ("us", "cn", "cn_hour") |
| localStorage | Web API | Yes | Browser localStorage for persistent caching |
| window.configLoader | ConfigLoader | No | Optional config for cache settings |
Outputs
| Name | Type | Description |
|---|---|---|
| cachedData | Object or null | Previously cached agent data for the market, or null on miss |
| performanceMetrics | Object | Load time, cache hit status, and method used |
Usage Examples
const cacheManager = new CacheManager();
// Check if caching is enabled
if (cacheManager.isCacheEnabled()) {
// Try loading from cache first
const cached = await cacheManager.loadCachedData("us");
if (cached) {
console.log("Cache hit! Loaded data from localStorage");
}
}
// Save computed data to cache
cacheManager.saveToCache("us", computedData, "v1.0");
// Get performance metrics
const metrics = cacheManager.getPerformanceMetrics();
console.log(`Load time: ${metrics.lastLoadTime}ms, Cache hit: ${metrics.cacheHit}`);