-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bff4eba
commit b1ce45b
Showing
7 changed files
with
85 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { P2P } from '@shardus/types' | ||
import { config } from '../Config' | ||
import { queryLatestCycleRecords } from '../dbstore/cycles' | ||
|
||
let cachedCycleRecords: P2P.CycleCreatorTypes.CycleData[] = [] | ||
let lastCacheUpdateFromDBRunning = false | ||
|
||
async function updateCacheFromDB(): Promise<void> { | ||
if (lastCacheUpdateFromDBRunning) { | ||
return | ||
} | ||
|
||
lastCacheUpdateFromDBRunning = true | ||
|
||
try { | ||
cachedCycleRecords = await queryLatestCycleRecords(config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST) | ||
} catch (error) { | ||
console.log('Error updating latest cache: ', error) | ||
} finally { | ||
lastCacheUpdateFromDBRunning = false | ||
} | ||
} | ||
|
||
export async function addCyclesToCache(cycles: P2P.CycleCreatorTypes.CycleData[]): Promise<void> { | ||
cycles.sort((a, b) => a.counter - b.counter); | ||
|
||
if (cachedCycleRecords.length === 0) { | ||
await updateCacheFromDB() | ||
} | ||
|
||
for (const cycle of cycles) { | ||
if(cycle.counter < cachedCycleRecords[0].counter) { | ||
continue | ||
} | ||
|
||
cachedCycleRecords.unshift(cycle) | ||
} | ||
|
||
if (cachedCycleRecords.length > config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST) { | ||
cachedCycleRecords.splice(config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST) | ||
} | ||
} | ||
|
||
export async function getLatestCycleRecords(count: number): Promise<P2P.CycleCreatorTypes.CycleData[]> { | ||
if (cachedCycleRecords.length === 0) { | ||
await updateCacheFromDB() | ||
} | ||
|
||
return cachedCycleRecords.slice(0, count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { P2P, StateManager } from "@shardus/types" | ||
|
||
export interface Cycle { | ||
counter: P2P.CycleCreatorTypes.CycleData['counter'] | ||
cycleRecord: P2P.CycleCreatorTypes.CycleData | ||
cycleMarker: StateManager.StateMetaDataTypes.CycleMarker | ||
} | ||
|
||
export type DbCycle = Cycle & { | ||
cycleRecord: string | ||
} |