Skip to content

Commit

Permalink
refactoring cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mejgun committed Oct 14, 2024
1 parent 8616fc8 commit efba8e2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
17 changes: 0 additions & 17 deletions src/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package cache

import (
"fmt"
"time"

cache_default "ytproxy/cache/impl/default"
cache_empty "ytproxy/cache/impl/empty"
extractor_config "ytproxy/extractor/config"
logger "ytproxy/logger"
)

type T interface {
Expand All @@ -19,16 +15,3 @@ type T interface {
type ConfigT struct {
ExpireTime *string `json:"expire-time"`
}

func New(conf ConfigT, log logger.T) (T, error) {
t, err := time.ParseDuration(*conf.ExpireTime)
if err != nil {
return cache_default.New(0), err
}
if t.Seconds() < 1 {
log.LogDebug("", "disabled by config")
return cache_empty.New(), nil
}
log.LogDebug("", fmt.Sprintf("expire time set to %s", t))
return cache_default.New(t), nil
}
3 changes: 3 additions & 0 deletions src/cache/impl/default/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"sync"
"time"

cache "ytproxy/cache"
extractor_config "ytproxy/extractor/config"
)

var _ cache.T = (*defaultCache)(nil)

func New(t time.Duration) *defaultCache {
return &defaultCache{
cache: make(map[extractor_config.RequestT]extractor_config.ResultT),
Expand Down
24 changes: 24 additions & 0 deletions src/cache/mux/mux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cache

import (
"fmt"
"time"

cache "ytproxy/cache"
cache_default "ytproxy/cache/impl/default"
cache_empty "ytproxy/cache/impl/empty"
logger "ytproxy/logger"
)

func New(conf cache.ConfigT, log logger.T) (cache.T, error) {
t, err := time.ParseDuration(*conf.ExpireTime)
if err != nil {
return cache_default.New(0), err
}
if t.Seconds() < 1 {
log.LogDebug("", "disabled by config")
return cache_empty.New(), nil
}
log.LogDebug("", fmt.Sprintf("expire time set to %s", t))
return cache_default.New(t), nil
}
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ytproxy

go 1.22.6
go 1.18
4 changes: 2 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"syscall"

app "ytproxy/app"
cache "ytproxy/cache"
cache_mux "ytproxy/cache/mux"
config "ytproxy/config"
extractor "ytproxy/extractor"
logger "ytproxy/logger"
Expand Down Expand Up @@ -192,7 +192,7 @@ func getNewApp(log logger.T, v config.SubConfigT) (app.Option, error) {
if err != nil {
return app.Option{}, nameerr(texts[0], err)
}
cch, err := cache.New(v.Cache,
cch, err := cache_mux.New(v.Cache,
logger.NewLayer(log, newname(texts[1])))
if err != nil {
return app.Option{}, nameerr(texts[1], err)
Expand Down

0 comments on commit efba8e2

Please sign in to comment.