@@ -29,23 +29,30 @@ func (app *App) turnData(ctx context.Context, url string, updatedAt time.Time) (
2929 hash := sha256 .Sum256 ([]byte (key ))
3030 cacheFile := filepath .Join (app .cacheDir , hex .EncodeToString (hash [:])[:16 ]+ ".json" )
3131
32- // Try to read from cache (gracefully handle all cache errors)
33- if data , readErr := os .ReadFile (cacheFile ); readErr == nil {
34- var entry cacheEntry
35- if unmarshalErr := json .Unmarshal (data , & entry ); unmarshalErr != nil {
36- log .Printf ("Failed to unmarshal cache data for %s: %v" , url , unmarshalErr )
37- // Remove corrupted cache file
38- if removeErr := os .Remove (cacheFile ); removeErr != nil {
39- log .Printf ("Failed to remove corrupted cache file: %v" , removeErr )
32+ // Skip cache if --no-cache flag is set
33+ if ! app .noCache {
34+ // Try to read from cache (gracefully handle all cache errors)
35+ if data , readErr := os .ReadFile (cacheFile ); readErr == nil {
36+ var entry cacheEntry
37+ if unmarshalErr := json .Unmarshal (data , & entry ); unmarshalErr != nil {
38+ log .Printf ("Failed to unmarshal cache data for %s: %v" , url , unmarshalErr )
39+ // Remove corrupted cache file
40+ if removeErr := os .Remove (cacheFile ); removeErr != nil {
41+ log .Printf ("Failed to remove corrupted cache file: %v" , removeErr )
42+ }
43+ } else if time .Since (entry .CachedAt ) < cacheTTL && entry .UpdatedAt .Equal (updatedAt ) {
44+ // Check if cache is still valid (2 hour TTL)
45+ return entry .Data , nil
4046 }
41- } else if time .Since (entry .CachedAt ) < cacheTTL && entry .UpdatedAt .Equal (updatedAt ) {
42- // Check if cache is still valid (2 hour TTL)
43- return entry .Data , nil
4447 }
4548 }
4649
4750 // Cache miss, fetch from API
48- log .Printf ("Cache miss for %s, fetching from Turn API" , url )
51+ if app .noCache {
52+ log .Printf ("Cache bypassed for %s (--no-cache), fetching from Turn API" , url )
53+ } else {
54+ log .Printf ("Cache miss for %s, fetching from Turn API" , url )
55+ }
4956
5057 // Just try once with timeout - if Turn API fails, it's not critical
5158 turnCtx , cancel := context .WithTimeout (ctx , 10 * time .Second )
@@ -57,20 +64,22 @@ func (app *App) turnData(ctx context.Context, url string, updatedAt time.Time) (
5764 return nil , err
5865 }
5966
60- // Save to cache (don't fail if caching fails)
61- entry := cacheEntry {
62- Data : data ,
63- CachedAt : time .Now (),
64- UpdatedAt : updatedAt ,
65- }
66- if cacheData , marshalErr := json .Marshal (entry ); marshalErr != nil {
67- log .Printf ("Failed to marshal cache data for %s: %v" , url , marshalErr )
68- } else {
69- // Ensure cache directory exists
70- if dirErr := os .MkdirAll (filepath .Dir (cacheFile ), 0o700 ); dirErr != nil {
71- log .Printf ("Failed to create cache directory: %v" , dirErr )
72- } else if writeErr := os .WriteFile (cacheFile , cacheData , 0o600 ); writeErr != nil {
73- log .Printf ("Failed to write cache file for %s: %v" , url , writeErr )
67+ // Save to cache (don't fail if caching fails) - skip if --no-cache is set
68+ if ! app .noCache {
69+ entry := cacheEntry {
70+ Data : data ,
71+ CachedAt : time .Now (),
72+ UpdatedAt : updatedAt ,
73+ }
74+ if cacheData , marshalErr := json .Marshal (entry ); marshalErr != nil {
75+ log .Printf ("Failed to marshal cache data for %s: %v" , url , marshalErr )
76+ } else {
77+ // Ensure cache directory exists
78+ if dirErr := os .MkdirAll (filepath .Dir (cacheFile ), 0o700 ); dirErr != nil {
79+ log .Printf ("Failed to create cache directory: %v" , dirErr )
80+ } else if writeErr := os .WriteFile (cacheFile , cacheData , 0o600 ); writeErr != nil {
81+ log .Printf ("Failed to write cache file for %s: %v" , url , writeErr )
82+ }
7483 }
7584 }
7685
0 commit comments