Skip to main content

InMemoryAutoRefresh

InMemoryAutoRefresh is an in-memory implementation of the AutoRefresh interface. It is a thread-safe general purpose auto-refresh cache that watches for updates asynchronously for the keys after they are added to the cache. An item can be inserted only once.

Methods


Start()

@classmethod
def Start(
ctx: context.Context
) - > error

Starts the auto-refresh process, initializing background goroutines for enqueueing and syncing items. This method should be called once to begin the cache's operation.

Parameters

NameTypeDescription
ctxcontext.ContextThe context for managing the lifecycle of the background goroutines, allowing for cancellation.

Returns

TypeDescription
errorAn error if the start operation fails, otherwise nil.

enqueueLoop()

@classmethod
def enqueueLoop(
ctx: context.Context
)

Continuously processes items from the workqueue, creating and enqueueing batches for asynchronous processing. This is a background goroutine that runs indefinitely until the context is cancelled.

Parameters

NameTypeDescription
ctxcontext.ContextThe context for managing the lifecycle of the loop, allowing for cancellation.

Update()

@classmethod
def Update(
id: [ItemID](itemid.md?sid=flytestdlib_cache_itemid),
item: [Item](item.md?sid=flytestdlib_cache_item)
) - > bool

Updates an existing item in the cache with a new value. If the item does not exist, it will not be added.

Parameters

NameTypeDescription
id[ItemID](itemid.md?sid=flytestdlib_cache_itemid)The unique identifier of the item to update.
item[Item](item.md?sid=flytestdlib_cache_item)The new item value to associate with the given ID.

Returns

TypeDescription
boolA boolean indicating whether the update was successful (true) or if the item was not found (false).

Delete()

@classmethod
def Delete(
key: interface{}
)

Removes an item from the cache immediately. This operation is synchronous and directly modifies the cache.

Parameters

NameTypeDescription
keyinterface{}The key of the item to delete from the cache.

Get()

@classmethod
def Get(
id: [ItemID](itemid.md?sid=flytestdlib_cache_itemid)
) - > [Item](item.md?sid=flytestdlib_cache_item), error

Retrieves an item from the cache using its ID. This is a synchronous read operation.

Parameters

NameTypeDescription
id[ItemID](itemid.md?sid=flytestdlib_cache_itemid)The unique identifier of the item to retrieve.

Returns

TypeDescription
[Item](item.md?sid=flytestdlib_cache_item), errorThe item associated with the ID and an error if the item is not found or an issue occurs.

GetOrCreate()

@classmethod
def GetOrCreate(
id: [ItemID](itemid.md?sid=flytestdlib_cache_itemid),
item: [Item](item.md?sid=flytestdlib_cache_item)
) - > [Item](item.md?sid=flytestdlib_cache_item), error

Retrieves an item from the cache if it exists, otherwise it creates and stores the provided item. This ensures that an item is always present after this call.

Parameters

NameTypeDescription
id[ItemID](itemid.md?sid=flytestdlib_cache_itemid)The unique identifier of the item to retrieve or create.
item[Item](item.md?sid=flytestdlib_cache_item)The item to create if no item with the given ID exists in the cache.

Returns

TypeDescription
[Item](item.md?sid=flytestdlib_cache_item), errorThe existing or newly created item, and an error if creation fails.

DeleteDelayed()

@classmethod
def DeleteDelayed(
id: [ItemID](itemid.md?sid=flytestdlib_cache_itemid)
) - > error

Marks an item for delayed deletion, adding it to a set of items to be removed during a future sync cycle. This allows for eventual consistency in deletion.

Parameters

NameTypeDescription
id[ItemID](itemid.md?sid=flytestdlib_cache_itemid)The unique identifier of the item to mark for delayed deletion.

Returns

TypeDescription
errorAn error if the item cannot be marked for delayed deletion, otherwise nil.

enqueueBatches()

@classmethod
def enqueueBatches(
ctx: context.Context
) - > error

Processes a batch of items, typically by calling a user-defined callback to create new batches and adding them to the workqueue. This method is part of the asynchronous refresh mechanism.

Parameters

NameTypeDescription
ctxcontext.ContextThe context for managing the operation's lifecycle, allowing for cancellation.

Returns

TypeDescription
errorAn error if batch creation or enqueueing fails, otherwise nil.

sync()

@classmethod
def sync(
ctx: context.Context
) - > error

Performs a synchronization operation, refreshing the cache with the latest data and processing any pending deletions. This method is typically run periodically as a scheduled task.

Parameters

NameTypeDescription
ctxcontext.ContextThe context for managing the synchronization operation's lifecycle, allowing for cancellation.

Returns

TypeDescription
errorAn error if the synchronization process encounters an issue, otherwise nil.

inProcessing()

@classmethod
def inProcessing(
key: interface{}
) - > bool

Checks if a given key is currently being processed by the cache's background workers. This prevents multiple workers from processing the same item concurrently.

Parameters

NameTypeDescription
keyinterface{}The key to check for its presence in the processing set.

Returns

TypeDescription
boolA boolean indicating whether the key is currently in the processing set (true) or not (false).