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(Session): Add enable_session_cache option #664

Merged
merged 1 commit into from
Jun 3, 2024
Merged
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
15 changes: 11 additions & 4 deletions src/core/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export type SessionOptions = {
* If you want to force a new session to be generated, you must clear the cache.
*/
generate_session_locally?: boolean;
/**
* Specifies whether the session data should be cached.
*/
enable_session_cache?: boolean;
/**
* Platform to use for the session.
*/
Expand Down Expand Up @@ -254,7 +258,8 @@ export default class Session extends EventEmitter {
options.timezone,
options.fetch,
options.on_behalf_of_user,
options.cache
options.cache,
options.enable_session_cache
);

return new Session(
Expand Down Expand Up @@ -317,13 +322,14 @@ export default class Session extends EventEmitter {
tz: string = Intl.DateTimeFormat().resolvedOptions().timeZone,
fetch: FetchFunction = Platform.shim.fetch,
on_behalf_of_user?: string,
cache?: ICache
cache?: ICache,
enable_session_cache = true
) {
const session_args = { lang, location, time_zone: tz, device_category, client_name, enable_safety_mode, visitor_data, on_behalf_of_user };

let session_data: SessionData | undefined;

if (cache) {
if (cache && enable_session_cache) {
const cached_session_data = await this.fromCache(cache, session_args);
if (cached_session_data) {
Log.info(TAG, 'Found session data in cache.');
Expand Down Expand Up @@ -372,7 +378,8 @@ export default class Session extends EventEmitter {
context: this.#buildContext(context_data)
};

await this.#storeSession(session_data, cache);
if (enable_session_cache)
await this.#storeSession(session_data, cache);
}

Log.debug(TAG, 'Session data:', session_data);
Expand Down
Loading