| 5 | ) |
| 6 | |
| 7 | func CreateCache[KeyType comparable, DataType any](cleanupInterval time.Duration, properties CacheProperties) *Cache[KeyType, DataType] { |
| 8 | cache := &Cache[KeyType, DataType]{ |
| 9 | Items: make(map[KeyType]CacheItem[DataType]), |
| 10 | } |
| 11 | |
| 12 | cache.Properties = properties |
| 13 | |
| 14 | if properties.SetCacheEvent { |
| 15 | cache.SetCacheEvent = make(chan KeyType, 100) |
| 16 | } |
| 17 | if properties.TimeoutCacheEvent { |
| 18 | cache.TimeoutCacheEvent = make(chan CacheEvent[KeyType, DataType], 100) |
| 19 | } |
| 20 | |
| 21 | if cleanupInterval > 0 { |
| 22 | ticker := time.NewTicker(cleanupInterval) |
| 23 | cache.Ticker = ticker |
| 24 | go cache.Loop() |
| 25 | } |
| 26 | return cache |
| 27 | } |