ContainerHelper.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using Senparc.Weixin.Containers;
  19. namespace Senparc.Weixin.Helpers
  20. {
  21. /// <summary>
  22. /// 容器帮助类
  23. /// </summary>
  24. public class ContainerHelper
  25. {
  26. /// <summary>
  27. /// 获取缓存Key(命名空间,形如:Container:Senparc.Weixin.MP.Containers.AccessTokenBag)
  28. /// </summary>
  29. /// <returns></returns>
  30. public static string GetCacheKeyNamespace(Type bagType)
  31. {
  32. return string.Format("Container:{0}", bagType);
  33. }
  34. /// <summary>
  35. /// 获取ContainerBag缓存Key,包含命名空间(形如:Container:Senparc.Weixin.MP.Containers.AccessTokenBag:wx669ef95216eef885)
  36. /// </summary>
  37. /// <param name="shortKey">最简短的Key,比如AppId,不需要考虑容器前缀</param>
  38. /// <returns></returns>
  39. public static string GetItemCacheKey(Type bagType, string shortKey)
  40. {
  41. return string.Format("{0}:{1}", GetCacheKeyNamespace(bagType), shortKey);
  42. }
  43. /// <summary>
  44. /// 获取ContainerBag缓存Key,包含命名空间(形如:Container:Senparc.Weixin.MP.Containers.AccessTokenBag:wx669ef95216eef885)
  45. /// </summary>
  46. /// <typeparam name="T"></typeparam>
  47. /// <param name="bag"></param>
  48. /// <returns></returns>
  49. public static string GetItemCacheKey(IBaseContainerBag bag)
  50. {
  51. return GetItemCacheKey(bag, bag.Key);
  52. }
  53. /// <summary>
  54. /// 获取ContainerBag缓存Key,包含命名空间(形如:Container:Senparc.Weixin.MP.Containers.AccessTokenBag:wx669ef95216eef885)
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="bag"></param>
  58. /// <param name="shortKey"></param>
  59. /// <returns></returns>
  60. public static string GetItemCacheKey(IBaseContainerBag bag, string shortKey)// where T : IBaseContainerBag
  61. {
  62. return GetItemCacheKey(bag.GetType(), shortKey);
  63. }
  64. }
  65. }