Guest User

Untitled

a guest
May 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. public static class Consent1Cache
  2. {
  3. private static MemoryCache _cache = new MemoryCache("ConsentType1Cache");
  4.  
  5. public static object GetItem(string key)
  6. {
  7. return AddOrGetExisting(key, () => InitItem(key));
  8. }
  9.  
  10. private static T AddOrGetExisting<T>(string key, Func<T> valueFactory)
  11. {
  12. var newValue = new Lazy<T>(valueFactory);
  13. var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy<T>;
  14. try
  15. {
  16. return (oldValue ?? newValue).Value;
  17. }
  18. catch
  19. {
  20. // Handle cached lazy exception by evicting from cache.
  21. _cache.Remove(key);
  22. throw;
  23. }
  24. }
  25.  
  26. public static string InitItem(string consentNumber)
  27. {
  28. using (DLL.ConsentDataContext db = new DLL.ConsentDataContext())
  29. {
  30. return db.vw_ConsentType1.Where(x => x.ConsentNumber == consentNumber).Select(c => c.ConsentNumber + " - " + c.Proposal).FirstOrDefault();
  31. };
  32. }
  33. }
  34.  
  35. public static class Consent2Cache
  36. {
  37. private static MemoryCache _cache = new MemoryCache("ConsentType2Cache");
  38.  
  39. public static object GetItem(string key)
  40. {
  41. return AddOrGetExisting(key, () => InitItem(key));
  42. }
  43.  
  44. private static T AddOrGetExisting<T>(string key, Func<T> valueFactory)
  45. {
  46. var newValue = new Lazy<T>(valueFactory);
  47. var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy<T>;
  48. try
  49. {
  50. return (oldValue ?? newValue).Value;
  51. }
  52. catch
  53. {
  54. // Handle cached lazy exception by evicting from cache.
  55. _cache.Remove(key);
  56. throw;
  57. }
  58. }
  59.  
  60. public static string InitItem(string consentNumber)
  61. {
  62. using (DLL.ConsentDataContext db = new DLL.ConsentDataContext())
  63. {
  64. return db.vw_ConsentType2.Where(x => x.ConsentNumber == consentNumber).Select(c => c.ConsentNumber + " - " + c.Proposal).FirstOrDefault();
  65. };
  66. }
  67. }
Add Comment
Please, Sign In to add comment