43 lines
1004 B
Go
43 lines
1004 B
Go
|
package svc
|
||
|
|
||
|
import (
|
||
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
||
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
||
|
"time"
|
||
|
"url_generate/internal/config"
|
||
|
"url_generate/internal/module/url/domain/usecase"
|
||
|
"url_generate/internal/module/url/repository"
|
||
|
uc "url_generate/internal/module/url/usecase"
|
||
|
)
|
||
|
|
||
|
type ServiceContext struct {
|
||
|
Config config.Config
|
||
|
URLUseCase usecase.URLUseCase
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
DefaultSingleFlyCacheTimeout = 60 * time.Second
|
||
|
DefaultFindDataNotFoundTimeout = 5 * time.Second
|
||
|
)
|
||
|
|
||
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||
|
ur := repository.NewURLRepository(repository.URLRepositoryParam{
|
||
|
Conf: &c.Mongo,
|
||
|
CacheConf: c.Cache,
|
||
|
DbOpts: []mon.Option{},
|
||
|
CacheOpts: []cache.Option{
|
||
|
cache.WithExpiry(DefaultSingleFlyCacheTimeout),
|
||
|
cache.WithNotFoundExpiry(DefaultFindDataNotFoundTimeout),
|
||
|
},
|
||
|
})
|
||
|
|
||
|
urlUseCase := uc.MustURLUseCase(uc.URLUseCaseParam{
|
||
|
URLRepo: ur,
|
||
|
})
|
||
|
|
||
|
return &ServiceContext{
|
||
|
Config: c,
|
||
|
URLUseCase: urlUseCase,
|
||
|
}
|
||
|
}
|