| package executor |
|
|
| import ( |
| "sync" |
| "time" |
| ) |
|
|
| type codexCache struct { |
| ID string |
| Expire time.Time |
| } |
|
|
| |
| |
| var ( |
| codexCacheMap = make(map[string]codexCache) |
| codexCacheMu sync.RWMutex |
| ) |
|
|
| |
| const codexCacheCleanupInterval = 15 * time.Minute |
|
|
| |
| var codexCacheCleanupOnce sync.Once |
|
|
| |
| |
| func startCodexCacheCleanup() { |
| go func() { |
| ticker := time.NewTicker(codexCacheCleanupInterval) |
| defer ticker.Stop() |
| for range ticker.C { |
| purgeExpiredCodexCache() |
| } |
| }() |
| } |
|
|
| |
| func purgeExpiredCodexCache() { |
| now := time.Now() |
| codexCacheMu.Lock() |
| defer codexCacheMu.Unlock() |
| for key, cache := range codexCacheMap { |
| if cache.Expire.Before(now) { |
| delete(codexCacheMap, key) |
| } |
| } |
| } |
|
|
| |
| func getCodexCache(key string) (codexCache, bool) { |
| codexCacheCleanupOnce.Do(startCodexCacheCleanup) |
| codexCacheMu.RLock() |
| cache, ok := codexCacheMap[key] |
| codexCacheMu.RUnlock() |
| if !ok || cache.Expire.Before(time.Now()) { |
| return codexCache{}, false |
| } |
| return cache, true |
| } |
|
|
| |
| func setCodexCache(key string, cache codexCache) { |
| codexCacheCleanupOnce.Do(startCodexCacheCleanup) |
| codexCacheMu.Lock() |
| codexCacheMap[key] = cache |
| codexCacheMu.Unlock() |
| } |
|
|