-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an application cache for REST API (#9626)
* implement application cache * add snyk exception --------- Signed-off-by: Jesse Nelson <[email protected]>
- Loading branch information
Showing
18 changed files
with
662 additions
and
31 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,48 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import config from '../config'; | ||
import {Cache} from '../cache'; | ||
import {defaultBeforeAllTimeoutMillis} from './integrationUtils'; | ||
|
||
let cache; | ||
|
||
beforeAll(async () => { | ||
config.redis.enabled = false; | ||
logger.info('Redis disabled'); | ||
}, defaultBeforeAllTimeoutMillis); | ||
|
||
beforeEach(async () => { | ||
cache = new Cache(); | ||
}); | ||
|
||
const loader = (keys) => keys.map((key) => `v${key}`); | ||
const keyMapper = (key) => `k${key}`; | ||
|
||
describe('Redis disabled', () => { | ||
test('get', async () => { | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
}); | ||
|
||
test('getSingleWithTtl', async () => { | ||
const key = 'myKey'; | ||
const value = await cache.getSingleWithTtl(key); | ||
expect(value).toBeUndefined(); | ||
const setResult = await cache.setSingle(key, 5, 'someValue'); | ||
expect(setResult).toBeUndefined(); | ||
}); | ||
}); |
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,58 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import config from '../config'; | ||
import {Cache} from '../cache'; | ||
import {RedisContainer} from '@testcontainers/redis'; | ||
import {defaultBeforeAllTimeoutMillis} from './integrationUtils'; | ||
|
||
let cache; | ||
let redisContainer; | ||
|
||
beforeAll(async () => { | ||
config.redis.enabled = true; | ||
config.redis.uri = 'redis://invalid:6379'; | ||
redisContainer = await new RedisContainer().withStartupTimeout(20000).start(); | ||
logger.info('Started Redis container'); | ||
}, defaultBeforeAllTimeoutMillis); | ||
|
||
afterAll(async () => { | ||
await cache.stop(); | ||
await redisContainer.stop({signal: 'SIGKILL', t: 5}); | ||
logger.info('Stopped Redis container'); | ||
}); | ||
|
||
beforeEach(async () => { | ||
cache = new Cache(); | ||
}); | ||
|
||
const loader = (keys) => keys.map((key) => `v${key}`); | ||
const keyMapper = (key) => `k${key}`; | ||
|
||
describe('Misconfigured Redis URL', () => { | ||
test('get', async () => { | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
}); | ||
|
||
test('getSingleWithTtl', async () => { | ||
const key = 'myKey'; | ||
const value = await cache.getSingleWithTtl(key); | ||
expect(value).toBeUndefined(); | ||
const setResult = await cache.setSingle(key, 'someValue'); | ||
expect(setResult).toBeUndefined(); | ||
}); | ||
}); |
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.