-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jens Alm
committed
Dec 3, 2024
1 parent
9a83ca8
commit 8d9034e
Showing
38 changed files
with
4,165 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"github.com/patrickmn/go-cache" | ||
"log/slog" | ||
) | ||
|
||
type CacheGetter func(id string) (interface{}, error) | ||
|
||
func GetItemFromCache(c *cache.Cache, key string, fn CacheGetter) (interface{}, error) { | ||
if key == "" { | ||
return nil, errors.New("key is null, can't resolve from cache or get it from source") | ||
} | ||
detail, found := c.Get(key) | ||
if !found { | ||
slog. | ||
With("key", key). | ||
Debug("Item not in cache, getting from source") | ||
var err error | ||
detail, err = fn(key) | ||
if err != nil { | ||
return nil, errors.Join(errors.New("error reading: "+key), err) | ||
} | ||
c.Set(key, detail, cache.NoExpiration) | ||
return detail, nil | ||
} | ||
slog. | ||
With("key", key). | ||
Debug("Item found in cache") | ||
return detail, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.