Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle singleton in cache properly #10

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cache

import (
"sync"

"github.com/waynezhang/foto/internal/constants"
)

Expand All @@ -10,6 +12,11 @@ type Cache interface {
Clear()
}

var (
once sync.Once
instance Cache
)

func Shared() Cache {
once.Do(func() {
instance = NewFolderCache(constants.CacheDirectoryName)
Expand Down
9 changes: 1 addition & 8 deletions internal/cache/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cache
import (
"fmt"
"path/filepath"
"sync"

cp "github.com/otiai10/copy"
"github.com/rs/zerolog/log"
Expand All @@ -16,16 +15,10 @@ type folderCache struct {
directoryName string
}

var (
once sync.Once
instance Cache
)

func NewFolderCache(directoryName string) Cache {
instance = folderCache{
return folderCache{
directoryName: directoryName,
}
return instance
}

// `src` is used to compute checksum, `file` will be copied to the cache
Expand Down
Loading