BaseContainerBag.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:BaseContainerBag.cs
  17. 文件功能描述:微信容器接口中的封装Value(如Ticket、AccessToken等数据集合)
  18. 创建标识:Senparc - 20151003
  19. 修改标识:Senparc - 20160717
  20. 修改描述:v4.5.21 添加BaseContainerBag.Name属性
  21. 修改标识:Senparc - 20180606
  22. 修改描述:缓存工厂重命名为 ContainerCacheStrategyFactory
  23. 修改标识:Senparc - 20180614
  24. 修改描述:CO2NET v0.1.0 ContainerBag 取消属性变动通知机制,使用手动更新缓存
  25. 修改标识:Senparc - 20170522
  26. 修改描述:v6.3.2 修改 DateTime 为 DateTimeOffset
  27. ----------------------------------------------------------------*/
  28. using System;
  29. using System.Runtime.CompilerServices;
  30. using Senparc.CO2NET.MessageQueue;
  31. using Senparc.Weixin.Annotations;
  32. using Senparc.Weixin.Cache;
  33. using Senparc.Weixin.Entities;
  34. using Senparc.Weixin.Helpers;
  35. namespace Senparc.Weixin.Containers
  36. {
  37. /// <summary>
  38. /// IBaseContainerBag,BaseContainer容器中的Value类型
  39. /// </summary>
  40. public interface IBaseContainerBag
  41. {
  42. /// <summary>
  43. /// 用于标记,方便后台管理
  44. /// </summary>
  45. string Name { get; set; }
  46. /// <summary>
  47. /// 缓存键,形如:wx669ef95216eef885,最底层的Key,不考虑命名空间等
  48. /// </summary>
  49. string Key { get; set; }
  50. /// <summary>
  51. /// 当前对象被缓存的时间
  52. /// </summary>
  53. DateTimeOffset CacheTime { get; set; }
  54. }
  55. /// <summary>
  56. /// 提供给具有 AppId 的 IBaseContainerBag 使用的接口
  57. /// </summary>
  58. public interface IBaseContainerBag_AppId
  59. {
  60. /// <summary>
  61. /// AppId
  62. /// </summary>
  63. string AppId { get; set; }
  64. }
  65. /// <summary>
  66. /// BaseContainer容器中的Value类型
  67. /// </summary>
  68. [Serializable]
  69. public class BaseContainerBag : /*BindableBase, */IBaseContainerBag
  70. {
  71. //private string _key;
  72. //private string _name;
  73. /// <summary>
  74. /// 用于标记,方便后台管理
  75. /// </summary>
  76. public string Name { get; set; }
  77. // {
  78. // get { return _name; }
  79. //#if NET35 || NET40
  80. // set { this.SetContainerProperty(ref _name, value, "Name"); }
  81. //#else
  82. // set { this.SetContainerProperty(ref _name, value); }
  83. //#endif
  84. //}
  85. /// <summary>
  86. /// 通常为AppId
  87. /// </summary>
  88. public string Key { get; set; }
  89. // {
  90. // get { return _key; }
  91. //#if NET35 || NET40
  92. // set { this.SetContainerProperty(ref _key, value, "Key"); }
  93. //#else
  94. // set { this.SetContainerProperty(ref _key, value); }
  95. //#endif
  96. //}
  97. /// <summary>
  98. /// 缓存时间,不使用属性变化监听
  99. /// </summary>
  100. public DateTimeOffset CacheTime { get; set; }
  101. // private void BaseContainerBag_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  102. // {
  103. // var containerBag = (IBaseContainerBag)sender;
  104. // var mqKey = SenparcMessageQueue.GenerateKey("ContainerBag", sender.GetType(), containerBag.Key, "UpdateContainerBag");
  105. // //获取对应Container的缓存相关
  106. // //加入消息队列,每过一段时间进行自动更新,防止属性连续被编辑,短时间内反复更新缓存。
  107. // SenparcMessageQueue mq = new SenparcMessageQueue();
  108. // mq.Add(mqKey, () =>
  109. // {
  110. // //var containerCacheStrategy = CacheStrategyFactory.GetContainerCacheStrategyInstance();
  111. // var containerCacheStrategy = ContainerCacheStrategyFactory.GetContainerCacheStrategyInstance()/*.ContainerCacheStrategy*/;
  112. // var itemCacheKey = ContainerHelper.GetItemCacheKey(containerBag);
  113. // containerBag.CacheTime = DateTimeOffset.Now;//记录缓存时间
  114. // //cacheKey形如:Container:Senparc.Weixin.MP.Containers.AccessTokenBag:wx669ef95216eef885
  115. // containerCacheStrategy.UpdateContainerBag(itemCacheKey, containerBag);
  116. // });
  117. // }
  118. // /// <summary>
  119. // /// 设置Container属性
  120. // /// </summary>
  121. // /// <typeparam name="T"></typeparam>
  122. // /// <param name="storage"></param>
  123. // /// <param name="value"></param>
  124. // /// <param name="propertyName"></param>
  125. // /// <returns></returns>
  126. //#if NET35 || NET40
  127. // protected bool SetContainerProperty<T>(ref T storage, T value, String propertyName)
  128. //#else
  129. // protected bool SetContainerProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
  130. //#endif
  131. // {
  132. // var result = base.SetProperty(ref storage, value, propertyName);
  133. // return result;
  134. // }
  135. public BaseContainerBag()
  136. {
  137. //base.PropertyChanged += BaseContainerBag_PropertyChanged;
  138. }
  139. }
  140. }