@@ -2,6 +2,7 @@ package cache
22
33import (
44 "context"
5+ "log/slog"
56 "sync"
67 "time"
78
@@ -10,6 +11,7 @@ import (
1011
1112type Cache struct {
1213 backend types.GitForge
14+ logger * slog.Logger
1315
1416 rootContentLock sync.RWMutex
1517 cachedRootContent map [string ]types.GroupSource
@@ -18,9 +20,10 @@ type Cache struct {
1820 cachedContent map [string ]CachedContent
1921}
2022
21- func NewForgeCache (backend types.GitForge ) types.GitForge {
23+ func NewForgeCache (backend types.GitForge , logger * slog. Logger ) types.GitForge {
2224 return & Cache {
2325 backend : backend ,
26+ logger : logger ,
2427
2528 cachedContent : map [string ]CachedContent {},
2629 }
@@ -43,6 +46,7 @@ func (c *Cache) FetchRootGroupContent(ctx context.Context) (map[string]types.Gro
4346 // check to make sure the data is still not there,
4447 // since RWMutex is not upgradeable and another thread may have grabbed the lock in the meantime
4548 if c .cachedRootContent == nil {
49+ c .logger .Info ("Fetching root content from backend" )
4650 content , err := c .backend .FetchRootGroupContent (ctx )
4751 if err != nil {
4852 return nil , err
@@ -56,10 +60,14 @@ func (c *Cache) FetchRootGroupContent(ctx context.Context) (map[string]types.Gro
5660}
5761
5862func (c * Cache ) FetchGroupContent (ctx context.Context , source types.GroupSource ) (types.GroupContent , error ) {
63+ logger := c .logger .With ("groupID" , source .GetGroupID ()).With ("groupPath" , source .GetGroupPath ())
64+
5965 c .contentLock .RLock ()
6066 if cachedContent , found := c .cachedContent [source .GetGroupPath ()]; ! found {
6167 c .contentLock .RUnlock ()
6268
69+ logger .Debug ("Cache miss" )
70+
6371 // acquire write lock
6472 c .contentLock .Lock ()
6573 defer c .contentLock .Unlock ()
@@ -70,6 +78,7 @@ func (c *Cache) FetchGroupContent(ctx context.Context, source types.GroupSource)
7078 }
7179
7280 // fetch content from backend and cache it
81+ logger .Info ("Fetching content from backend" )
7382 content , err := c .backend .FetchGroupContent (ctx , source )
7483 if err != nil {
7584 return types.GroupContent {}, err
@@ -81,6 +90,7 @@ func (c *Cache) FetchGroupContent(ctx context.Context, source types.GroupSource)
8190 return content , nil
8291 } else {
8392 c .contentLock .RUnlock ()
93+ logger .Debug ("Cache hit" )
8494 return cachedContent .GroupContent , nil
8595 }
8696}
0 commit comments