Managing Cache

The purpose of cache management is to address the following issues:

  • Re-allocation in bucketing mode :

In bucketing mode, the SDK ensures that a visitor remains in the variation where they were initially allocated, even if the customer or dynamic allocation changes the traffic allocation. This is because in bucketing mode, the assignment is made on the local device, so changing campaign allocation on the platform could cause visitors to see different campaigns.

  • Handle offline mode on client side :

When the cache is enabled, the SDK attempts to retrieve the latest visitor data (campaign assignments) from the cache. It also saves all failed hits and visitor exposures to resend them later.

Flagship Sdks provide two interfaces to handle caches, one for the visitor cache and the other one for the hits cache. For additional details, refer to your specific SDK reference documentation.

Note: JavaScript for browsers, React.js, and React Native come with a built-in default cache implementation.

import { Flagship, IHitCacheImplementation, IVisitorCacheImplementation } from "@flagship.io/js-sdk";

class HitCache implements IHitCacheImplementation {
  cacheHit(hits: Record<string, HitCacheDTO>): Promise<void> {
    // store hits in the cache
    throw new Error("Method not implemented.");
  }
  lookupHits(): Promise<Record<string, HitCacheDTO>> {
    // retrieve hits from the cache
    throw new Error("Method not implemented.");
  }
  flushHits(hitKeys: string[]): Promise<void> {
    // remove hits from the cache
    throw new Error("Method not implemented.");
  }
  flushAllHits(): Promise<void> {
    // remove all hits from the cache
    throw new Error("Method not implemented.");
  }
}

class VisitorCache implements IVisitorCacheImplementation{
    cacheVisitor(visitorId: string, Data: VisitorCacheDTO): Promise<void> {
        // store visitor data in the cache
        throw new Error("Method not implemented.");
    }
    lookupVisitor(visitorId: string): Promise<VisitorCacheDTO> {
        // retrieve visitor data from the cache
        throw new Error("Method not implemented.");
    }
    flushVisitor(visitorId: string): Promise<void> {
        // remove visitor data from the cache
        throw new Error("Method not implemented.");
    }
    
}

Flagship.start("<ENV_ID>", "<API_KEY>", {
  hitCacheImplementation: new HitCache(),
  visitorCacheImplementation: new VisitorCache(),
});

import React from "react";
import { FlagshipProvider, IHitCacheImplementation, IVisitorCacheImplementation } from "@flagship.io/react-sdk";

class HitCache implements IHitCacheImplementation {
  cacheHit(hits: Record<string, HitCacheDTO>): Promise<void> {
    // store hits in the cache
    throw new Error("Method not implemented.");
  }
  lookupHits(): Promise<Record<string, HitCacheDTO>> {
    // retrieve hits from the cache
    throw new Error("Method not implemented.");
  }
  flushHits(hitKeys: string[]): Promise<void> {
    // remove hits from the cache
    throw new Error("Method not implemented.");
  }
  flushAllHits(): Promise<void> {
    // remove all hits from the cache
    throw new Error("Method not implemented.");
  }
}

class VisitorCache implements IVisitorCacheImplementation{
    cacheVisitor(visitorId: string, Data: VisitorCacheDTO): Promise<void> {
        // store visitor data in the cache
        throw new Error("Method not implemented.");
    }
    lookupVisitor(visitorId: string): Promise<VisitorCacheDTO> {
        // retrieve visitor data from the cache
        throw new Error("Method not implemented.");
    }
    flushVisitor(visitorId: string): Promise<void> {
        // remove visitor data from the cache
        throw new Error("Method not implemented.");
    }
    
}

const App = () => (
  <>
    <FlagshipProvider
      envId="YOUR_ENV_ID"
      apiKey="YOUR_API_KEY"
      visitorData={null}
			hitCacheImplementation={new HitCache()},
      visitorCacheImplementation={new VisitorCache()},
    >
      {/* ... */}
    </FlagshipProvider>
  </>
);
import React from "react";
import { FlagshipProvider, IHitCacheImplementation, IVisitorCacheImplementation } from "@flagship.io/react-native-sdk";

class HitCache implements IHitCacheImplementation {
  cacheHit(hits: Record<string, HitCacheDTO>): Promise<void> {
    // store hits in the cache
    throw new Error("Method not implemented.");
  }
  lookupHits(): Promise<Record<string, HitCacheDTO>> {
    // retrieve hits from the cache
    throw new Error("Method not implemented.");
  }
  flushHits(hitKeys: string[]): Promise<void> {
    // remove hits from the cache
    throw new Error("Method not implemented.");
  }
  flushAllHits(): Promise<void> {
    // remove all hits from the cache
    throw new Error("Method not implemented.");
  }
}

class VisitorCache implements IVisitorCacheImplementation{
    cacheVisitor(visitorId: string, Data: VisitorCacheDTO): Promise<void> {
        // store visitor data in the cache
        throw new Error("Method not implemented.");
    }
    lookupVisitor(visitorId: string): Promise<VisitorCacheDTO> {
        // retrieve visitor data from the cache
        throw new Error("Method not implemented.");
    }
    flushVisitor(visitorId: string): Promise<void> {
        // remove visitor data from the cache
        throw new Error("Method not implemented.");
    }
    
}

const App = () => (
  <>
    <FlagshipProvider
      envId="YOUR_ENV_ID"
      apiKey="YOUR_API_KEY"
      visitorData={null}
			hitCacheImplementation={new HitCache()},
      visitorCacheImplementation={new VisitorCache()},
    >
      {/* ... */}
    </FlagshipProvider>
  </>
);
use Flagship\Flagship;
use Flagship\Cache\IVisitorCacheImplementation;
use Flagship\Config\FlagshipConfig;

class VisitorCache implements IVisitorCacheImplementation
{

    public function cacheVisitor(string $visitorId, array $data): void
    {
        // TODO: store visitor data in the cache
    }

    public function lookupVisitor(string $visitorId): array
    {
        // TODO: retrieve visitor data from the cache
    }

    public function flushVisitor(string $visitorId): void
    {
        // TODO: remove visitor data from the cache
    }
}

class HitCache implements IHitCacheImplementation
{
    public function cacheHit(array $hits): void
    {
        // TODO: store hits in the cache
    }

    public function lookupHits(): array
    {
        // TODO: retrieve hits from the cache
    }

    public function flushHits(array $hitKeys): void
    {
        // TODO: remove hits from the cache
    }

    public function flushAllHits(): void
    {
        // TODO: remove all hits from the cache
    }
}

$config = FlagshipConfig::decisionApi()
    ->setLogLevel(LogLevel::DEBUG)
    ->setTimeout(2000)
    ->setVisitorCacheImplementation(new VisitorCache())
    ->setHitCacheImplementation(new HitCache());

Flagship::start("<ENV_ID>", "<API_KEY>", $config);