I have the following problem:
I have a API getSimilarTransactions which gets a Transaction:
{
"name": "zvi",
"email": "zvi@gmail.com",
"amount": 100,
...
}
And returns a list of transactions that have similar (name, email) from the persistence database.
This operation takes ~40seconds which is a lot of time.
I also know that a lot of the API requests are pretty similar (same id, email) , and they're a lot of API requests per minute.
I want to create memorize caching with the following logic:
- If i did already call the database and got a response for
(id, email)in the last30 secondsreturn this answer - otherwise, make a call to the database and update the cache (call to the database returns
Future[List[Transaction]], its also can fail - in that case i do NOT want to update the cache with an error.
I want to solve my problem with memorize, something as follows, but asynchronously with timeout expiration for cache key
def memoize[I,O](f: I => O) = new mutable.HashMap[I, O]() {
override def apply(x: I) = getOrElseUpdate(x, f(x))
}
Database call:
transactionsDao.getSimilarTransaction(id: String, email: String): Future[List[Transaction]]
Any ideas on how to solve it?