|
1 | 1 | package wailsapp |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "strings" |
6 | 5 | ) |
7 | 6 |
|
8 | | -type authFileMetadataCacheEntry struct { |
9 | | - Name string |
| 7 | +type authFileMetadataFingerprint struct { |
10 | 8 | Size int64 |
11 | 9 | Modified int64 |
12 | | - Type string |
13 | | - Provider string |
14 | | - Priority int |
15 | | - Email string |
16 | | - PlanType string |
17 | 10 | } |
18 | 11 |
|
19 | | -func authFileMetadataCacheKey(name string, size int64, modified int64) string { |
20 | | - return fmt.Sprintf("%s|%d|%d", strings.TrimSpace(name), size, modified) |
| 12 | +type authFileMetadataCacheEntry struct { |
| 13 | + Name string |
| 14 | + Fingerprint authFileMetadataFingerprint |
| 15 | + Type string |
| 16 | + Provider string |
| 17 | + Priority int |
| 18 | + Email string |
| 19 | + PlanType string |
| 20 | +} |
| 21 | + |
| 22 | +func authFileMetadataCacheName(name string) string { |
| 23 | + return strings.TrimSpace(name) |
| 24 | +} |
| 25 | + |
| 26 | +func authFileMetadataFingerprintFor(file AuthFileItem) authFileMetadataFingerprint { |
| 27 | + return authFileMetadataFingerprint{ |
| 28 | + Size: file.Size, |
| 29 | + Modified: file.Modified, |
| 30 | + } |
21 | 31 | } |
22 | 32 |
|
23 | 33 | func (a *App) cachedAuthFileMetadata(file AuthFileItem) (authFileMetadataCacheEntry, bool) { |
24 | | - name := strings.TrimSpace(file.Name) |
| 34 | + name := authFileMetadataCacheName(file.Name) |
25 | 35 | if name == "" { |
26 | 36 | return authFileMetadataCacheEntry{}, false |
27 | 37 | } |
28 | | - key := authFileMetadataCacheKey(name, file.Size, file.Modified) |
| 38 | + fingerprint := authFileMetadataFingerprintFor(file) |
29 | 39 | a.authFileCacheMu.RLock() |
30 | | - entry, ok := a.authFileMetadataCache[key] |
| 40 | + entry, ok := a.authFileMetadataCache[name] |
31 | 41 | a.authFileCacheMu.RUnlock() |
32 | | - if !ok { |
| 42 | + if !ok || entry.Fingerprint != fingerprint { |
33 | 43 | return authFileMetadataCacheEntry{}, false |
34 | 44 | } |
35 | 45 | return entry, true |
36 | 46 | } |
37 | 47 |
|
38 | 48 | func (a *App) storeAuthFileMetadata(file AuthFileItem) { |
39 | | - name := strings.TrimSpace(file.Name) |
| 49 | + name := authFileMetadataCacheName(file.Name) |
40 | 50 | if name == "" { |
41 | 51 | return |
42 | 52 | } |
43 | 53 | 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), |
| 54 | + Name: name, |
| 55 | + Fingerprint: authFileMetadataFingerprintFor(file), |
| 56 | + Type: strings.TrimSpace(file.Type), |
| 57 | + Provider: strings.TrimSpace(file.Provider), |
| 58 | + Priority: file.Priority, |
| 59 | + Email: strings.TrimSpace(file.Email), |
| 60 | + PlanType: strings.TrimSpace(file.PlanType), |
52 | 61 | } |
53 | | - key := authFileMetadataCacheKey(name, file.Size, file.Modified) |
54 | 62 | a.authFileCacheMu.Lock() |
55 | 63 | if a.authFileMetadataCache == nil { |
56 | 64 | a.authFileMetadataCache = map[string]authFileMetadataCacheEntry{} |
57 | 65 | } |
58 | | - a.authFileMetadataCache[key] = entry |
| 66 | + a.authFileMetadataCache[name] = entry |
59 | 67 | a.authFileCacheMu.Unlock() |
60 | 68 | } |
61 | 69 |
|
@@ -94,16 +102,16 @@ func (a *App) invalidateAuthFileMetadataCache(names ...string) { |
94 | 102 | } |
95 | 103 | targets := map[string]struct{}{} |
96 | 104 | for _, name := range names { |
97 | | - if trimmed := strings.TrimSpace(name); trimmed != "" { |
| 105 | + if trimmed := authFileMetadataCacheName(name); trimmed != "" { |
98 | 106 | targets[trimmed] = struct{}{} |
99 | 107 | } |
100 | 108 | } |
101 | 109 | if len(targets) == 0 { |
102 | 110 | return |
103 | 111 | } |
104 | | - for key, entry := range a.authFileMetadataCache { |
105 | | - if _, ok := targets[entry.Name]; ok { |
106 | | - delete(a.authFileMetadataCache, key) |
| 112 | + for name := range targets { |
| 113 | + if _, ok := a.authFileMetadataCache[name]; ok { |
| 114 | + delete(a.authFileMetadataCache, name) |
107 | 115 | } |
108 | 116 | } |
109 | 117 | } |
0 commit comments