2 ms·
This approach makes caching no longer invisible to the caller. Moreover, all callers, rather than the class, have complete control over the cache activation and
by vog 16y ago
This approach makes caching no longer invisible to the caller. Moreover, all callers, rather than the class, have complete control over the cache activation and expiration. Also, the caller needs to know which methods can safely be cached at all.
So for the whole application, this approach will probably cause more brittle code than caching "by hand", where caching was at least hidden behind well-defined interfaces.
Nevertheless, I think that meta-class magic is the right way to abstract away this caching code duplication. It just shouldn't change class interfaces.
For example, there could be a static attribute that contains the names of all methods that should be cached. And there could be an additional method for invalidating caches, probably with an array argument to facilitate invalidation multiple method caches at once. And, of course, this cache invalidation should mainly be used by the class itself, and ideally not be available to the caller at all.