This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated GA data to use the database as cache #37
Signed-off-by: RaenonX <[email protected]>
- Loading branch information
Showing
4 changed files
with
79 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {Collection, MongoClient} from 'mongodb'; | ||
|
||
import {CollectionInfo} from '../../base/controller/info'; | ||
import {CACHE_LIFE_SECS} from '../../utils/cache/const'; | ||
import {getCollection} from '../../utils/mongodb'; | ||
import {GACache} from './type'; | ||
|
||
|
||
const dbInfo: CollectionInfo = { | ||
dbName: 'cache', | ||
collectionName: 'ga', | ||
}; | ||
|
||
export enum GACacheKey { | ||
data = 'd', | ||
generationTimestamp = 'g' | ||
} | ||
|
||
export type GACacheDocument = { | ||
[GACacheKey.data]: GACache, | ||
[GACacheKey.generationTimestamp]: Date, | ||
}; | ||
|
||
const getCacheCollection = (mongoClient: MongoClient): Collection<GACacheDocument> => { | ||
return getCollection<GACacheDocument>(mongoClient, dbInfo, (collection) => { | ||
// Enable data auto-expiration | ||
collection.createIndex(GACacheKey.generationTimestamp, {expireAfterSeconds: CACHE_LIFE_SECS}); | ||
}); | ||
}; | ||
|
||
export const getCache = async (mongoClient: MongoClient): Promise<GACache | null> => { | ||
const collection = getCacheCollection(mongoClient); | ||
|
||
const cacheEntry = await collection.findOne(); | ||
|
||
if (!cacheEntry) { | ||
return null; | ||
} | ||
|
||
return cacheEntry[GACacheKey.data]; | ||
}; | ||
|
||
export const setCache = async (mongoClient: MongoClient, data: GACache): Promise<void> => { | ||
const collection = getCacheCollection(mongoClient); | ||
|
||
await collection.insertOne({ | ||
[GACacheKey.data]: data, | ||
[GACacheKey.generationTimestamp]: new Date(), | ||
}); | ||
}; | ||
|
||
export const resetCache = async (mongoClient: MongoClient): Promise<void> => { | ||
const collection = getCacheCollection(mongoClient); | ||
|
||
await collection.deleteMany({}); | ||
}; |