Skip to content

Commit

Permalink
add stop
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Mar 17, 2024
1 parent 57fee54 commit ea898c7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
36 changes: 25 additions & 11 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func (i item[V]) isExpired() bool {
// TTLCache is a generic cache implementation with support for time-to-live
// (TTL) expiration.
type TTLCache[K comparable, V any] struct {
items map[K]item[V] // The map storing cache items.
mu sync.Mutex // Mutex for controlling concurrent access to the cache.
items map[K]item[V] // The map storing cache items.
mu sync.Mutex // Mutex for controlling concurrent access to the cache.
stop chan interface{} // Channel to stop the goroutine that removes expired items.
}

// NewTTL creates a new TTLCache instance and starts a goroutine to periodically
Expand All @@ -31,23 +32,36 @@ func NewTTL[K comparable, V any]() *TTLCache[K, V] {
}

go func() {
for range time.Tick(5 * time.Second) {
c.mu.Lock()

// Iterate over the cache items and delete expired ones.
for key, item := range c.items {
if item.isExpired() {
delete(c.items, key)
// Create a new ticker to remove expired items every 5 seconds.
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for {
select {
case <-c.stop:
return
case <-ticker.C:
c.mu.Lock()

// Iterate over the cache items and delete expired ones.
for key, item := range c.items {
if item.isExpired() {
delete(c.items, key)
}
}
}

c.mu.Unlock()
c.mu.Unlock()
}
}
}()

return c
}

func (c *TTLCache[K, V]) Stop() {
close(c.stop)
}

// Set adds a new item to the cache with the specified key, value, and
// time-to-live (TTL).
func (c *TTLCache[K, V]) Set(key K, value V, ttl time.Duration) {
Expand Down
5 changes: 4 additions & 1 deletion internal/config/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ func GetDatabaseConfigFromConfigFile(cf *database.ConfigFile) (res *database.Con
ch := cache.New(cf.CacheDuration)

return &database.Config{
Disconnect: c.Prisma.Disconnect,
Disconnect: func() error {
ch.Stop()
return c.Prisma.Disconnect()
},
Repository: prisma.NewPrismaRepository(c, pool, prisma.WithLogger(&l), prisma.WithCache(ch)),
Seed: cf.Seed,
}, nil
Expand Down
7 changes: 7 additions & 0 deletions internal/repository/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type Cacheable interface {

// Get gets a value from the cache with the given key
Get(key string) (interface{}, bool)

// Stop stops the cache and clears any goroutines
Stop()
}

type Cache struct {
Expand All @@ -27,6 +30,10 @@ func (c *Cache) Get(key string) (interface{}, bool) {
return c.cache.Get(key)
}

func (c *Cache) Stop() {
c.cache.Stop()
}

func New(duration time.Duration) *Cache {
if duration == 0 {
// consider a duration of 0 a very short expiry instead of no expiry
Expand Down

0 comments on commit ea898c7

Please sign in to comment.