Search code examples
phphacklang

Memoize attribute in the Hack language - cache timeout


Hack has the <<__Memoize>> attribute to easily cache method results.

How can I use it to cache results of some database or API request for a limited amount of time?

Let's say my code very frequently needs some information from a database:

public function loadEmployees(
  string $company_name,
): ImmSet<string> {
  return $this->db->sqlQuery(...);
}

To improve performance, I'd like to cache the results for one minute.

If the data changes my program should see it within a minute. I'm fine with the results being stale for one minute.

Checked the official docs.


Solution

  • Memoize only lives in the context of a request. Your request takes more than one minute? If that is the case, Memoize is not for your use case.