Proper way to lru cache a read call for data that may or may not have been written at the time of the call

35 Views Asked by At

I have a class that manages data. There is data written for each date. I have a reader function

read(self, for_date)

That is called frequently, and often times for the same dates, so I added an LRU cache decorator with a cache size 100 (the rolling window of the dates for calls is about 100 so this is appropriate). However, this cache scheme doesn't quite work for my use case.

The issue here is that we can have the following sequence:

# input_date has not yet been written and this function will return an empty list []
instance.read(for_date=input_date)

# write data for input date
instance.write(for_date=input_date)

# input_date has now been written and the result is no longer an empty list, but if I cached the first call, it would return an empty list here
instance.read(for_date=input_date)

One approach I've thought of is to maintain a last of dates that have been written and before calling read, check if the date has been written, i.e., create a wrapper function around read, and put the lru cache decorator on the read function and not on the wrapper. I don't quite like this approach because it involves having to keep track of which dates have been written.

What other approaches are viable here?

The implementation is in python, so I need a working solution for python, but I think the issue here has some element of language-agnosticism, hence the tag.

0

There are 0 best solutions below