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.
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.
<<__Memoize>> Only caches for the lifetime of the request and is not shared across requests. It's highly unlikely you have an HTTP request that lasts for more than a minute, so it sounds like you want a cache that is shared across multiple requests. APC is generally used for that (see http://php.net/apc). If you really want a more granular control of caching within a request you will need to roll your own using a static member or a global variable.