Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
1. Bump minimal version Go
Browse files Browse the repository at this point in the history
2. Fixed #5
3. Refactoring cache
  • Loading branch information
BorzdeG committed Aug 15, 2024
1 parent 755ffd4 commit c13dd8e
Show file tree
Hide file tree
Showing 27 changed files with 561 additions and 334 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

include::changelog/dev-version.adoc[]

include::changelog/v2024.08-beta-06.adoc[]

include::changelog/v2024.08-beta-05.adoc[]

include::changelog/v2024.08-beta-04.adoc[]
Expand Down
2 changes: 1 addition & 1 deletion changelog/v2024.08-beta-04.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
:icons: font

[no-bullet]
- icon:wrench[] Fixed #1
- icon:wrench[] Fixed link:https://github.com/dev-itbasis-sdkm/sdkm/issues/1[#1]
6 changes: 6 additions & 0 deletions changelog/v2024.08-beta-06.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= v2024.08-beta-06
:icons: font

[no-bullet]
- icon:wrench[] Fixed link:https://github.com/dev-itbasis-sdkm/sdkm/issues/5[#5]
- icon:wrench[] refactoring cache
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dev.itbasis.sdkm

go 1.22.5
go 1.23.0

require (
github.com/dusted-go/logging v1.3.0
Expand All @@ -11,6 +11,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
go.uber.org/mock v0.4.0
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
golang.org/x/mod v0.20.0
golift.io/xtractr v0.2.2
)
Expand All @@ -36,7 +37,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
Expand Down
37 changes: 0 additions & 37 deletions internal/cache/cache-file-updated.go

This file was deleted.

117 changes: 0 additions & 117 deletions internal/cache/cache-file.go

This file was deleted.

64 changes: 46 additions & 18 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,77 @@ package cache

import (
"context"
"fmt"
"sync"

sdkmSDKVersion "github.com/dev.itbasis.sdkm/pkg/sdk-version"
"golang.org/x/exp/maps"
)

type sdkVersions struct {
filePath string
type cache struct {
storeLock sync.Mutex

sdkVersions sync.Map
cacheStorage sdkmSDKVersion.CacheStorage
cache map[sdkmSDKVersion.VersionType][]sdkmSDKVersion.SDKVersion
}

func NewCacheSDKVersions() sdkmSDKVersion.SDKVersionsCache {
return &sdkVersions{}
func NewCache() sdkmSDKVersion.Cache {
return &cache{
cache: map[sdkmSDKVersion.VersionType][]sdkmSDKVersion.SDKVersion{},
}
}

func (receiver *sdkVersions) String() string {
return fmt.Sprintf("SDKVersionCache [file=%s]", receiver.filePath)
func (receiver *cache) String() string {
var result = "SDKVersionCache"

if receiver.cacheStorage != nil {
result = result + " (" + receiver.cacheStorage.String() + ")"
}

return result
}

func (receiver *sdkVersions) WithFile(filePath string) sdkmSDKVersion.SDKVersionsCache {
receiver.filePath = filePath
receiver.loadFromFile()
func (receiver *cache) WithExternalStore(cacheStorage sdkmSDKVersion.CacheStorage) sdkmSDKVersion.Cache {
receiver.cacheStorage = cacheStorage
maps.Clear(receiver.cache)

return receiver
}

func (receiver *sdkVersions) Load(_ context.Context, versionType sdkmSDKVersion.VersionType) []sdkmSDKVersion.SDKVersion {
value, ok := receiver.sdkVersions.Load(versionType)
func (receiver *cache) Valid(ctx context.Context) bool {
if receiver.cacheStorage != nil {
return receiver.cacheStorage.Valid(ctx)
}

return len(receiver.cache) > 0
}

func (receiver *cache) Load(ctx context.Context, versionType sdkmSDKVersion.VersionType) []sdkmSDKVersion.SDKVersion {
receiver.storeLock.Lock()
defer receiver.storeLock.Unlock()

if cacheStorage := receiver.cacheStorage; cacheStorage != nil {
if len(receiver.cache) == 0 || !cacheStorage.Valid(ctx) {
receiver.cache = cacheStorage.Load(ctx)
}
}

var list, ok = receiver.cache[versionType]
if !ok {
return []sdkmSDKVersion.SDKVersion{}
}

return value.([]sdkmSDKVersion.SDKVersion)
return list
}

func (receiver *sdkVersions) Store(
func (receiver *cache) Store(
ctx context.Context, versionType sdkmSDKVersion.VersionType, sdkVersions []sdkmSDKVersion.SDKVersion,
) {
receiver.sdkVersions.Store(versionType, sdkVersions)
receiver.storeLock.Lock()
defer receiver.storeLock.Unlock()

receiver.cache[versionType] = sdkVersions

if receiver.filePath != "" {
receiver.saveToFile(ctx)
if receiver.cacheStorage != nil {
receiver.cacheStorage.Store(ctx, receiver.cache)
}
}
Loading

0 comments on commit c13dd8e

Please sign in to comment.