-
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.
- Loading branch information
1 parent
1b52488
commit 548c51f
Showing
13 changed files
with
1,300 additions
and
410 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class AbstractPersonalizedCache { | ||
|
||
/** @type {Record<number, Record<string, any>>} */ | ||
#cache = {}; | ||
|
||
/** | ||
* @param {number} userId | ||
* @param {string} entityName | ||
* @param {any} data | ||
*/ | ||
setUserCacheEntity(userId, entityName, data) { | ||
if (typeof userId !== 'number') | ||
throw new Error('userId must be number'); | ||
|
||
if (!this.#cache[userId]) | ||
this.#cache[userId] = {}; | ||
|
||
if (typeof this.#cache[userId][entityName] === 'undefined') | ||
this.#cache[userId][entityName] = structuredClone(data); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* @param {number} userId | ||
* @param {string} [entityName] | ||
*/ | ||
getUserCache(userId, entityName) { | ||
if (typeof userId !== 'number') | ||
throw new Error('userId must be number'); | ||
|
||
const userCache = this.#cache[userId]; | ||
|
||
if (!userCache) | ||
return undefined; | ||
|
||
return entityName !== undefined && entityName ? userCache[entityName] : userCache; | ||
} | ||
|
||
/** | ||
* @param {number} userId | ||
* @param {string} [entityName] | ||
*/ | ||
flushUserCache(userId, entityName) { | ||
if (typeof userId !== 'number') | ||
throw new Error('userId must be number'); | ||
|
||
if (!this.#cache[userId]) | ||
return this; | ||
|
||
if (entityName && this.#cache[userId][entityName]) { | ||
this.#cache[userId][entityName] = undefined; | ||
|
||
return this; | ||
} | ||
|
||
this.#cache[userId] = undefined; | ||
|
||
return this; | ||
} | ||
} | ||
|
||
module.exports = AbstractPersonalizedCache; |
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 @@ | ||
const { default: expect } = require('expect'); | ||
const AbstractPersonalizedCache = require('./AbstractPersonalizedCache'); | ||
|
||
describe('AbstractPersonalizedCache', () => { | ||
|
||
/** @type {AbstractPersonalizedCache} */ | ||
let cache; | ||
|
||
before(() => { | ||
cache = new AbstractPersonalizedCache(); | ||
}); | ||
|
||
it('should store user cache entity', () => { | ||
expect(() => { | ||
cache.setUserCacheEntity(1, 'key', [1, 2, 3]) | ||
}).not.toThrow(); | ||
}) | ||
|
||
it('should return user cache', () => { | ||
const userCache = cache.getUserCache(1); | ||
expect(userCache).toHaveProperty('key', [1, 2, 3]); | ||
}); | ||
|
||
it('should return user cache entity', () => { | ||
const userCache = cache.getUserCache(1, 'key'); | ||
expect(userCache).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it('should flush user cache entity', () => { | ||
expect(cache.flushUserCache(1, 'key').getUserCache(1, 'key')).toBe(undefined); | ||
expect(cache.flushUserCache(1).getUserCache(1)).toBe(undefined); | ||
}); | ||
|
||
it('should throw when used incorrectly', () => { | ||
expect(() => { | ||
cache.setUserCacheEntity('key'); | ||
}).toThrow(); | ||
|
||
expect(() => { | ||
cache.getUserCache('key'); | ||
}).toThrow(); | ||
|
||
expect(() => { | ||
cache.flushUserCache('key'); | ||
}).toThrow(); | ||
}); | ||
}); |
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,13 @@ | ||
const truncate = num => { | ||
if (num >= 1e9) { | ||
return (num / 1e9).toFixed(2) + 'B'; | ||
} else if (num >= 1e6) { | ||
return (num / 1e6).toFixed(2) + 'M'; | ||
} else if (num >= 1e3) { | ||
return (num / 1e3).toFixed(2) + 'K'; | ||
} else { | ||
return num.toFixed(2); | ||
} | ||
} | ||
|
||
module.exports = truncate; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.