-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
18 changed files
with
312 additions
and
21 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
40 changes: 40 additions & 0 deletions
40
src/plugins/telemetry_collection_manager/server/cache/cache_manager.test.ts
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,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { CacheManager } from './cache_manager'; | ||
|
||
describe('CacheManager', () => { | ||
const mockCacheKey = 'mock_key'; | ||
const mockCacheItem = 'cache_item'; | ||
const cacheDurationMs = 10000; | ||
let mockNow: number; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers('modern'); | ||
mockNow = jest.getRealSystemTime(); | ||
jest.setSystemTime(mockNow); | ||
}); | ||
afterEach(() => jest.clearAllMocks()); | ||
afterAll(() => jest.useRealTimers()); | ||
|
||
it('caches object for the cache duration only', () => { | ||
const cacheManager = new CacheManager({ cacheDurationMs }); | ||
cacheManager.setCache(mockCacheKey, mockCacheItem); | ||
expect(cacheManager.getFromCache(mockCacheKey)).toEqual(mockCacheItem); | ||
jest.advanceTimersByTime(cacheDurationMs + 100); | ||
expect(cacheManager.getFromCache(mockCacheKey)).toEqual(undefined); | ||
}); | ||
|
||
it('#resetCache removes cached objects', () => { | ||
const cacheManager = new CacheManager({ cacheDurationMs }); | ||
cacheManager.setCache(mockCacheKey, mockCacheItem); | ||
expect(cacheManager.getFromCache(mockCacheKey)).toEqual(mockCacheItem); | ||
cacheManager.resetCache(); | ||
expect(cacheManager.getFromCache(mockCacheKey)).toEqual(undefined); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
src/plugins/telemetry_collection_manager/server/cache/cache_manager.ts
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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import LRUCache from 'lru-cache'; | ||
|
||
export interface CacheManagerConfig { | ||
// cache duration of objects in ms | ||
cacheDurationMs: number; | ||
} | ||
|
||
export class CacheManager { | ||
private readonly cache: LRUCache<string, unknown>; | ||
|
||
constructor({ cacheDurationMs }: CacheManagerConfig) { | ||
this.cache = new LRUCache({ | ||
max: 1, | ||
maxAge: cacheDurationMs, | ||
}); | ||
} | ||
|
||
/** | ||
* Cache an object by key | ||
*/ | ||
public setCache = (cacheKey: string, data: unknown): void => { | ||
this.cache.set(cacheKey, data); | ||
}; | ||
|
||
/** | ||
* returns cached object. If the key is not found will return undefined. | ||
*/ | ||
public getFromCache = <T = unknown>(cacheKey: string): T | undefined => { | ||
return this.cache.get(cacheKey) as T; | ||
}; | ||
|
||
/** | ||
* Removes all cached objects | ||
*/ | ||
public resetCache(): void { | ||
this.cache.reset(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/plugins/telemetry_collection_manager/server/cache/index.ts
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,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { CacheManager } from './cache_manager'; | ||
export type { CacheManagerConfig } from './cache_manager'; |
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
Oops, something went wrong.