Skip to content

Instantly share code, notes, and snippets.

@eldewall
Last active December 18, 2015 15:19
Show Gist options
  • Save eldewall/5803739 to your computer and use it in GitHub Desktop.
Save eldewall/5803739 to your computer and use it in GitHub Desktop.
Cached Reader decorator
public class AsdController : BaseController {
private IReader<Encrypted, SalesObject> Reader { get; private set; }
public AsdController(IReader<Encrypted, SalesObject> reader) {
Reader = reader;
if (base.UseCaching) {
Reader = new CachedReader<Encrypted, SalesObject>(Reader);
}
}
[HttpGet]
public ActionResult Get(Encrypted id) {
var salesObject = Reader.get(id);
return View(salesObject);
}
}
public interface IReader<Key, Value> {
Value Get(Key key);
}
public interface ICache<Key, Value> {
Value Store(Key key, Func<Key, Value> getter);
}
public class CachedReader<Key, Value> : IReader<Key, Value> {
private IReader<Key, Value> _reader;
private ICache<Key, Value> _cache;
public CachedReader(IReader<Key, Value> reader) {
_reader = reader;
_cache = new ItemCache<Key, Value>();
}
public Value Get(Key key) {
return _cache.Store(key, _reader.Get);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment