Cache component holds data in an in-memory lookup table as a set of name value pairs, identified by one or more designated key name value pairs. A set of key fields and data fields are defined in the CPS of the component. The component stores values for the defined fields as Name Value pairs, where name is the name of the field and value is the data held in the field. A field can be a key field or a data field.
The component support the following operations
- lookup – returns all the data fields that are mapped for a given key field from the in-memory lookup table.
- add – adds a set of mappings from key field to data fields to the in-memory lookup table.
- update – updates a set of mappings that are defined for a key field in the in-memory lookup table.
- delete – deletes a set of mappings that are defined for a key field in the in-memory lookup table.
The component has the following ports:
- ADD_PORT – Input port ADD_PORT is used to send input request to perform add, update or lookup operations.
- DEL_PORT – Input port DEL_PORT is used to send input request to perform delete operations.
- OUT_PORT – Output port OUT_PORT is used to read output request that contains results of add, lookup, update or delete operations.
Configuration and Testing
Figure 1: CPS of Cache component
Attributes
Field Definition Table
Click to launch Field Definition Table, the editor to define fields.
Figure 2: Editor to define fields in lookup table
The Field Definition Table contains Name, Type and Key fields.
- Name: Name of the field.
- Type: Data type of the field. Data type field contains the following field values – int, long, short, float, double, boolean, string and date.
- Key: Specifies whether the current field is a Key or a Data field.
To define field definitions, perform the following actions:- Click Add button to add a new field definition.
- Edit the value in Name column of the newly added row and provide the name of the field.
- Select the data type of field value from the drop-down list in Type column.
- Select the check box in Key column, if the field is a key field.
- Repeat steps 1 to 4 to add all field definitions.
- Click Ok.
- XSD: Xml schema can be specified only if data type is XML.
Cache threshold
This property determines the maximum number of entries that are held in the lookup table. If the number of entries in the lookup table exceeds the value defined by this property, oldest entries are discarded based on the property Criteria to remove entry.
Criteria to remove entry
When a threshold is defined, this property determines which entry should be removed after the threshold is reached. Figure 3 shows all the available criteria.
Figure 3: Criteria for removing entries after the threshold is reached
- Least recently added - The oldest entry in the lookup table will be removed first irrespective of the frequency at which an entry is used/updated.
- Least recently updated - The entry which has been in the lookup table for the longest duration without being updated will be removed.
- Least recently accessed – The entry has been in the lookup table for the longest time without being looked up or updated for the longest duration will be removed first.
Initial cache size
Initial size with which lookup table has to be initialized. The table will be resized depending on the underlying implementation. Providing an appropriate initial size will reduce the number of resizes.
Validate input
This property determines whether the input message has to be validated against the schema defined on the input ports.
- If enabled, input messages are validated against the schema defined on the input port on which the message is received.
- If disabled, input messages are not validated.
Input and Output
The schema for input and output are auto generated based on the field definitions provided in the CPS.
Input
The structure of messages on ADD_PORT for the field definitions shown in Figure 2 is shown in Figure 4.
Figure 4: Structure of messages on ADD_PORT
When a message is received on the ADD_PORT an entry is added, updated or looked up from lookup table based on the elements defined in the input message.
- An add operation is performed if Data element is present under CacheEntry element and at least one of the data fields is present and the value defined for a key field is not already present in the lookup table.
- An update operation is performed if Data element is present under CacheEntry element and at least one of the data fields is present and the value defined for a key field is already present in the lookup table.
- A lookup operation is performed if Data element is not present under CacheEntry element or none of the data fields are present.
- A lookup of all entries is performed if All element is present as defined in the structure.
The structure of messages on DEL_PORT for the field definitions shown in Figure 2 is shown in Figure 5.
Figure 5: Structure of messages on DEL_PORT
Output
Figure 6 contains the structure of messages on OUT_PORT for the field definitions shown in Figure 2.
Figure 6: Structure of messages on OUT_PORT
Functional Demonstration
Scenario 1
This scenario demonstrates the functioning of the Cache component. Perform the steps shown in this scenario in strict order.
Configure the Cache component as shown in Figure 2 and create a flow as shown in Figure 7. Output port of AddUpdateLookupRequest should be connected to ADD_PORT and output port of DeleteRequest should be connected to DEL_PORT.
Figure 7: Sample flow to demonstrate scenario 1
Add operation
Send the input shown in Figure 8 from Feeder AddUpdateLookupRequest to add the following entries to lookup table.
- ssn:ssn1234 → location:New York, EmployeeName:John Williams, ...
- emp_id:9876 → location:New York, EmployeeName:John Williams, ...
Where an entry is in form <key field name>:<key field value> → <data field name>:<data field value>, <data field name>:<data field value>....
Figure 8: Input for add operation
The output message contains all the entries that are added as shown in Figure 9.
Figure 9: Result of add operation
Update operation
Send the input shown in Figure 10 from Feeder AddUpdateLookupRequest to update the name field for the entry with key field ssn whose value is ssn1234.
Figure 10: Input for update operation
The output message contains the updated entry as shown in Figure 11.
Figure 11: Result of update operation
Entries in the database after the update operation are:
- ssn:ssn1234 → location:London, EmployeeName:John Williams, ...
- emp_id:9876 → location:London, EmployeeName:John Williams, ...
Lookup operation
Send the input shown in Figure 13 from Feeder AddUpdateLookupRequest to lookup the name and age fields for the entry with key field ssn whose value is ssn1234.
Figure 13: Input for lookup operation
The output message contains the entries looked up from lookup table as shown in Figure 14.
Figure 14: Result of lookup operation
To lookup all the entries in the lookup send, the input shown in Figure 15 from AddUpdateLookupRequest.
Figure 15: Input for lookup of all entries
The output containing all the entries in the lookup table is shown in Figure 16.
Figure 16: Output of lookup of all entries
Delete operation
Send the input shown in Figure 17 from Feeder DeleteRequest to delete the entry with key field ssn whose value is ssn1234.
Figure 17: Input for delete operation
The output message contains the entries that are deleted from lookup table as shown in Figure 18.
Figure 18: Output for delete operation
Send the input shown in Figure 18 from Feeder DeleteRequest to delete all entries in lookup table.
Figure 19: Input for deleting all entries
The output message contains the entries that are deleted from lookup table as shown in Figure 20. Since there is only undeleted entry in the database the output message contains only one entry.
Figure 20: Result of delete all entries operation
Useful Tips
- Size of the lookup table should be within in the memory limit available for the component.
- The lookup table should always be populated after the component starts as the lookup table is not persisted implicitly.
- Use this component in conjunction with a DB component to improve the performance of lookups and updates.
- Lookup can be performed using any of key fields which are part of add request.
- Update to any key field also effects the lookup data returned by other key fields which are part of add request.
- Disable input message validation to improve the performance of the component.
- Send All element in the input request on ADD_PORT to retrieve all the entries from the lookup table
- Send All element in the input request on DEL_PORT to delete all the entries from the lookup table
- When performing multiple add, update or lookup operation in a single input request on ADD_PORT the output contains corresponding result for each of the operation based on the state of lookup table when the operation is performed. If operations are performed in the order - add, lookup, add, lookup – the first lookup will contain only entry but the second lookup contain both the entries.