|
| 1 | +package wailsapp |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +type authFileMetadataCacheEntry struct { |
| 9 | + Name string |
| 10 | + Size int64 |
| 11 | + Modified int64 |
| 12 | + Type string |
| 13 | + Provider string |
| 14 | + Priority int |
| 15 | + Email string |
| 16 | + PlanType string |
| 17 | +} |
| 18 | + |
| 19 | +func authFileMetadataCacheKey(name string, size int64, modified int64) string { |
| 20 | + return fmt.Sprintf("%s|%d|%d", strings.TrimSpace(name), size, modified) |
| 21 | +} |
| 22 | + |
| 23 | +func (a *App) cachedAuthFileMetadata(file AuthFileItem) (authFileMetadataCacheEntry, bool) { |
| 24 | + name := strings.TrimSpace(file.Name) |
| 25 | + if name == "" { |
| 26 | + return authFileMetadataCacheEntry{}, false |
| 27 | + } |
| 28 | + key := authFileMetadataCacheKey(name, file.Size, file.Modified) |
| 29 | + a.authFileCacheMu.RLock() |
| 30 | + entry, ok := a.authFileMetadataCache[key] |
| 31 | + a.authFileCacheMu.RUnlock() |
| 32 | + if !ok { |
| 33 | + return authFileMetadataCacheEntry{}, false |
| 34 | + } |
| 35 | + return entry, true |
| 36 | +} |
| 37 | + |
| 38 | +func (a *App) storeAuthFileMetadata(file AuthFileItem) { |
| 39 | + name := strings.TrimSpace(file.Name) |
| 40 | + if name == "" { |
| 41 | + return |
| 42 | + } |
| 43 | + entry := authFileMetadataCacheEntry{ |
| 44 | + Name: name, |
| 45 | + Size: file.Size, |
| 46 | + Modified: file.Modified, |
| 47 | + Type: strings.TrimSpace(file.Type), |
| 48 | + Provider: strings.TrimSpace(file.Provider), |
| 49 | + Priority: file.Priority, |
| 50 | + Email: strings.TrimSpace(file.Email), |
| 51 | + PlanType: strings.TrimSpace(file.PlanType), |
| 52 | + } |
| 53 | + key := authFileMetadataCacheKey(name, file.Size, file.Modified) |
| 54 | + a.authFileCacheMu.Lock() |
| 55 | + if a.authFileMetadataCache == nil { |
| 56 | + a.authFileMetadataCache = map[string]authFileMetadataCacheEntry{} |
| 57 | + } |
| 58 | + a.authFileMetadataCache[key] = entry |
| 59 | + a.authFileCacheMu.Unlock() |
| 60 | +} |
| 61 | + |
| 62 | +func applyCachedAuthFileMetadata(file *AuthFileItem, entry authFileMetadataCacheEntry) { |
| 63 | + if file == nil { |
| 64 | + return |
| 65 | + } |
| 66 | + if needsAuthFileKindInference(*file) { |
| 67 | + if entry.Provider != "" { |
| 68 | + file.Provider = entry.Provider |
| 69 | + } |
| 70 | + if entry.Type != "" { |
| 71 | + file.Type = entry.Type |
| 72 | + } |
| 73 | + } |
| 74 | + if strings.TrimSpace(file.Email) == "" { |
| 75 | + file.Email = entry.Email |
| 76 | + } |
| 77 | + if strings.TrimSpace(file.PlanType) == "" { |
| 78 | + file.PlanType = entry.PlanType |
| 79 | + } |
| 80 | + if file.Priority == 0 { |
| 81 | + file.Priority = entry.Priority |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (a *App) invalidateAuthFileMetadataCache(names ...string) { |
| 86 | + a.authFileCacheMu.Lock() |
| 87 | + defer a.authFileCacheMu.Unlock() |
| 88 | + if len(a.authFileMetadataCache) == 0 { |
| 89 | + return |
| 90 | + } |
| 91 | + if len(names) == 0 { |
| 92 | + a.authFileMetadataCache = map[string]authFileMetadataCacheEntry{} |
| 93 | + return |
| 94 | + } |
| 95 | + targets := map[string]struct{}{} |
| 96 | + for _, name := range names { |
| 97 | + if trimmed := strings.TrimSpace(name); trimmed != "" { |
| 98 | + targets[trimmed] = struct{}{} |
| 99 | + } |
| 100 | + } |
| 101 | + if len(targets) == 0 { |
| 102 | + return |
| 103 | + } |
| 104 | + for key, entry := range a.authFileMetadataCache { |
| 105 | + if _, ok := targets[entry.Name]; ok { |
| 106 | + delete(a.authFileMetadataCache, key) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments