Cache Manager Block
The Cache Manager block lets you interact with a cache system, such as Redis, to perform operations like retrieving data, setting distributed locks, incrementing or decrementing numeric values, and managing cache entries within your Neo dataflow. This block helps optimize dataflow performance by caching frequently accessed data, ensuring data consistency across distributed processes using locks, and managing temporary or transient states.
Example Scenario
Requirement
A high-traffic e-commerce platform displays product recommendations on its homepage. Generating these recommendations requires multiple database queries and machine learning model inferences, which are computationally intensive. To ensure fast page load times and reduce database load, the platform must cache recommendations for popular categories for a short period.
During flash sales, when a user adds a limited-stock item to their cart, the system must place a temporary lock on that item’s inventory count for that user. This lock prevents overselling while the user completes the checkout process.
Solution
To meet the performance and consistency requirements, you can create a Neo dataflow that uses the Cache Manager block for the two key tasks:
1. Caching Product Recommendations
- Use the Cache Manager block in
Get
mode to check if recommendations for a product category already exist in the cache. - If a cache miss occurs (no data found or expired), the dataflow computes the recommendations using downstream blocks (e.g., database queries or model inference).
- While the Cache Manager block currently does not support
Set
mode, the system generating the recommendations is responsible for writing them back to the cache with a suitable TTL (time to live). This ensures that future requests can fetch recommendations directly from the cache, improving page load times and reducing database and compute load.
2. Locking Inventory During Flash Sales
- You can use the Cache Manager block to store a temporary lock for an item’s inventory when a user adds it to their cart.
- The lock entry includes user and item identifiers, with a short TTL to automatically release the lock if the user does not complete the checkout.
- When the user proceeds to checkout, the dataflow can check the cache to validate the lock before finalising the order.
Configuring the CacheManager Block
To configure the Cache Manager block:
-
From the dataflow canvas, click the node where you want to add caching logic, and select the Cache Manager block from the list of available executors.
-
In the Block name field, enter a descriptive name for the block.
Note: Block names cannot contain spaces or special characters, except for underscores (_
). Use camelCase or snake_case formatting. -
In the Operation Mode field, choose the cache operation to perform.
The following operations are available:Operation Description Get
(Retrieve from Cache)Retrieves an item from the cache using the specified key. If the cached item was originally the output of a Neo block, specifically a BlockDto
(Block Data Transfer Object), the system correctly reconstructs it. Returnsnull
if the key is not found or if an error occurs.Lock
(Acquire Distributed Lock)Attempts to acquire a distributed lock for a specific key. This is useful for synchronizing operations on shared resources and preventing race conditions. Incr
(Increment Value)Increments the numeric value associated with a key. If the key does not exist, it is initialized to 0
before the increment operation. A Time-To-Live (TTL) can also be set.Decr
(Decrement Value)Decrements the numeric value associated with a key. If the key does not exist, it is initialized to 0
before the decrement operation. A Time-To-Live (TTL) can also be set.RemoveKey
(Delete from Cache)Deletes a key and its associated value from the cache. ReleaseLock
(Release Distributed Lock)Releases a previously acquired distributed lock. Always release locks when they are no longer needed to free up resources. Set The Set
operation is currently not supported in the Cache Manager block. You must write to the cache using an external system or custom logic.
-
In the Cache Key Expression field, enter a JavaScript expression that evaluates to the final cache key string. This key uniquely identifies the data or lock in the cache.
Example:
To create a user-specific cache key, use:"user_profile_" + input.body.userId
-
Configure the input execution logic.
-
Depending on the selected Operation Mode, additional fields become available:
-
If the Operation Mode is
Lock
, the following fields are available. Both fields are optional.Field Name Description Default Constraints Lock Duration (seconds) Specifies the duration, in seconds, to hold the lock. If the lock isn't explicitly released, it automatically expires after this period. 20 seconds Must be a positive number between 1
and20
(inclusive). If an invalid value is provided, the default is used.Lock Acquisition Timeout (seconds) Specifies the maximum time, in seconds, for the block to try acquiring the lock if another process is holding it. If the lock isn’t acquired within this time, the attempt fails. 1 Must be a positive number between 1
and20
(inclusive). -
If the Operation Mode is
Incr
orDecr
, the following fields are available. Both are optional.Field Name Description Default Constraints Amount Specifies the numeric value to increment or decrement the cached key. 1 Must be a valid number. Value TTL (seconds) Specifies the time-to-live (TTL), in seconds, for the key when its value is set or updated by the increment or decrement operation. 3600 Must be a positive number.
-
-
Specify the execution path, if needed.
-
Click Done.
Updated 2 days ago