| 82 | } |
| 83 | |
| 84 | func setCache(key string, value []DirEntryResult) { |
| 85 | cacheMu.Lock() |
| 86 | defer cacheMu.Unlock() |
| 87 | // if already exists, update it |
| 88 | if entry, ok := cache[key]; ok { |
| 89 | entry.value = value |
| 90 | entry.expiration = time.Now().Add(cacheTTL) |
| 91 | cacheLRU.MoveToFront(entry.lruElement) |
| 92 | return |
| 93 | } |
| 94 | // evict if at capacity |
| 95 | if cacheLRU.Len() >= maxCacheEntries { |
| 96 | oldest := cacheLRU.Back() |
| 97 | if oldest != nil { |
| 98 | oldestKey := oldest.Value.(string) |
| 99 | if oldEntry, ok := cache[oldestKey]; ok { |
| 100 | cacheLRU.Remove(oldEntry.lruElement) |
| 101 | delete(cache, oldestKey) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | // add new entry |
| 106 | elem := cacheLRU.PushFront(key) |
| 107 | cache[key] = &cacheEntry{ |
| 108 | key: key, |
| 109 | value: value, |
| 110 | expiration: time.Now().Add(cacheTTL), |
| 111 | lruElement: elem, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // cacheDispose clears all cache entries for the provided widgetId. |
| 116 | func cacheDispose(widgetId string) { |