wheat-cache/storage/service/single.go

70 lines
1.7 KiB
Go

package service
import (
"time"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/middle"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/storage/dao"
"gitee.com/timedb/wheatCache/storage/persisted"
"github.com/spf13/viper"
)
type singleService struct {
middleProduce event.ProduceInterface
lruProduce event.ProduceInterface
timeOut time.Duration
lruCache *lru.SingleCache
dao dao.Interface
aof *persisted.AOF
}
func loadAOF() *persisted.AOF {
decName := viper.GetString("storage.aof-codec")
var aofCodec persisted.AOFCodec
switch decName {
case "":
return nil
case "b16":
aofCodec = nil
default:
aofCodec = nil
}
aofPath := viper.GetString("storage.aof-path")
aofFlush := viper.GetInt("storage.aof-flush-time")
aofCheckTime := viper.GetInt("storage.aof-check-time")
aofCheckFreq := viper.GetInt("storage.aof-check-freq")
return persisted.NewAOF(aofCodec, aofPath, time.Second*time.Duration(aofFlush),
aofCheckFreq, time.Second*time.Duration(aofCheckTime))
}
func NewSingleServer() proto.CommServerServer {
oneSingleServer.Do(func() {
timeOut := viper.GetInt("storage.timeOut")
if timeOut == 0 {
timeOut = timeOutDefault
}
lruCache := lru.NewLRUCache()
// 加载 aof
aof := loadAOF()
ser := &singleService{
lruCache: lruCache,
lruProduce: event.NewProduce(lruCache.GetDriver()),
timeOut: time.Duration(timeOut) * time.Second,
dao: dao.NewDao(lruCache),
middleProduce: event.NewProduce(middle.NewMiddleWare().GetEventDriver()),
aof: aof,
}
sysSingleService = ser
})
return sysSingleService
}