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
Initiates the asynchronous processing and synchronization loops for the cache. This method must be called to begin the auto-refresh functionality.
Parameters
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing cancellation and timeouts of the background operations. |
Returns
| Type | Description |
|---|
error | An error if the startup process fails. |
enqueueLoop()
@classmethod
def enqueueLoop(
ctx: context.Context
)
Runs a continuous loop to process items from the workqueue. This method is responsible for dequeuing items and initiating their processing.
Parameters
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing cancellation and timeouts of the enqueue loop. |
Update()
@classmethod
def Update(
id: 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 | 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 bypasses the delayed deletion mechanism.
Parameters
| Name | Type | Description |
|---|
| key | interface{} | The key of the item to be deleted from the cache. |
Get()
@classmethod
def Get(
id: ItemID
) - > ([Item](item.md?sid=flytestdlib_cache_item), error)
Retrieves an item from the cache using its unique identifier. This method provides immediate access to cached items.
Parameters
| Name | Type | Description |
|---|
| id | ItemID | The unique identifier of the item to retrieve. |
Returns
| Type | Description |
|---|
([Item](item.md?sid=flytestdlib_cache_item), error) | The requested item and an error if the item is not found or an issue occurs during retrieval. |
GetOrCreate()
@classmethod
def GetOrCreate(
id: 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 an item is always present after this call.
Parameters
| Name | Type | Description |
|---|
| id | 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 existing item is found for the given ID. |
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
) - > error
Marks an item for delayed deletion from the cache. The actual removal will occur during a subsequent synchronization cycle.
Parameters
| Name | Type | Description |
|---|
| id | ItemID | The unique identifier of the item to be marked for delayed deletion. |
Returns
| Type | Description |
|---|
error | An error if the item cannot be marked for delayed deletion. |
enqueueBatches()
@classmethod
def enqueueBatches(
ctx: context.Context
) - > error
Enqueues batches of items for processing. This method is typically called internally to add items to the workqueue for asynchronous handling.
Parameters
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing cancellation and timeouts during batch enqueuing. |
Returns
| Type | Description |
|---|
error | An error if there is an issue enqueuing the batches. |
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 ensures cache consistency.
Parameters
| Name | Type | Description |
|---|
| ctx | context.Context | The context for managing cancellation and timeouts of the synchronization operation. |
Returns
| Type | Description |
|---|
error | An error if the synchronization process encounters an issue. |
inProcessing()
@classmethod
def inProcessing(
key: interface{}
) - > bool
Checks if a given key is currently being processed by the cache's background workers. This prevents duplicate processing of the same item.
Parameters
| Name | Type | Description |
|---|
| key | interface{} | The key of the item to check for its processing status. |
Returns
| Type | Description |
|---|
bool | A boolean indicating whether the item associated with the key is currently in the processing queue (true) or not (false). |