Hi,
I've got a relatively expensive data-fetching operation that I want to cache the results of. This operation is called from const
methods, roughly like this:
double AdjustData(double d, int key) const {
double factor = LongRunningOperationToFetchFactor(key);
return factor * d;
}
I'd like AdjustData
to remain const
, but I want to cache out the factor so I only fetch it the first time. At present I'm using a mutable map<int, double>
to store the result (the map being from key
to factor
), but I'm thinking using a function-scoped static might be a better solution - this factor is only needed by this function, and is irrelevant to the rest of the class.
Does that seem like a good way to go? Are there any better options? What things might I think about, particularly with regard to thread-safety.
Thanks,
Dom