url/internal/svc/service_context.go

48 lines
1.1 KiB
Go

package svc
import (
"context"
"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),
},
})
_, err := ur.Index20241226001UP(context.Background())
if err != nil {
panic(err)
}
urlUseCase := uc.MustURLUseCase(uc.URLUseCaseParam{
URLRepo: ur,
})
return &ServiceContext{
Config: c,
URLUseCase: urlUseCase,
}
}