Skip to content

Commit

Permalink
Merge pull request #34 from damongolding/api-cache
Browse files Browse the repository at this point in the history
Person and Album Api endpoint caching
  • Loading branch information
damongolding authored Aug 12, 2024
2 parents e7d3685 + 7aaedcb commit e503ba4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ kiosk:
| **yaml** | **ENV** | **Value** | **Description** |
|-------------------|-------------------------|----------------------------|--------------------------------------------------------------------------------------------|
| password | KIOSK_PASSWORD | string | If set, requests MUST contain the password in the GET parameters e.g. `http://192.168.0.123:3000?password=PASSWORD`. |
| cache | KIOSK_CACHE | bool | Cache selective Immich api calls to reduce unnecessary calls. Default is true. |


------
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
48 changes: 45 additions & 3 deletions immich/immich.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (
"math/rand/v2"

"github.com/charmbracelet/log"
"github.com/patrickmn/go-cache"

"github.com/damongolding/immich-kiosk/config"
)

// maxRetries the maximum amount of retries to find a IMAGE type
const maxRetries int = 10

var (
// baseConfig the base config i.e config.yaml or ENV
baseConfig config.Config
// apiCache cache store for immich api call(s)
apiCache *cache.Cache
)

type ImmichError struct {
Expand Down Expand Up @@ -100,12 +105,47 @@ type ImmichAlbum struct {
Assets []ImmichAsset `json:"assets"`
}

func init() {
// Setting up Immich api cache
apiCache = cache.New(5*time.Minute, 10*time.Minute)
}

// NewImage returns a new image instance
func NewImage(base config.Config) ImmichAsset {
baseConfig = base
return ImmichAsset{}
}

type ImmichApiCall func(string) ([]byte, error)

// immichApiCallDecorator Decorator to impliment cache for the immichApiCall func
func immichApiCallDecorator(immichApiCall ImmichApiCall, requestId string) ImmichApiCall {
return func(apiUrl string) ([]byte, error) {

if baseConfig.Kiosk.Cache {
apiData, found := apiCache.Get(apiUrl)
if found {
log.Debug(requestId+" Cache hit", "url", apiUrl)
return apiData.([]byte), nil
}

log.Debug(requestId+" Cache miss", "url", apiUrl)
body, err := immichApiCall(apiUrl)
if err != nil {
log.Error(err)
return nil, err
}

apiCache.Set(apiUrl, body, cache.DefaultExpiration)
log.Debug(requestId+" Cache saved", "url", apiUrl)
return body, nil

}

return immichApiCall(apiUrl)
}
}

// immichApiCall bootstrap for immich api call
func (i *ImmichAsset) immichApiCall(apiUrl string) ([]byte, error) {

Expand Down Expand Up @@ -215,7 +255,8 @@ func (i *ImmichAsset) GetRandomImageOfPerson(personId, requestId string) error {
Path: "api/people/" + personId + "/assets",
}

body, err := i.immichApiCall(apiUrl.String())
immichApiCal := immichApiCallDecorator(i.immichApiCall, requestId)
body, err := immichApiCal(apiUrl.String())
if err != nil {
log.Error(err)
return err
Expand Down Expand Up @@ -260,7 +301,7 @@ func (i *ImmichAsset) GetRandomImageOfPerson(personId, requestId string) error {
if log.GetLevel() == log.DebugLevel {
for _, per := range i.People {
if per.ID == personId {
log.Debug(requestId+" Got image of", "perople", per.Name)
log.Debug(requestId+" Got image of", "person", per.Name)
break
}
}
Expand All @@ -284,7 +325,8 @@ func (i *ImmichAsset) GetRandomImageFromAlbum(albumId, requestId string) error {
Path: "api/albums/" + albumId,
}

body, err := i.immichApiCall(apiUrl.String())
immichApiCall := immichApiCallDecorator(i.immichApiCall, requestId)
body, err := immichApiCall(apiUrl.String())
if err != nil {
log.Error(err)
return err
Expand Down

0 comments on commit e503ba4

Please sign in to comment.