Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Consent1Cache
- {
- private static MemoryCache _cache = new MemoryCache("ConsentType1Cache");
- public static object GetItem(string key)
- {
- return AddOrGetExisting(key, () => InitItem(key));
- }
- private static T AddOrGetExisting<T>(string key, Func<T> valueFactory)
- {
- var newValue = new Lazy<T>(valueFactory);
- var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy<T>;
- try
- {
- return (oldValue ?? newValue).Value;
- }
- catch
- {
- // Handle cached lazy exception by evicting from cache.
- _cache.Remove(key);
- throw;
- }
- }
- public static string InitItem(string consentNumber)
- {
- using (DLL.ConsentDataContext db = new DLL.ConsentDataContext())
- {
- return db.vw_ConsentType1.Where(x => x.ConsentNumber == consentNumber).Select(c => c.ConsentNumber + " - " + c.Proposal).FirstOrDefault();
- };
- }
- }
- public static class Consent2Cache
- {
- private static MemoryCache _cache = new MemoryCache("ConsentType2Cache");
- public static object GetItem(string key)
- {
- return AddOrGetExisting(key, () => InitItem(key));
- }
- private static T AddOrGetExisting<T>(string key, Func<T> valueFactory)
- {
- var newValue = new Lazy<T>(valueFactory);
- var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy<T>;
- try
- {
- return (oldValue ?? newValue).Value;
- }
- catch
- {
- // Handle cached lazy exception by evicting from cache.
- _cache.Remove(key);
- throw;
- }
- }
- public static string InitItem(string consentNumber)
- {
- using (DLL.ConsentDataContext db = new DLL.ConsentDataContext())
- {
- return db.vw_ConsentType2.Where(x => x.ConsentNumber == consentNumber).Select(c => c.ConsentNumber + " - " + c.Proposal).FirstOrDefault();
- };
- }
- }
Add Comment
Please, Sign In to add comment