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
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing the lifecycle of the background goroutines, allowing for cancellation. |
Returns
| Type | Description |
|---|
error | An 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
| Name | Type | Description |
|---|
| ctx | context.Context | The 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
| Name | Type | Description |
|---|
| 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
| Type | Description |
|---|
bool | A 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
| Name | Type | Description |
|---|
| key | interface{} | 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
| Name | Type | Description |
|---|
| id | [ItemID](itemid.md?sid=flytestdlib_cache_itemid) | The unique identifier of the item to retrieve. |
Returns
| Type | Description |
|---|
[Item](item.md?sid=flytestdlib_cache_item), error | The 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
| Name | Type | Description |
|---|
| 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
| Type | Description |
|---|
[Item](item.md?sid=flytestdlib_cache_item), error | The 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
| Name | Type | Description |
|---|
| id | [ItemID](itemid.md?sid=flytestdlib_cache_itemid) | The unique identifier of the item to mark for delayed deletion. |
Returns
| Type | Description |
|---|
error | An 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
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing the operation's lifecycle, allowing for cancellation. |
Returns
| Type | Description |
|---|
error | An 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
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing the synchronization operation's lifecycle, allowing for cancellation. |
Returns
| Type | Description |
|---|
error | An 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
| Name | Type | Description |
|---|
| key | interface{} | The key to check for its presence in the processing set. |
Returns
| Type | Description |
|---|
bool | A boolean indicating whether the key is currently in the processing set (true) or not (false). |