No, one either uses a cache or doesn't.
In your application, one would determine whether caching was necessary. I would inject caching as a decorator pattern.
Consider this interface.
public interface ICommand<in TIn, out TOut>
{
TOut Execute(TIn command);
}
We pass something in and get something back out. Maybe to get something takes a long time. So, we decide to add caching.
public class CacheDecorator<TIn, TOut> : ICommand<TIn, TOut>
{
private readonly ICache _cache;
private readonly ICommand<TIn, TOut> _command;
private readonly string _key;
public CacheDecorator(string key, ICache cache, ICommand<TIn, TOut> command)
{
_command = command;
_key = key;
_cache = cache;
}
public TOut Execute(TIn input)
{
TOut obj;
if (_cache.Get(_key, out obj))
{
Debug.WriteLine(string.Format("Cache hit for key {0}", _key));
return obj;
}
Debug.WriteLine(string.Format("Cache miss for key {0}", _key));
obj = _command.Execute(input);
_cache.Set(_key, obj);
return obj;
}
}
Now any command that get's something back can be cached. If we don't need caching then the command is not decorated.
Let's pretend we are getting a widget and getting a widget is expensive:
public Widget GetWidget(string id)
{
var widgetCommand = new WidgetServiceCommand();
var command = new CacheDecorator<string, Widget>(id, _cache, widgetCommand);
return command.Execute(id);
}
If getting widgets was not expensive, then just remove the decorator and the widget is not cached.