Blame view

美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/AdminWorkingScopeCache.cs 1.75 KB
49755ef0   李曜臣   6-12代码优化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  using Microsoft.Extensions.Caching.Distributed;
  using Volo.Abp.Caching;
  
  namespace FoodLabeling.Application.Helpers;
  
  /// <summary>
  /// 管理员(或级联选店流程)当前工作门店缓存项。
  /// </summary>
  public class AdminWorkingScopeCacheItem
  {
      public string LocationId { get; set; } = string.Empty;
  
      public string PartnerId { get; set; } = string.Empty;
  
      public string GroupId { get; set; } = string.Empty;
  }
  
  /// <summary>
  /// 按用户 Id 缓存当前选中的工作门店。
  /// </summary>
  public class AdminWorkingScopeCacheKey
  {
      public AdminWorkingScopeCacheKey(Guid userId)
      {
          UserId = userId;
      }
  
      public Guid UserId { get; }
  }
  
  /// <summary>
  /// 读写 <see cref="AdminWorkingScopeCacheItem"/>(选店后与 App <c>my-locations</c> 行为对齐)。
  /// </summary>
  public static class AdminWorkingScopeCacheHelper
  {
      private static readonly DistributedCacheEntryOptions DefaultOptions = new()
      {
          AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24)
      };
  
      public static async Task<AdminWorkingScopeCacheItem?> GetAsync(
          IDistributedCache<AdminWorkingScopeCacheItem, AdminWorkingScopeCacheKey> cache,
          Guid userId)
      {
          return await cache.GetAsync(new AdminWorkingScopeCacheKey(userId));
      }
  
      public static Task SetAsync(
          IDistributedCache<AdminWorkingScopeCacheItem, AdminWorkingScopeCacheKey> cache,
          Guid userId,
          AdminWorkingScopeCacheItem item) =>
          cache.SetAsync(new AdminWorkingScopeCacheKey(userId), item, DefaultOptions);
  
      public static Task RemoveAsync(
          IDistributedCache<AdminWorkingScopeCacheItem, AdminWorkingScopeCacheKey> cache,
          Guid userId) =>
          cache.RemoveAsync(new AdminWorkingScopeCacheKey(userId));
  }