Skip to content

Commit

Permalink
remove http cache not static behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLep committed Jul 11, 2023
1 parent 0a9bbfc commit 3ee9d60
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/core/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export abstract class AHCache<T, U> {
currentCacheSize: number = 0;
cacheConfig: AHCacheConfig;

constructor() {}

/**
* Set the cache config
* @param cacheConfig The cache config to set
Expand Down Expand Up @@ -44,7 +42,7 @@ export abstract class AHCache<T, U> {
// Delete data to free up space
while (this.currentCacheSize + dataToAddSize > this.cacheConfig.maxCacheSize) {
AHLogger.getInstance().debug(
'Current cache size will exceed max cache size: older data in cache deleted to free up space',
"Current cache size will exceed max cache size: older data in cache deleted to free up space",
);
this.currentCacheSize = this.currentCacheSize - AHObjectHelper.getSizeOf(this.data[0]);
this.data.splice(0, 1);
Expand Down
17 changes: 0 additions & 17 deletions src/core/cache/http-request-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import { AHHttpRequestParameters } from "../models/cache/http-request-parameters


export class AHHttpRequestCache extends AHCache<AHHttpRequestParameters, AHHttpResponse> {
private static instance: AHHttpRequestCache;

constructor() {
super();
}

/**
* Singleton getInstance method
* @returns The singleton instance
*/
static getInstance(): AHHttpRequestCache {
if (!AHHttpRequestCache.instance) {
AHHttpRequestCache.instance = new AHHttpRequestCache();
}

return AHHttpRequestCache.instance;
}

/**
* Get a cache item with its id
Expand Down
9 changes: 5 additions & 4 deletions src/framework/features/rest-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class AHRestHandler {
private callable: AHCallable;
private cacheConfig: AHCacheConfig = AHRestHandler.defaultCacheConfig;
private options: AHRestHandlerOptions = AHRestHandler.defaultOptions;
private httpCache: AHHttpRequestCache = new AHHttpRequestCache();

constructor(params: AHRestHandlerParams) {
if (!/^[a-zA-z_]+[a-zA-z0-9]*$/.test(params.name)) {
Expand Down Expand Up @@ -130,8 +131,8 @@ export class AHRestHandler {

if (this.cacheConfig.cachable) {
tracker.startSegment("cache-configuration");
AHHttpRequestCache.getInstance().setConfig(this.cacheConfig);
AHHttpRequestCache.getInstance().flushCache(this.cacheConfig.ttl);
this.httpCache.setConfig(this.cacheConfig);
this.httpCache.flushCache(this.cacheConfig.ttl);
tracker.stopSegment("cache-configuration");
}

Expand Down Expand Up @@ -168,7 +169,7 @@ export class AHRestHandler {
tracker.startSegment("cache-retrieving");

AHLogger.getInstance().debug("Try to get last http response from cache");
const cacheResponse = AHHttpRequestCache.getInstance().getCacheItem(
const cacheResponse = this.httpCache.getCacheItem(
AHHttpRequestCache.buildCacheRequestParameters(ev),
);

Expand All @@ -193,7 +194,7 @@ export class AHRestHandler {
tracker.stopSegment("callable-run");

if (this.cacheConfig.cachable) {
AHHttpRequestCache.getInstance().addDataInCache(
this.httpCache.addDataInCache(
AHHttpRequestCache.buildCacheRequestParameters(ev),
callableReponse,
);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/http-request-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('AHHttpRequestCache', () => {
const requestCache = AHHttpRequestCache.buildCacheRequestParameters(AHTestResource.getBaseEvent({ path: '/test' }));
const requestCache2 = AHHttpRequestCache.buildCacheRequestParameters(AHTestResource.getBaseEvent({ path: '/test2' }));
const response = new AHHttpResponse(200, { status: AHHttpResponseBodyStatusEnum.Success });
const httpRequestCache = AHHttpRequestCache.getInstance();
const httpRequestCache = new AHHttpRequestCache();

// Set default cache config
httpRequestCache.setConfig({
Expand Down

0 comments on commit 3ee9d60

Please sign in to comment.