Skip to content

Commit

Permalink
Merge pull request #9 from uxuycom/feature/optimize_cache_init_v0.1
Browse files Browse the repository at this point in the history
optimize cache init
  • Loading branch information
max-uxuy authored Jan 15, 2024
2 parents ee4500e + 120a0c9 commit 156c82f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions dcache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ func NewManager(db *storage.DBClient, chain string) *Manager {
return e
}

e.initInscriptionCache()
e.initInscriptionStatsCache()
e.initBalanceCache()
e.initInscriptionCache(chain)
e.initInscriptionStatsCache(chain)
e.initBalanceCache(chain)
e.initUtxoCache()
return e
}

func (h *Manager) initInscriptionCache() {
func (h *Manager) initInscriptionCache(chain string) {
h.Inscription = NewInscription()

startTs := time.Now()
Expand All @@ -68,7 +68,7 @@ func (h *Manager) initInscriptionCache() {
maxSid := uint32(0)
xylog.Logger.Infof("load inscriptions data start...")
for {
items, err := h.db.GetInscriptionsByIdLimit(uint64(start), limit)
items, err := h.db.GetInscriptionsByIdLimit(chain, uint64(start), limit)
if err != nil {
xylog.Logger.Fatalf("failed to initialize inscription cache data. err:%v", err)
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func (h *Manager) initInscriptionCache() {
xylog.Logger.Infof("load inscriptions data finished, cost ts:%v", time.Since(startTs))
}

func (h *Manager) initInscriptionStatsCache() {
func (h *Manager) initInscriptionStatsCache(chain string) {
h.InscriptionStats = NewInscriptionStats()

startTs := time.Now()
Expand All @@ -113,7 +113,7 @@ func (h *Manager) initInscriptionStatsCache() {
maxSid := uint32(0)
xylog.Logger.Infof("load inscription-stats data start...")
for {
items, err := h.db.GetInscriptionStatsByIdLimit(uint64(start), limit)
items, err := h.db.GetInscriptionStatsByIdLimit(chain, uint64(start), limit)
if err != nil {
xylog.Logger.Fatalf("failed to initialize inscription-stats cache data. err:%v", err)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func (h *Manager) initInscriptionStatsCache() {
xylog.Logger.Infof("load inscription-stats data finished, cost ts:%v", time.Since(startTs))
}

func (h *Manager) initBalanceCache() {
func (h *Manager) initBalanceCache(chain string) {
h.Balance = NewBalance()

startTs := time.Now()
Expand All @@ -157,7 +157,7 @@ func (h *Manager) initBalanceCache() {
maxSid := uint64(0)
xylog.Logger.Infof("load balances data start...")
for {
balances, err := h.db.GetBalancesByIdLimit(start, limit)
balances, err := h.db.GetBalancesByIdLimit(chain, start, limit)
if err != nil {
xylog.Logger.Fatalf("failed to initialize balance cache data. err:%v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions explorer/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewExplorer(rpcClient xycommon.IRPCClient, dbc *storage.DBClient, cfg *conf
fromBlock: cfg.Scan.StartBlock,
mu: sync.Mutex{},
dCache: dCache,
blocks: make(chan *xycommon.RpcBlock, 1024),
blocks: make(chan *xycommon.RpcBlock, 100),
txResultHandler: txResultHandler,

scanChn: make(chan uint64, 4),
Expand Down Expand Up @@ -141,7 +141,7 @@ func (e *Explorer) Scan() {
continue
}

if e.currentBlock-blockNumber > 500 {
if e.currentBlock-blockNumber > 100 {
endBlock := blockNumber + scanLimit
if e.config.Scan.BatchWorkers > 0 {
endBlock = blockNumber + e.config.Scan.BatchWorkers
Expand Down
12 changes: 6 additions & 6 deletions storage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,18 +375,18 @@ func (conn *DBClient) GetInscriptions(limit, offset int, chain, protocol, tick,
return data, total, nil
}

func (conn *DBClient) GetInscriptionsByIdLimit(start uint64, limit int) ([]model.Inscriptions, error) {
func (conn *DBClient) GetInscriptionsByIdLimit(chain string, start uint64, limit int) ([]model.Inscriptions, error) {
inscriptions := make([]model.Inscriptions, 0)
err := conn.SqlDB.Where("id > ?", start).Order("id asc").Limit(limit).Find(&inscriptions).Error
err := conn.SqlDB.Where("chain = ?", chain).Where("id > ?", start).Order("id asc").Limit(limit).Find(&inscriptions).Error
if err != nil {
return nil, err
}
return inscriptions, nil
}

func (conn *DBClient) GetInscriptionStatsByIdLimit(start uint64, limit int) ([]model.InscriptionsStats, error) {
func (conn *DBClient) GetInscriptionStatsByIdLimit(chain string, start uint64, limit int) ([]model.InscriptionsStats, error) {
stats := make([]model.InscriptionsStats, 0)
err := conn.SqlDB.Where("id > ?", start).Order("id asc").Limit(limit).Find(&stats).Error
err := conn.SqlDB.Where("chain = ?", chain).Where("id > ?", start).Order("id asc").Limit(limit).Find(&stats).Error
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -520,9 +520,9 @@ func (conn *DBClient) GetUTXOCount(address, chain, protocol, tick string) (int64
return count, nil
}

func (conn *DBClient) GetBalancesByIdLimit(start uint64, limit int) ([]model.Balances, error) {
func (conn *DBClient) GetBalancesByIdLimit(chain string, start uint64, limit int) ([]model.Balances, error) {
balances := make([]model.Balances, 0)
err := conn.SqlDB.Where("id > ?", start).Order("id asc").Limit(limit).Find(&balances).Error
err := conn.SqlDB.Where("chain = ?", chain).Where("id > ?", start).Order("id asc").Limit(limit).Find(&balances).Error
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 156c82f

Please sign in to comment.