Add close waiter and close logic for db

This commit is contained in:
mcrakhman committed 2022-10-12 21:36:16 +02:00
1 parent 5e2a6778f0
commit ad021e5758
5 files changed
+86 -5

No files matched your search

+6 -2
View File
@@ -29,17 +29,21 @@ type Service interface {
type service struct {
conf config.Space
spaceCache ocache.OCache
closeWaiter *ocache.CloseWaiter
commonSpace commonspace.Service
spaceStorageProvider storage.SpaceStorageProvider
}
func (s *service) Init(a *app.App) (err error) {
s.closeWaiter = ocache.NewCloseWaiter(func(ctx context.Context, id string) (value ocache.Object, err error) {
return s.commonSpace.GetSpace(ctx, id)
})
s.conf = a.MustComponent(config.CName).(*config.Config).Space
s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.Service)
s.spaceStorageProvider = a.MustComponent(storage.CName).(storage.SpaceStorageProvider)
s.spaceCache = ocache.New(
func(ctx context.Context, id string) (value ocache.Object, err error) {
return s.commonSpace.GetSpace(ctx, id)
return s.closeWaiter.Load(ctx, id)
},
ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute),
@@ -66,7 +70,7 @@ func (s *service) GetSpace(ctx context.Context, id string) (commonspace.Space, e
if err != nil {
return nil, err
}
return v.(commonspace.Space), nil
return v.(*ocache.CloseWrapper).Value.(commonspace.Space), nil
}
func (s *service) Close(ctx context.Context) (err error) {