I am writing an Excel addin using ExcelDNA and creating a UDF(User-Defined Function).
The UDF establishes a connection to a server and that server connects to an SQL Database to retrieve data and send it back to the client.
However, this UDF will be expected to be called hundreds of times in one worksheet.
I am trying to find a way to reduce the number of requests to the server.
As of this moment, this is how the template looks like (simplified):
ITEM | JAN 2016 | FEB 2016 | MAR 2016 |
---------------------------------------------------
ITEM 1 | UDF | UDF | UDF |
ITEM 2 | UDF | UDF | UDF |
ITEM 3 | UDF | UDF | UDF |
ITEM 4 | UDF | UDF | UDF |
ITEM 5 | UDF | UDF | UDF |
ITEM 6 | UDF | UDF | UDF |
So, right now, this would send 18 requests in total.
I would like to try to reduce the number of requests to 6.
The way I am thinking of doing that right now is that for each item, whichever request gets sent first will get the data for all the months listed of that item. Then the 2nd request for the same item would just wait for the result of the first request.
For example:
For ITEM 1, If the UDF in the JAN 2016 column gets called first, it will request data for all the months of ITEM 1 and then store that data somewhere for the other requests to take from.
Then when the 2nd request comes (let's say MAR 2016 is next), that UDF will wait for the result of the UDF of JAN 2016. This way, the UDF of MAR 2016 will not have to start another connection to the server.
However, I have no idea how to implement this. I have heard of the keywords await and async, but have no experience with using them.
Any other solutions to the problem are also welcome.