@gsb-core/core
Version:
GSB core services and classes for platform-independent web applications
133 lines (132 loc) • 6.17 kB
TypeScript
import { GsbEntityDef } from '../../models/gsb-entity-def.model';
import { QueryParams } from '../../types/query-params';
import { GsbEnum } from '../../models/gsb-enum.model';
import { GsbQueryResponse } from '../../types/responses';
export type PersistenceType = 'localStorage' | 'indexedDB' | 'none';
export declare class GsbCacheService {
private static instance;
private entityDefCache;
private entityCache;
private baseUrl;
private enumCache;
private propertyDefCache;
private genericCache;
private readonly CACHE_DURATION;
private enumCacheTimeout;
private entityService;
private allDefinitionsCached;
private allDefinitionsCachedTimestamp;
private acquireQueue;
private dbPromise;
private readonly DB_NAME;
private readonly OBJECT_STORE_NAME;
private constructor();
static getInstance(): GsbCacheService;
private isIndexedDbSupported;
private getDb;
private getIndexedDb;
private setIndexedDb;
private removeIndexedDb;
private clearIndexedDb;
getAuthParams(): Promise<{
token?: string;
tenantCode?: string;
sessionId?: string;
}>;
private acquire;
private getPropertyDefs;
/**
* Get an entity definition from the cache
* @param def The entity definition to get from the cache
* @param token Optional custom token to use for this request
* @param tenantCode Optional custom tenant code to use for this request
* @returns The entity definition
*/
getEntityDef(def: GsbEntityDef, token?: string, tenantCode?: string): Promise<GsbEntityDef | null>;
/**
* Set an entity definition in the cache
* @param newEntityDef The entity definition to set in the cache
* @param arg1 Optional argument 1
* @param arg2 Optional argument 2
*/
setEntityDef(newEntityDef: GsbEntityDef, token?: string, tenantCode?: string): void;
removeEntityDef(id: string, token: string | undefined, tenantCode: string | undefined): boolean;
/**
* Get an enum definition from the cache
* @param id The id of the enum definition to get from the cache
* @param token Optional custom token to use for this request
* @param tenantCode Optional custom tenant code to use for this request
* @returns The enum definition or null if not found
*/
getEnum(id: string, token?: string, tenantCode?: string): Promise<GsbEnum | null>;
getEnums(enumIds: string[], token?: string, tenantCode?: string): Promise<Map<string, GsbEnum>>;
/**
* Store entity definitions in cache
* @param entityDefs Array of entity definitions to store in cache
* @param token Optional custom token to use for this request
* @param tenantCode Optional custom tenant code to use for this request
*/
storeAllEntityDefinitions(entityDefs: GsbEntityDef[], token?: string, tenantCode?: string): Promise<void>;
/**
* Check if all entity definitions are cached and not expired
* @returns true if all definitions are cached and not expired
*/
areAllDefinitionsCached(): boolean;
/**
* Get all cached entity definitions
* @returns Array of all cached entity definitions
*/
getAllCachedEntityDefinitions(): GsbEntityDef[];
/**
* Get a value from the generic cache with optional localStorage fallback
* @param key The cache key
* @param localStorageKey Optional localStorage key for persistence
* @param duration Optional cache duration in milliseconds (defaults to CACHE_DURATION)
* @returns The cached value or null if not found or expired
*/
getCache<T>(key: string, localStorageKey?: string, duration?: number): T | null;
/**
* Set a value in the generic cache with optional localStorage persistence
* @param key The cache key
* @param value The value to store
* @param duration Optional cache duration in milliseconds (defaults to CACHE_DURATION)
* @param localStorageKey Optional localStorage key for persistence
* @returns The stored value
*/
setCache<T>(key: string, value: T, duration?: number, localStorageKey?: string): T;
/**
* Remove a value from both the generic cache and localStorage if specified
* @param key The cache key
* @param localStorageKey Optional localStorage key to also remove
*/
removeCache(key: string, localStorageKey?: string): void;
clearCache(): void;
/**
* Get all entity definitions
*/
getAllEntityDefs(): Promise<{
entityDefs: GsbEntityDef[];
}>;
/**
* Fetches entities for a given definition from cache if available or from server if not
* @param entityDef The entity definition to fetch data for
* @param cacheKey Optional custom cache key (defaults to entity name)
* @param queryParams Optional query parameters (defaults to QueryType.Full)
* @param expiry Optional cache expiry time in ms (defaults to CACHE_DURATION)
* @param token Optional custom token to use for this request
* @param tenantCode Optional custom tenant code to use for this request
* @returns Query response with entities
*/
fetchOrCached(entDefName: string, cacheKey?: string, queryParams?: QueryParams<any>, expiry?: number, token?: string, tenantCode?: string): Promise<GsbQueryResponse>;
/**
* Get all entity definitions metadata with their properties
* This method caches the results and returns from cache if available
* @param token Optional custom token to use for this request
* @param tenantCode Optional custom tenant code to use for this request
* @returns Array of entity definitions with their properties
*/
getAllDefinitions(token?: string, tenantCode?: string): Promise<GsbEntityDef[]>;
getCacheAsync<T>(key: string, persistenceKey?: string, duration?: number, persistence?: PersistenceType): Promise<T | null>;
setCacheAsync<T>(key: string, value: T, duration?: number, persistenceKey?: string, persistence?: PersistenceType): Promise<T>;
removeCacheAsync(key: string, persistenceKey?: string, persistence?: PersistenceType): Promise<void>;
}