Skip to content

[FSSDK-11505] update cache interface #1051

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

Merged
merged 3 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions lib/core/decision_service/cmab/cmab_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { LoggerFacade } from "../../../logging/logger";
import { IOptimizelyUserContext } from "../../../optimizely_user_context";
import { ProjectConfig } from "../../../project_config/project_config"
import { OptimizelyDecideOption, UserAttributes } from "../../../shared_types"
import { Cache } from "../../../utils/cache/cache";
import { Cache, CacheWithRemove } from "../../../utils/cache/cache";
import { CmabClient } from "./cmab_client";
import { v4 as uuidV4 } from 'uuid';
import murmurhash from "murmurhash";
Expand Down Expand Up @@ -53,12 +53,12 @@ export type CmabCacheValue = {

export type CmabServiceOptions = {
logger?: LoggerFacade;
cmabCache: Cache<CmabCacheValue>;
cmabCache: CacheWithRemove<CmabCacheValue>;
cmabClient: CmabClient;
}

export class DefaultCmabService implements CmabService {
private cmabCache: Cache<CmabCacheValue>;
private cmabCache: CacheWithRemove<CmabCacheValue>;
private cmabClient: CmabClient;
private logger?: LoggerFacade;

Expand All @@ -81,7 +81,7 @@ export class DefaultCmabService implements CmabService {
}

if (options[OptimizelyDecideOption.RESET_CMAB_CACHE]) {
this.cmabCache.clear();
this.cmabCache.reset();
}

const cacheKey = this.getCacheKey(userContext.getUserId(), ruleId);
Expand All @@ -90,7 +90,7 @@ export class DefaultCmabService implements CmabService {
this.cmabCache.remove(cacheKey);
}

const cachedValue = await this.cmabCache.get(cacheKey);
const cachedValue = await this.cmabCache.lookup(cacheKey);

const attributesJson = JSON.stringify(filteredAttributes, Object.keys(filteredAttributes).sort());
const attributesHash = String(murmurhash.v3(attributesJson));
Expand All @@ -104,7 +104,7 @@ export class DefaultCmabService implements CmabService {
}

const variation = await this.fetchDecision(ruleId, userContext.getUserId(), filteredAttributes);
this.cmabCache.set(cacheKey, {
this.cmabCache.save(cacheKey, {
attributesHash,
variationId: variation.variationId,
cmabUuid: variation.cmabUuid,
Expand Down
12 changes: 7 additions & 5 deletions lib/event_processor/batch_event_processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
*/

import { EventProcessor, ProcessableEvent } from "./event_processor";
import { Cache } from "../utils/cache/cache";
import { getBatchedAsync, getBatchedSync, Store } from "../utils/cache/store";
import { EventDispatcher, EventDispatcherResponse, LogEvent } from "./event_dispatcher/event_dispatcher";
import { buildLogEvent } from "./event_builder/log_event";
import { BackoffController, ExponentialBackoff, IntervalRepeater, Repeater } from "../utils/repeater/repeater";
Expand Down Expand Up @@ -49,7 +49,7 @@ export type BatchEventProcessorConfig = {
dispatchRepeater: Repeater,
failedEventRepeater?: Repeater,
batchSize: number,
eventStore?: Cache<EventWithId>,
eventStore?: Store<EventWithId>,
eventDispatcher: EventDispatcher,
closingEventDispatcher?: EventDispatcher,
logger?: LoggerFacade,
Expand All @@ -69,7 +69,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
private closingEventDispatcher?: EventDispatcher;
private eventQueue: EventWithId[] = [];
private batchSize: number;
private eventStore?: Cache<EventWithId>;
private eventStore?: Store<EventWithId>;
private dispatchRepeater: Repeater;
private failedEventRepeater?: Repeater;
private idGenerator: IdGenerator = new IdGenerator();
Expand Down Expand Up @@ -114,7 +114,9 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
(k) => !this.dispatchingEventIds.has(k) && !this.eventQueue.find((e) => e.id === k)
);

const events = await this.eventStore.getBatched(keys);
const events = await (this.eventStore.operation === 'sync' ?
getBatchedSync(this.eventStore, keys) : getBatchedAsync(this.eventStore, keys));

const failedEvents: EventWithId[] = [];
events.forEach((e) => {
if(e) {
Expand Down
18 changes: 9 additions & 9 deletions lib/event_processor/event_processor_factory.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,14 +41,14 @@ vi.mock('../utils/cache/local_storage_cache.browser', () => {
return { LocalStorageCache: vi.fn() };
});

vi.mock('../utils/cache/cache', () => {
return { SyncPrefixCache: vi.fn() };
vi.mock('../utils/cache/store', () => {
return { SyncPrefixStore: vi.fn() };
});


import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
import { SyncPrefixCache } from '../utils/cache/cache';
import { SyncPrefixStore } from '../utils/cache/store';
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.browser';
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
import sendBeaconEventDispatcher from './event_dispatcher/send_beacon_dispatcher.browser';
Expand Down Expand Up @@ -85,21 +85,21 @@ describe('createForwardingEventProcessor', () => {
describe('createBatchEventProcessor', () => {
const mockGetOpaqueBatchEventProcessor = vi.mocked(getOpaqueBatchEventProcessor);
const MockLocalStorageCache = vi.mocked(LocalStorageCache);
const MockSyncPrefixCache = vi.mocked(SyncPrefixCache);
const MockSyncPrefixStore = vi.mocked(SyncPrefixStore);

beforeEach(() => {
mockGetOpaqueBatchEventProcessor.mockClear();
MockLocalStorageCache.mockClear();
MockSyncPrefixCache.mockClear();
MockSyncPrefixStore.mockClear();
});

it('uses LocalStorageCache and SyncPrefixCache to create eventStore', () => {
it('uses LocalStorageCache and SyncPrefixStore to create eventStore', () => {
const processor = createBatchEventProcessor({});
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
const eventStore = mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore;
expect(Object.is(eventStore, MockSyncPrefixCache.mock.results[0].value)).toBe(true);
expect(Object.is(eventStore, MockSyncPrefixStore.mock.results[0].value)).toBe(true);

const [cache, prefix, transformGet, transformSet] = MockSyncPrefixCache.mock.calls[0];
const [cache, prefix, transformGet, transformSet] = MockSyncPrefixStore.mock.calls[0];
expect(Object.is(cache, MockLocalStorageCache.mock.results[0].value)).toBe(true);
expect(prefix).toBe(EVENT_STORE_PREFIX);

Expand Down
4 changes: 2 additions & 2 deletions lib/event_processor/event_processor_factory.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import sendBeaconEventDispatcher from './event_dispatcher/send_beacon_dispatcher.browser';
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
import { SyncPrefixCache } from '../utils/cache/cache';
import { SyncPrefixStore } from '../utils/cache/store';
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';

export const DEFAULT_EVENT_BATCH_SIZE = 10;
Expand All @@ -45,7 +45,7 @@ export const createBatchEventProcessor = (
options: BatchEventProcessorOptions = {}
): OpaqueEventProcessor => {
const localStorageCache = new LocalStorageCache<EventWithId>();
const eventStore = new SyncPrefixCache<EventWithId, EventWithId>(
const eventStore = new SyncPrefixStore<EventWithId, EventWithId>(
localStorageCache, EVENT_STORE_PREFIX,
identity,
identity,
Expand Down
32 changes: 16 additions & 16 deletions lib/event_processor/event_processor_factory.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,16 +39,16 @@ vi.mock('../utils/cache/async_storage_cache.react_native', () => {
return { AsyncStorageCache: vi.fn() };
});

vi.mock('../utils/cache/cache', () => {
return { SyncPrefixCache: vi.fn(), AsyncPrefixCache: vi.fn() };
vi.mock('../utils/cache/store', () => {
return { SyncPrefixStore: vi.fn(), AsyncPrefixStore: vi.fn() };
});

import { createBatchEventProcessor, createForwardingEventProcessor } from './event_processor_factory.node';
import { getForwardingEventProcessor } from './forwarding_event_processor';
import nodeDefaultEventDispatcher from './event_dispatcher/default_dispatcher.node';
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
import { getOpaqueBatchEventProcessor } from './event_processor_factory';
import { AsyncCache, AsyncPrefixCache, SyncCache, SyncPrefixCache } from '../utils/cache/cache';
import { AsyncStore, AsyncPrefixStore, SyncStore, SyncPrefixStore } from '../utils/cache/store';
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';

describe('createForwardingEventProcessor', () => {
Expand Down Expand Up @@ -80,14 +80,14 @@ describe('createForwardingEventProcessor', () => {
describe('createBatchEventProcessor', () => {
const mockGetOpaqueBatchEventProcessor = vi.mocked(getOpaqueBatchEventProcessor);
const MockAsyncStorageCache = vi.mocked(AsyncStorageCache);
const MockSyncPrefixCache = vi.mocked(SyncPrefixCache);
const MockAsyncPrefixCache = vi.mocked(AsyncPrefixCache);
const MockSyncPrefixStore = vi.mocked(SyncPrefixStore);
const MockAsyncPrefixStore = vi.mocked(AsyncPrefixStore);

beforeEach(() => {
mockGetOpaqueBatchEventProcessor.mockClear();
MockAsyncStorageCache.mockClear();
MockSyncPrefixCache.mockClear();
MockAsyncPrefixCache.mockClear();
MockSyncPrefixStore.mockClear();
MockAsyncPrefixStore.mockClear();
});

it('uses no default event store if no eventStore is provided', () => {
Expand All @@ -98,16 +98,16 @@ describe('createBatchEventProcessor', () => {
expect(eventStore).toBe(undefined);
});

it('wraps the provided eventStore in a SyncPrefixCache if a SyncCache is provided as eventStore', () => {
it('wraps the provided eventStore in a SyncPrefixStore if a SyncCache is provided as eventStore', () => {
const eventStore = {
operation: 'sync',
} as SyncCache<string>;
} as SyncStore<string>;

const processor = createBatchEventProcessor({ eventStore });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);

expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockSyncPrefixCache.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockSyncPrefixCache.mock.calls[0];
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockSyncPrefixStore.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockSyncPrefixStore.mock.calls[0];

expect(cache).toBe(eventStore);
expect(prefix).toBe(EVENT_STORE_PREFIX);
Expand All @@ -117,16 +117,16 @@ describe('createBatchEventProcessor', () => {
expect(transformSet({ value: 1 })).toBe('{"value":1}');
});

it('wraps the provided eventStore in a AsyncPrefixCache if a AsyncCache is provided as eventStore', () => {
it('wraps the provided eventStore in a AsyncPrefixStore if a AsyncCache is provided as eventStore', () => {
const eventStore = {
operation: 'async',
} as AsyncCache<string>;
} as AsyncStore<string>;

const processor = createBatchEventProcessor({ eventStore });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);

expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockAsyncPrefixCache.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockAsyncPrefixCache.mock.calls[0];
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockAsyncPrefixStore.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockAsyncPrefixStore.mock.calls[0];

expect(cache).toBe(eventStore);
expect(prefix).toBe(EVENT_STORE_PREFIX);
Expand Down
40 changes: 20 additions & 20 deletions lib/event_processor/event_processor_factory.react_native.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,8 +40,8 @@ vi.mock('../utils/cache/async_storage_cache.react_native', () => {
return { AsyncStorageCache: vi.fn() };
});

vi.mock('../utils/cache/cache', () => {
return { SyncPrefixCache: vi.fn(), AsyncPrefixCache: vi.fn() };
vi.mock('../utils/cache/store', () => {
return { SyncPrefixStore: vi.fn(), AsyncPrefixStore: vi.fn() };
});

vi.mock('@react-native-community/netinfo', () => {
Expand Down Expand Up @@ -79,7 +79,7 @@ import { getForwardingEventProcessor } from './forwarding_event_processor';
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL, getPrefixEventStore } from './event_processor_factory';
import { getOpaqueBatchEventProcessor } from './event_processor_factory';
import { AsyncCache, AsyncPrefixCache, SyncCache, SyncPrefixCache } from '../utils/cache/cache';
import { AsyncStore, AsyncPrefixStore, SyncStore, SyncPrefixStore } from '../utils/cache/store';
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
import { BatchEventProcessor } from './batch_event_processor';
Expand Down Expand Up @@ -115,15 +115,15 @@ describe('createForwardingEventProcessor', () => {
describe('createBatchEventProcessor', () => {
const mockGetOpaqueBatchEventProcessor = vi.mocked(getOpaqueBatchEventProcessor);
const MockAsyncStorageCache = vi.mocked(AsyncStorageCache);
const MockSyncPrefixCache = vi.mocked(SyncPrefixCache);
const MockAsyncPrefixCache = vi.mocked(AsyncPrefixCache);
const MockSyncPrefixStore = vi.mocked(SyncPrefixStore);
const MockAsyncPrefixStore = vi.mocked(AsyncPrefixStore);

beforeEach(() => {
isNetInfoAvailable = false;
mockGetOpaqueBatchEventProcessor.mockClear();
MockAsyncStorageCache.mockClear();
MockSyncPrefixCache.mockClear();
MockAsyncPrefixCache.mockClear();
MockSyncPrefixStore.mockClear();
MockAsyncPrefixStore.mockClear();
});

it('returns an instance of ReacNativeNetInfoEventProcessor if netinfo can be required', async () => {
Expand All @@ -140,14 +140,14 @@ describe('createBatchEventProcessor', () => {
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][1]).toBe(BatchEventProcessor);
});

it('uses AsyncStorageCache and AsyncPrefixCache to create eventStore if no eventStore is provided', () => {
it('uses AsyncStorageCache and AsyncPrefixStore to create eventStore if no eventStore is provided', () => {
const processor = createBatchEventProcessor({});

expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
const eventStore = mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore;
expect(Object.is(eventStore, MockAsyncPrefixCache.mock.results[0].value)).toBe(true);
expect(Object.is(eventStore, MockAsyncPrefixStore.mock.results[0].value)).toBe(true);

const [cache, prefix, transformGet, transformSet] = MockAsyncPrefixCache.mock.calls[0];
const [cache, prefix, transformGet, transformSet] = MockAsyncPrefixStore.mock.calls[0];
expect(Object.is(cache, MockAsyncStorageCache.mock.results[0].value)).toBe(true);
expect(prefix).toBe(EVENT_STORE_PREFIX);

Expand Down Expand Up @@ -177,7 +177,7 @@ describe('createBatchEventProcessor', () => {
isAsyncStorageAvailable = false;
const eventStore = {
operation: 'sync',
} as SyncCache<string>;
} as SyncStore<string>;

const { AsyncStorageCache } = await vi.importActual<
typeof import('../utils/cache/async_storage_cache.react_native')
Expand All @@ -192,16 +192,16 @@ describe('createBatchEventProcessor', () => {
isAsyncStorageAvailable = true;
});

it('wraps the provided eventStore in a SyncPrefixCache if a SyncCache is provided as eventStore', () => {
it('wraps the provided eventStore in a SyncPrefixStore if a SyncCache is provided as eventStore', () => {
const eventStore = {
operation: 'sync',
} as SyncCache<string>;
} as SyncStore<string>;

const processor = createBatchEventProcessor({ eventStore });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);

expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockSyncPrefixCache.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockSyncPrefixCache.mock.calls[0];
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockSyncPrefixStore.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockSyncPrefixStore.mock.calls[0];

expect(cache).toBe(eventStore);
expect(prefix).toBe(EVENT_STORE_PREFIX);
Expand All @@ -211,16 +211,16 @@ describe('createBatchEventProcessor', () => {
expect(transformSet({ value: 1 })).toBe('{"value":1}');
});

it('wraps the provided eventStore in a AsyncPrefixCache if a AsyncCache is provided as eventStore', () => {
it('wraps the provided eventStore in a AsyncPrefixStore if a AsyncCache is provided as eventStore', () => {
const eventStore = {
operation: 'async',
} as AsyncCache<string>;
} as AsyncStore<string>;

const processor = createBatchEventProcessor({ eventStore });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);

expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockAsyncPrefixCache.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockAsyncPrefixCache.mock.calls[0];
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore).toBe(MockAsyncPrefixStore.mock.results[0].value);
const [cache, prefix, transformGet, transformSet] = MockAsyncPrefixStore.mock.calls[0];

expect(cache).toBe(eventStore);
expect(prefix).toBe(EVENT_STORE_PREFIX);
Expand Down
4 changes: 2 additions & 2 deletions lib/event_processor/event_processor_factory.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
wrapEventProcessor,
} from './event_processor_factory';
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
import { AsyncPrefixCache } from '../utils/cache/cache';
import { AsyncPrefixStore } from '../utils/cache/store';
import { BatchEventProcessor, EventWithId } from './batch_event_processor';
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
Expand All @@ -45,7 +45,7 @@ const identity = <T>(v: T): T => v;
const getDefaultEventStore = () => {
const asyncStorageCache = new AsyncStorageCache<EventWithId>();

const eventStore = new AsyncPrefixCache<EventWithId, EventWithId>(
const eventStore = new AsyncPrefixStore<EventWithId, EventWithId>(
asyncStorageCache,
EVENT_STORE_PREFIX,
identity,
Expand Down
Loading
Loading