Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. public class CacheItem
  2. {
  3. public void ExpensiveLoad()
  4. {
  5. // some expensive code
  6. }
  7. }
  8. public class Cache
  9. {
  10. static object SynchObj = new object();
  11. static Dictionary<string, CacheItem> Cache = new Dictionary<string, CacheItem>();
  12. static volatile List<string> CacheKeys = new List<string>();
  13.  
  14. public CacheItem Get(string key)
  15. {
  16. List<string> keys = CacheKeys;
  17. if (!keys.Contains(key))
  18. {
  19. lock (SynchObj)
  20. {
  21. keys = CacheKeys;
  22. if (!keys.Contains(key))
  23. {
  24. CacheItem item = new CacheItem();
  25. item.ExpensiveLoad();
  26. Cache.Add(key, item);
  27. List<string> newKeys = new List<string>(CacheKeys);
  28. newKeys.Add(key);
  29. CacheKeys = newKeys;
  30. }
  31. }
  32. }
  33. return Cache[key];
  34. }
  35. }
  36.  
  37. return Cache[key];
  38.  
  39. _Cache.Add(key, item);
  40.  
  41. public CacheItem Get(string key)
  42. {
  43. lock (SynchObj)
  44. {
  45. CacheItem item;
  46. if (!Cache.TryGetValue(key, out item))
  47. {
  48. item = new CacheItem();
  49. item.ExpensiveLoad();
  50. Cache.Add(key, item);
  51. }
  52. return item;
  53. }
  54. }
  55.  
  56. static ConcurrentDictionary<string, CacheItem> Cache =
  57. new ConcurrentDictionary<string, CacheItem>(StringComparer.Ordinal);
  58. ...
  59. CacheItem item = Cache.GetOrAdd(key, key => ExpensiveLoad(key));
  60.  
  61. static ConcurrentDictionary<string, Lazy<CacheItem>> Cache =
  62. new ConcurrentDictionary<string, Lazy<CacheItem>>(StringComparer.Ordinal);
  63. ...
  64.  
  65. CacheItem item = Cache.GetOrAdd(key,
  66. new Lazy<CacheItem>(()=> ExpensiveLoad(key))
  67. ).Value;
  68.  
  69. public class CacheItem
  70. {
  71. public void ExpensiveLoad()
  72. {
  73. // some expensive code
  74. }
  75. }
  76. public class Cache
  77. {
  78. private static object _SynchObj = new object();
  79. private static Dictionary<string, CacheItem> _Cache = new Dictionary<string, CacheItem>();
  80. private static ReadOnlyCollection<string> _CacheKeysReadOnly = new ReadOnlyCollection(new List<string>());
  81.  
  82. public IList<string> CacheKeys
  83. {
  84. get
  85. {
  86. return _CacheKeysReadOnly;
  87. }
  88. }
  89.  
  90. public CacheItem Get(string key)
  91. {
  92. CacheItem item = null;
  93. ReadOnlyCollection<string> keys = _CacheKeysReadOnly;
  94. if (!keys.Contains(key))
  95. {
  96. lock (_SynchObj)
  97. {
  98. keys = _CacheKeysReadOnly;
  99. if (!keys.Contains(key))
  100. {
  101. item = new CacheItem();
  102. item.ExpensiveLoad();
  103. _Cache.Add(key, item);
  104. List<string> newKeys = new List<string>(_CacheKeysReadOnly);
  105. newKeys.Add(key);
  106. _CacheKeysReadOnly = newKeys.AsReadOnly();
  107. }
  108. }
  109. }
  110. return item;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement