Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Hash keys for idCache #833

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/identity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IdentityAPIMethod } from './sdkRuntimeModels';
import { isObject } from './utils';
const { Identify, Modify, Login, Logout } = Constants.IdentityMethods;
import { ONE_DAY_IN_SECONDS, MILLIS_IN_ONE_SEC } from './constants';
import { generateHash } from './utils';

export interface IKnownIdentities extends UserIdentities {
device_application_stamp?: string;
Expand Down Expand Up @@ -62,7 +63,7 @@ export const cacheIdentityRequest = (
const cache: Dictionary<ICachedIdentityCall> = idCache.retrieve() || ({} as Dictionary<ICachedIdentityCall>);
const cacheKey = concatenateIdentities(method, identities);

cache[cacheKey] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp};
cache[generateHash(cacheKey)] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be split into two lines for readability.

idCache.store(cache);
};

Expand Down Expand Up @@ -120,10 +121,10 @@ export const hasValidCachedIdentity = (
return false;
}

const cacheKey: string = concatenateIdentities(
const cacheKey: number = generateHash(concatenateIdentities(
method,
proposedUserIdentities
);
));

// if cache doesn't have the cacheKey, there is no valid cached identity
if (!cache.hasOwnProperty(cacheKey)) {
Expand Down Expand Up @@ -153,7 +154,7 @@ export const getCachedIdentity = (
);

const cache = idCache.retrieve();
const cachedIdentity = cache ? cache[cacheKey] : null;
const cachedIdentity = cache ? cache[generateHash(cacheKey)] : null;

return cachedIdentity;
};
Expand Down
10 changes: 5 additions & 5 deletions test/src/tests-identity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ICachedIdentityCall
} from "../../src/identity-utils";
import { LocalStorageVault } from "../../src/vault";
import { Dictionary } from "../../src/utils";
import { Dictionary, generateHash } from "../../src/utils";
import { expect } from 'chai';
import {
MILLISECONDS_IN_ONE_DAY,
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('identity-utils', () => {

expect(Object.keys(updatedMpIdCache!).length).to.equal(1);
const cachedKey =
'identify:device_application_stamp=test-device-id;customerid=id1;';
generateHash('identify:device_application_stamp=test-device-id;customerid=id1;');

expect(updatedMpIdCache!.hasOwnProperty(cachedKey)).to.equal(true);

Expand Down Expand Up @@ -180,7 +180,7 @@ describe('identity-utils', () => {
const updatedMpIdCache = cacheVault.retrieve();

expect(Object.keys(updatedMpIdCache!).length).to.equal(1);
const cachedKey = 'login:device_application_stamp=test-device-id;customerid=id1;';
const cachedKey = generateHash('login:device_application_stamp=test-device-id;customerid=id1;');
expect(updatedMpIdCache!.hasOwnProperty(cachedKey)).to.equal(true);

const cachedLoginCall: ICachedIdentityCall = updatedMpIdCache![
Expand Down Expand Up @@ -421,9 +421,9 @@ describe('identity-utils', () => {
const updatedMpIdCache = cacheVault.retrieve();

const knownIdentities1CachedKey =
'identify:device_application_stamp=test-device-id;customerid=id1;';
generateHash('identify:device_application_stamp=test-device-id;customerid=id1;');
const knownIdentities2CachedKey =
'identify:device_application_stamp=test-device-id;customerid=id2;';
generateHash('identify:device_application_stamp=test-device-id;customerid=id2;');


// both known identities cache keys should exist on the cacheVault
Expand Down
Loading