-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): bunch of small stuff
- Loading branch information
Showing
6 changed files
with
59 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { cloneDeep } from 'lodash-es'; | ||
|
||
export default class MemCache<T = any> { | ||
public readonly ttl: number; | ||
public dataTimestamp: number | undefined; | ||
private data: T | undefined; | ||
|
||
constructor(ttlSeconds = 60) { | ||
this.ttl = ttlSeconds * 1000; //converting to ms | ||
} | ||
|
||
/** | ||
* Checks if the data is still valid or wipes it | ||
*/ | ||
isValid() { | ||
if (this.dataTimestamp === undefined) return false; | ||
if (this.dataTimestamp < Date.now() - this.ttl) { | ||
this.dataTimestamp = undefined; | ||
this.data = undefined; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Sets the cache | ||
*/ | ||
set(data: T) { | ||
this.dataTimestamp = Date.now(); | ||
this.data = data; | ||
} | ||
|
||
/** | ||
* Returns the cache if valid, or undefined | ||
*/ | ||
get() { | ||
if (this.dataTimestamp === undefined || this.data === undefined) { | ||
return undefined; | ||
} | ||
|
||
if (this.isValid()) { | ||
return cloneDeep<T>(this.data); | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
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