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);
using Flagship.Cache;
using Flagship.Config;
using Flagship.Main;
using Newtonsoft.Json.Linq;
class VisitorCache : IVisitorCacheImplementation
{
public TimeSpan? LookupTimeout { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public Task CacheVisitor(string visitorId, JObject data)
{
// TODO: store visitor data in the cache
throw new NotImplementedException();
}
public Task FlushVisitor(string visitorId)
{
// TODO: retrieve visitor data from the cache
throw new NotImplementedException();
}
public Task<JObject> LookupVisitor(string visitorId)
{
// TODO: remove visitor data from the cache
throw new NotImplementedException();
}
}
class HitCache : IHitCacheImplementation
{
public TimeSpan? LookupTimeout { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public Task CacheHit(JObject data)
{
// TODO: store hits in the cache
throw new NotImplementedException();
}
public Task FlushAllHits()
{
// TODO: retrieve hits from the cache
throw new NotImplementedException();
}
public Task FlushHits(string[] hitKeys)
{
// TODO: remove hits from the cache
throw new NotImplementedException();
}
public Task<JObject> LookupHits()
{
// TODO: remove all hits from the cache
throw new NotImplementedException();
}
}
Fs.Start("<ENV_ID>", "<API_KEY>", new DecisionApiConfig(){
LogLevel = Flagship.Enums.LogLevel.DEBUG,
VisitorCacheImplementation = new VisitorCache(),
HitCacheImplementation = new HitCache()
});
Updated 4 months ago