Skip to content

Commit

Permalink
Convert cache to session storage (opensearch-project#1669) (opensearc…
Browse files Browse the repository at this point in the history
…h-project#1671)

* convert cache to session storage

* update tests to mock session storage

---------

(cherry picked from commit 952b4bc)

Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
(cherry picked from commit 3a69e36)
  • Loading branch information
opensearch-trigger-bot[bot] authored and ps48 committed Apr 9, 2024
1 parent 684a402 commit 62a287a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
36 changes: 18 additions & 18 deletions public/framework/catalog_cache/cache_manager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ interface LooseObject {
[key: string]: any;
}

// Mock localStorage
const localStorageMock = (() => {
// Mock sessionStorage
const sessionStorageMock = (() => {
let store = {} as LooseObject;
return {
getItem(key: string) {
Expand All @@ -41,13 +41,13 @@ const localStorageMock = (() => {
};
})();

Object.defineProperty(window, 'localStorage', { value: localStorageMock });
Object.defineProperty(window, 'sessionStorage', { value: sessionStorageMock });

describe('CatalogCacheManager', () => {
beforeEach(() => {
jest.spyOn(window.localStorage, 'setItem');
jest.spyOn(window.localStorage, 'getItem');
jest.spyOn(window.localStorage, 'removeItem');
jest.spyOn(window.sessionStorage, 'setItem');
jest.spyOn(window.sessionStorage, 'getItem');
jest.spyOn(window.sessionStorage, 'removeItem');
});

afterEach(() => {
Expand All @@ -68,7 +68,7 @@ describe('CatalogCacheManager', () => {
],
};
CatalogCacheManager.saveDataSourceCache(cacheData);
expect(localStorage.setItem).toHaveBeenCalledWith(
expect(sessionStorage.setItem).toHaveBeenCalledWith(
ASYNC_QUERY_DATASOURCE_CACHE,
JSON.stringify(cacheData)
);
Expand All @@ -86,7 +86,7 @@ describe('CatalogCacheManager', () => {
},
],
};
localStorage.setItem(ASYNC_QUERY_DATASOURCE_CACHE, JSON.stringify(initialCacheData));
sessionStorage.setItem(ASYNC_QUERY_DATASOURCE_CACHE, JSON.stringify(initialCacheData));

const newCacheData: DataSourceCacheData = {
version: '1.1',
Expand All @@ -100,7 +100,7 @@ describe('CatalogCacheManager', () => {
],
};
CatalogCacheManager.saveDataSourceCache(newCacheData);
expect(localStorage.setItem).toHaveBeenCalledWith(
expect(sessionStorage.setItem).toHaveBeenCalledWith(
ASYNC_QUERY_DATASOURCE_CACHE,
JSON.stringify(newCacheData)
);
Expand All @@ -120,13 +120,13 @@ describe('CatalogCacheManager', () => {
},
],
};
localStorage.setItem(ASYNC_QUERY_DATASOURCE_CACHE, JSON.stringify(cacheData));
sessionStorage.setItem(ASYNC_QUERY_DATASOURCE_CACHE, JSON.stringify(cacheData));
expect(CatalogCacheManager.getDataSourceCache()).toEqual(cacheData);
});

it('should return default cache object if cache is not found', () => {
const defaultCacheObject = { version: CATALOG_CACHE_VERSION, dataSources: [] };
localStorage.removeItem(ASYNC_QUERY_DATASOURCE_CACHE);
sessionStorage.removeItem(ASYNC_QUERY_DATASOURCE_CACHE);
expect(CatalogCacheManager.getDataSourceCache()).toEqual(defaultCacheObject);
});
});
Expand All @@ -138,7 +138,7 @@ describe('CatalogCacheManager', () => {
dataSources: [],
};
CatalogCacheManager.saveAccelerationsCache(cacheData);
expect(localStorage.setItem).toHaveBeenCalledWith(
expect(sessionStorage.setItem).toHaveBeenCalledWith(
ASYNC_QUERY_ACCELERATIONS_CACHE,
JSON.stringify(cacheData)
);
Expand All @@ -151,7 +151,7 @@ describe('CatalogCacheManager', () => {
version: CATALOG_CACHE_VERSION,
dataSources: [],
};
localStorage.setItem(ASYNC_QUERY_ACCELERATIONS_CACHE, JSON.stringify(cacheData));
sessionStorage.setItem(ASYNC_QUERY_ACCELERATIONS_CACHE, JSON.stringify(cacheData));
expect(CatalogCacheManager.getAccelerationsCache()).toEqual(cacheData);
});

Expand All @@ -160,7 +160,7 @@ describe('CatalogCacheManager', () => {
version: CATALOG_CACHE_VERSION,
dataSources: [],
};
localStorage.removeItem(ASYNC_QUERY_ACCELERATIONS_CACHE);
sessionStorage.removeItem(ASYNC_QUERY_ACCELERATIONS_CACHE);
expect(CatalogCacheManager.getAccelerationsCache()).toEqual(defaultCacheObject);
});
});
Expand Down Expand Up @@ -381,14 +381,14 @@ describe('CatalogCacheManager', () => {
describe('clearDataSourceCache', () => {
it('should clear data source cache from local storage', () => {
CatalogCacheManager.clearDataSourceCache();
expect(localStorage.removeItem).toHaveBeenCalledWith(ASYNC_QUERY_DATASOURCE_CACHE);
expect(sessionStorage.removeItem).toHaveBeenCalledWith(ASYNC_QUERY_DATASOURCE_CACHE);
});
});

describe('clearAccelerationsCache', () => {
it('should clear accelerations cache from local storage', () => {
CatalogCacheManager.clearAccelerationsCache();
expect(localStorage.removeItem).toHaveBeenCalledWith(ASYNC_QUERY_ACCELERATIONS_CACHE);
expect(sessionStorage.removeItem).toHaveBeenCalledWith(ASYNC_QUERY_ACCELERATIONS_CACHE);
});
});

Expand All @@ -404,7 +404,7 @@ describe('CatalogCacheManager', () => {
CatalogCacheManager.addOrUpdateAccelerationsByDataSource(dataSource);

// Verify that saveAccelerationsCache is called with the updated cache data
expect(localStorage.setItem).toHaveBeenCalledWith(
expect(sessionStorage.setItem).toHaveBeenCalledWith(
ASYNC_QUERY_ACCELERATIONS_CACHE,
JSON.stringify({
version: '1.0',
Expand All @@ -431,7 +431,7 @@ describe('CatalogCacheManager', () => {
CatalogCacheManager.addOrUpdateAccelerationsByDataSource(updatedDataSource);

// Verify that saveAccelerationsCache is called with the updated cache data
expect(localStorage.setItem).toHaveBeenCalledWith(
expect(sessionStorage.setItem).toHaveBeenCalledWith(
ASYNC_QUERY_ACCELERATIONS_CACHE,
JSON.stringify({
version: '1.0',
Expand Down
12 changes: 6 additions & 6 deletions public/framework/catalog_cache/cache_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export class CatalogCacheManager {
* @param {DataSourceCacheData} cacheData - The data source cache data to save.
*/
static saveDataSourceCache(cacheData: DataSourceCacheData): void {
localStorage.setItem(this.datasourceCacheKey, JSON.stringify(cacheData));
sessionStorage.setItem(this.datasourceCacheKey, JSON.stringify(cacheData));
}

/**
* Retrieves data source cache from local storage.
* @returns {DataSourceCacheData} The retrieved data source cache.
*/
static getDataSourceCache(): DataSourceCacheData {
const catalogData = localStorage.getItem(this.datasourceCacheKey);
const catalogData = sessionStorage.getItem(this.datasourceCacheKey);

if (catalogData) {
return JSON.parse(catalogData);
Expand All @@ -61,15 +61,15 @@ export class CatalogCacheManager {
* @param {AccelerationsCacheData} cacheData - The accelerations cache data to save.
*/
static saveAccelerationsCache(cacheData: AccelerationsCacheData): void {
localStorage.setItem(this.accelerationsCacheKey, JSON.stringify(cacheData));
sessionStorage.setItem(this.accelerationsCacheKey, JSON.stringify(cacheData));
}

/**
* Retrieves accelerations cache from local storage.
* @returns {AccelerationsCacheData} The retrieved accelerations cache.
*/
static getAccelerationsCache(): AccelerationsCacheData {
const accelerationCacheData = localStorage.getItem(this.accelerationsCacheKey);
const accelerationCacheData = sessionStorage.getItem(this.accelerationsCacheKey);

if (accelerationCacheData) {
return JSON.parse(accelerationCacheData);
Expand Down Expand Up @@ -234,13 +234,13 @@ export class CatalogCacheManager {
* Clears the data source cache from local storage.
*/
static clearDataSourceCache(): void {
localStorage.removeItem(this.datasourceCacheKey);
sessionStorage.removeItem(this.datasourceCacheKey);
}

/**
* Clears the accelerations cache from local storage.
*/
static clearAccelerationsCache(): void {
localStorage.removeItem(this.accelerationsCacheKey);
sessionStorage.removeItem(this.accelerationsCacheKey);
}
}

0 comments on commit 62a287a

Please sign in to comment.