Last active
December 18, 2015 15:19
-
-
Save eldewall/5803739 to your computer and use it in GitHub Desktop.
Cached Reader decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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