-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): Implement db cache (Closes #72)
For most frequent db request we want to implement cache which will decrease db usage
- Loading branch information
1 parent
cfe21e5
commit bf1f040
Showing
12 changed files
with
224 additions
and
48 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
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,118 @@ | ||
import { Logger } from "../logger"; | ||
import { sSuffix } from "../text"; | ||
|
||
const logger = new Logger("cache"); | ||
|
||
export class CacheProvider<Data, UniqId extends keyof Data> { | ||
private cache: Data[] = []; | ||
|
||
constructor( | ||
private readonly cacheSize: number, | ||
private readonly idKey: UniqId | ||
) { | ||
if (!this.hasCacheEnabled()) { | ||
logger.warn( | ||
`Cache size cacheSize=${cacheSize}, so the cache is turned off for ${logger.y( | ||
idKey | ||
)}` | ||
); | ||
} else { | ||
logger.info( | ||
`Cache size is cacheSize=${logger.y(cacheSize)} ${sSuffix( | ||
"item", | ||
cacheSize | ||
)} initialized for ${logger.y(idKey)}` | ||
); | ||
} | ||
} | ||
|
||
public addItem(item: Data): void { | ||
if (!this.hasCacheEnabled()) { | ||
return; | ||
} | ||
|
||
if (!item[this.idKey]) { | ||
logger.error( | ||
`The item with ${this.idKey}=${ | ||
item[this.idKey] | ||
} can not have empty index value. Caching skipped`, | ||
new Error("Cache item can not have empty index value") | ||
); | ||
return; | ||
} | ||
|
||
const existingItem = this.cache.find( | ||
(cItem) => cItem[this.idKey] === item[this.idKey] | ||
); | ||
if (existingItem) { | ||
logger.warn( | ||
`The item with ${this.idKey}=${ | ||
item[this.idKey] | ||
} is already exists. Removing old data from the cache` | ||
); | ||
|
||
this.removeItem(item[this.idKey]); | ||
} | ||
|
||
const newCacheData = [...this.cache, item]; | ||
|
||
if (newCacheData.length > this.cacheSize) { | ||
logger.warn( | ||
`Cache storage exceeds the limit of ${this.cacheSize} ${sSuffix( | ||
"item", | ||
this.cacheSize | ||
)} and have a size of ${ | ||
newCacheData.length | ||
}. Old records will be removed to keep storage under the limit` | ||
); | ||
} | ||
|
||
this.cache = newCacheData.slice( | ||
Math.max(newCacheData.length - this.cacheSize, 0) | ||
); | ||
|
||
logger.info( | ||
`Added cache item with ${this.idKey}=${item[this.idKey]}. Cache size=${ | ||
this.cache.length | ||
}` | ||
); | ||
} | ||
|
||
public getItem(idValue: Data[UniqId]): Data | null { | ||
if (!this.hasCacheEnabled()) { | ||
return null; | ||
} | ||
|
||
const cachedItem = this.cache.find( | ||
(cItem) => cItem[this.idKey] === idValue | ||
); | ||
|
||
if (!cachedItem) { | ||
logger.info( | ||
`Did not find the item with ${this.idKey}=${idValue} in cache` | ||
); | ||
return null; | ||
} | ||
|
||
logger.info( | ||
`Found the item with ${this.idKey}=${idValue} in cache. Skipping DB request` | ||
); | ||
return cachedItem; | ||
} | ||
|
||
public removeItem(idValue: Data[UniqId]): void { | ||
if (!this.hasCacheEnabled()) { | ||
return; | ||
} | ||
|
||
this.cache = this.cache.filter((cItem) => cItem[this.idKey] !== idValue); | ||
|
||
logger.info( | ||
`Removed cache item for ${this.idKey}=${idValue}. Cache size=${this.cache.length}` | ||
); | ||
} | ||
|
||
private hasCacheEnabled(): boolean { | ||
return this.cacheSize > 0; | ||
} | ||
} |
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,19 @@ | ||
import { LanguageCode } from "../recognition/types"; | ||
|
||
export enum NodeStatKey { | ||
Active = "active", | ||
SelfUrl = "selfUrl", | ||
Version = "version", | ||
} | ||
|
||
export enum UsageStatKey { | ||
UsageCount = "usageCount", | ||
LangId = "langId", | ||
ChatId = "chatId", | ||
UserName = "user", | ||
} | ||
|
||
export interface UsageStatCache { | ||
[UsageStatKey.ChatId]: number; | ||
[UsageStatKey.LangId]: LanguageCode; | ||
} |
Oops, something went wrong.