UpdMemory.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IntelligentControlForsx.Code
  8. {
  9. public class UpdMemory<T>
  10. {
  11. private readonly static ConcurrentDictionary<string, T> _dic=new ConcurrentDictionary<string, T>();
  12. #region IDictionary<string,ResultType> 成员
  13. public void Add(string key, T value)
  14. {
  15. _dic.TryAdd(key, value);
  16. }
  17. public bool ContainsKey(string key)
  18. {
  19. return _dic.ContainsKey(key);
  20. }
  21. public void AddorUpdate(string key, T value)
  22. {
  23. if (ContainsKey(key))
  24. {
  25. this[key] = value;
  26. }
  27. else
  28. {
  29. _dic.TryAdd(key, value);
  30. }
  31. }
  32. public ICollection<string> Keys
  33. {
  34. get { return _dic.Keys; }
  35. }
  36. public bool Remove(string key)
  37. {
  38. T val;
  39. return _dic.TryRemove(key, out val);
  40. }
  41. public bool TryGetValue(string key, out T value)
  42. {
  43. return _dic.TryGetValue(key, out value);
  44. }
  45. public ICollection<T> Values
  46. {
  47. get { return _dic.Values; }
  48. }
  49. public T this[string key]
  50. {
  51. get
  52. {
  53. return _dic[key];
  54. }
  55. set
  56. {
  57. _dic[key] = value;
  58. }
  59. }
  60. #endregion
  61. }
  62. }