Documentation
Cache

Cache

The Cede SDK can be used stateless, but for reasons of fluidity it is strongly recommended to integrate it with a caching system. The Cede SDK let you implement your own cache storage, but also provides a built-in in-memory cache storage.

Implement your own cache storage

💡

By default, the Cede SDK utilizes InMemoryCacheStorage, which, as the name implies, stores cache data in memory. This means that when the server instance is shut down, all cached data is lost. To persist cache data beyond server restarts, you can implement a custom cache storage solution tailored to your needs.

import CedeSDK from "@cedelabs-private/sdk";
 
const cedeSDK = new CedeSDK("MOCK", {
  cacheStorage: {
    init: async (data) => {
      // initialize your cache storage with the provided data
    },
    get: async (key: string) => {
      // return the value from your cache storage
    },
    set: async (key: string, value: any) => {
      // set the value in your cache storage
    },
    remove: async (key: string) => {
      // delete the value from your cache storage
    },
    removeByIncludedStrings: async (includedStrings: string[]) => {
      // delete all keys from your cache storage that include all the provided strings
    },
  },
});

Cache Keys

The Cede SDK exports constants and functions to calculate cache keys. We do not recommend using these keys directly in your application, but rather using the SDK methods to access data. However, it can be useful to debug or understand the SDK cache.

export const getCacheAssetsPerExchangeInstanceIdentifier = (exchangeInstanceId: string, walletType?: WalletType) =>
  `portfolio-assets-${exchangeInstanceId}-${walletType ?? ""}`;
 
export const pricesIdentifier = "prices";
 
export const fiatCurrenciesIdentifier = "fiat-currencies";
 
export const supportedExchangesIdentifier = "supported-exchanges";
 
export const getTradePathPerExchangeIdentifier = (exchangeId: string, from: string, to: string) =>
  `getTradePath-${exchangeId}-${from}-${to}`;
 
export const getTradePathBySupportedTokensIdentifier = (exchangeId: string, from: string, network: string) =>
  `getTradePathBySupportedTokens-${exchangeId}-${from}-${network}`;
 
/** "Mock" Mode Cache Keys */
export const getMockCacheAssetsPerExchangeInstanceIdentifier = (exchangeInstanceId: string, walletType?: WalletType) =>
  `${CacheKeys.PORTFOLIO_MOCK}-${exchangeInstanceId}-${walletType ?? ""}`;
 
export const getMockCacheWithdrawalIdentifier = (exchangeInstanceId: string) =>
  `${CacheKeys.WITHDRAW_MOCK}-${exchangeInstanceId}`;
 
export const getMockCacheOrdersPerExchangeInstanceIdentifier = (exchangeInstanceId: string) =>
  `${CacheKeys.TRADE_CONTROLLER_MOCK}-${exchangeInstanceId}`;