|
1 | 1 | namespace EasyCaching.Core.Interceptor
|
2 | 2 | {
|
3 |
| - using System; |
4 | 3 | using System.Collections.Generic;
|
5 | 4 | using System.Linq;
|
6 | 5 | using System.Reflection;
|
|
11 | 10 | /// </summary>
|
12 | 11 | public class DefaultEasyCachingKeyGenerator : IEasyCachingKeyGenerator
|
13 | 12 | {
|
14 |
| - /// <summary> |
15 |
| - /// The link char of cache key. |
16 |
| - /// </summary> |
17 |
| - private char _linkChar = ':'; |
| 13 | + private const char LinkChar = ':'; |
18 | 14 |
|
19 |
| - /// <summary> |
20 |
| - /// Gets the cache key. |
21 |
| - /// </summary> |
22 |
| - /// <returns>The cache key.</returns> |
23 |
| - /// <param name="methodInfo">Method info.</param> |
24 |
| - /// <param name="args">Arguments.</param> |
25 |
| - /// <param name="prefix">Prefix.</param> |
26 | 15 | public string GetCacheKey(MethodInfo methodInfo, object[] args, string prefix)
|
27 | 16 | {
|
28 |
| - if(string.IsNullOrWhiteSpace(prefix)) |
29 |
| - { |
30 |
| - var typeName = methodInfo.DeclaringType.Name; |
31 |
| - var methodName = methodInfo.Name; |
32 |
| - |
33 |
| - var methodArguments = this.FormatArgumentsToPartOfCacheKey(args); |
34 |
| - |
35 |
| - return this.GenerateCacheKey(typeName, methodName, methodArguments); |
36 |
| - } |
37 |
| - else |
38 |
| - { |
39 |
| - var methodArguments = this.FormatArgumentsToPartOfCacheKey(args); |
40 |
| - |
41 |
| - return this.GenerateCacheKey(string.Empty, prefix, methodArguments); |
42 |
| - } |
| 17 | + var methodArguments = args?.Any() == true |
| 18 | + ? args.Select(ParameterCacheKeys.GenerateCacheKey) |
| 19 | + : new[] { "0" }; |
| 20 | + return GenerateCacheKey(methodInfo, prefix, methodArguments); |
43 | 21 | }
|
44 | 22 |
|
45 |
| - /// <summary> |
46 |
| - /// Gets the cache key prefix. |
47 |
| - /// </summary> |
48 |
| - /// <returns>The cache key prefix.</returns> |
49 |
| - /// <param name="methodInfo">Method info.</param> |
50 |
| - /// <param name="prefix">Prefix.</param> |
51 | 23 | public string GetCacheKeyPrefix(MethodInfo methodInfo, string prefix)
|
52 | 24 | {
|
53 |
| - if (string.IsNullOrWhiteSpace(prefix)) |
54 |
| - { |
55 |
| - var typeName = methodInfo.DeclaringType.Name; |
56 |
| - var methodName = methodInfo.Name; |
| 25 | + if (!string.IsNullOrWhiteSpace(prefix)) return $"{prefix}{LinkChar}"; |
57 | 26 |
|
58 |
| - return this.GenerateCacheKeyPrefix(typeName, methodName); |
59 |
| - } |
60 |
| - else |
61 |
| - { |
62 |
| - return this.GenerateCacheKeyPrefix(string.Empty, prefix); |
63 |
| - } |
64 |
| - } |
| 27 | + var typeName = methodInfo.DeclaringType?.Name; |
| 28 | + var methodName = methodInfo.Name; |
65 | 29 |
|
66 |
| - /// <summary> |
67 |
| - /// Generates the cache key prefix. |
68 |
| - /// </summary> |
69 |
| - /// <returns>The cache key prefix.</returns> |
70 |
| - /// <param name="first">First.</param> |
71 |
| - /// <param name="second">Second.</param> |
72 |
| - private string GenerateCacheKeyPrefix(string first, string second) |
73 |
| - { |
74 |
| - return string.Concat(first,_linkChar,second,_linkChar).TrimStart(_linkChar); |
| 30 | + return $"{typeName}{LinkChar}{methodName}{LinkChar}"; |
75 | 31 | }
|
76 | 32 |
|
77 |
| - /// <summary> |
78 |
| - /// Formats the arguments to part of cache key. |
79 |
| - /// </summary> |
80 |
| - /// <returns>The arguments to part of cache key.</returns> |
81 |
| - /// <param name="methodArguments">Method arguments.</param> |
82 |
| - private IList<string> FormatArgumentsToPartOfCacheKey(object[] methodArguments) |
| 33 | + private string GenerateCacheKey(MethodInfo methodInfo, string prefix, IEnumerable<string> parameters) |
83 | 34 | {
|
84 |
| - if(methodArguments!=null && methodArguments.Length > 0) |
85 |
| - { |
86 |
| - return methodArguments.Select(this.GetArgumentValue).ToList(); |
87 |
| - } |
88 |
| - else |
89 |
| - { |
90 |
| - return new List<string> { "0" }; |
91 |
| - } |
92 |
| - } |
| 35 | + var cacheKeyPrefix = GetCacheKeyPrefix(methodInfo, prefix); |
93 | 36 |
|
94 |
| - /// <summary> |
95 |
| - /// Generates the cache key. |
96 |
| - /// </summary> |
97 |
| - /// <returns>The cache key.</returns> |
98 |
| - /// <param name="typeName">Type name.</param> |
99 |
| - /// <param name="methodName">Method name.</param> |
100 |
| - /// <param name="parameters">Parameters.</param> |
101 |
| - private string GenerateCacheKey(string typeName, string methodName, IList<string> parameters) |
102 |
| - { |
103 | 37 | var builder = new StringBuilder();
|
104 |
| - |
105 |
| - builder.Append(this.GenerateCacheKeyPrefix(typeName,methodName)); |
106 |
| - |
107 |
| - foreach (var param in parameters) |
108 |
| - { |
109 |
| - builder.Append(param); |
110 |
| - builder.Append(_linkChar); |
111 |
| - } |
112 |
| - |
113 |
| - var str = builder.ToString().TrimEnd(_linkChar); |
114 |
| - |
115 |
| - return str; |
116 |
| - //using (SHA1 sha1 = SHA1.Create()) |
117 |
| - //{ |
118 |
| - // byte[] data = sha1.ComputeHash(Encoding.UTF8.GetBytes(str)); |
119 |
| - // return Convert.ToBase64String(data, Base64FormattingOptions.None); |
120 |
| - //} |
121 |
| - } |
122 |
| - |
123 |
| - /// <summary> |
124 |
| - /// Gets the argument value. |
125 |
| - /// </summary> |
126 |
| - /// <returns>The argument value.</returns> |
127 |
| - /// <param name="arg">Argument.</param> |
128 |
| - private string GetArgumentValue(object arg) |
129 |
| - { |
130 |
| - if (arg is int || arg is long || arg is string) |
131 |
| - return arg.ToString(); |
132 |
| - |
133 |
| - if (arg is DateTime) |
134 |
| - return ((DateTime)arg).ToString("yyyyMMddHHmmss"); |
135 |
| - |
136 |
| - if (arg is ICachable) |
137 |
| - return ((ICachable)arg).CacheKey; |
138 |
| - |
139 |
| - return null; |
| 38 | + builder.Append(cacheKeyPrefix); |
| 39 | + builder.Append(string.Join(LinkChar.ToString(), parameters)); |
| 40 | + return builder.ToString(); |
140 | 41 | }
|
141 | 42 | }
|
142 | 43 | }
|
0 commit comments