Skip to content
Snippets Groups Projects
Unverified Commit 1e702c2a authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

feat(http): Create memory cache provider (#33900)

parent b5ae2f59
No related branches found
Tags 39.165.0
No related merge requests found
import { Http } from '..';
import * as httpMock from '../../../../test/http-mock';
import * as memCache from '../../cache/memory';
import { memCacheProvider as cacheProvider } from './memory-http-cache-provider';
describe('util/http/cache/memory-http-cache-provider', () => {
beforeEach(() => {
memCache.init();
});
afterEach(() => {
memCache.reset();
});
const http = new Http('test');
it('reuses data with etag', async () => {
const scope = httpMock.scope('https://example.com');
scope.get('/foo/bar').reply(200, { msg: 'Hello, world!' }, { etag: '123' });
const res1 = await http.getJsonUnchecked('https://example.com/foo/bar', {
cacheProvider,
});
expect(res1).toMatchObject({
statusCode: 200,
body: { msg: 'Hello, world!' },
authorization: false,
});
const res2 = await http.getJsonUnchecked('https://example.com/foo/bar', {
cacheProvider,
});
expect(res2).toMatchObject({
statusCode: 200,
body: { msg: 'Hello, world!' },
authorization: false,
});
});
});
import * as memCache from '../../cache/memory';
import { AbstractHttpCacheProvider } from './abstract-http-cache-provider';
import type { HttpCache } from './schema';
export class MemoryHttpCacheProvider extends AbstractHttpCacheProvider {
private cacheKey(url: string): string {
return `memory-cache-http-provider:${url}`;
}
protected override load(url: string): Promise<unknown> {
const data = memCache.get<HttpCache>(this.cacheKey(url));
return Promise.resolve(data);
}
protected override persist(url: string, data: HttpCache): Promise<void> {
memCache.set(this.cacheKey(url), data);
return Promise.resolve();
}
}
export const memCacheProvider = new MemoryHttpCacheProvider();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment