Skip to content

Commit 0ec5287

Browse files
authored
Update CacheKeyGenerator.cs
1 parent f886b99 commit 0ec5287

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/AybCache/CacheKeyGenerator.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
1-
namespace AybCache;
1+
namespace AybCache;
22

33
public static class CacheKeyGenerator
44
{
55
public static string GenerateCacheKey(string cacheKey, object[] arguments)
66
{
77
try
88
{
9-
if (!arguments.Any())
9+
if (IsArgumentsEmpty(arguments))
1010
{
1111
return cacheKey;
1212
}
13-
14-
if (arguments[0] is string str)
15-
{
16-
return string.Format(cacheKey, str.ToLower());
17-
}
18-
19-
if (arguments[0] is List<string> stringArguments)
20-
{
21-
return string.Format(cacheKey,
22-
string.Join(",", stringArguments.Select(x => x.ToLower()).OrderBy(q => q)));
23-
}
2413

25-
if (arguments[0] is ICacheKeyHolder cacheKeyHolder)
14+
var firstArgument = arguments[0];
15+
16+
return firstArgument switch
2617
{
27-
return string.Format(cacheKey, cacheKeyHolder.CacheKey);
28-
}
29-
18+
string str => FormatCacheKeyWithString(cacheKey, str),
19+
List<string> stringList => FormatCacheKeyWithStringList(cacheKey, stringList),
20+
ICacheKeyHolder cacheKeyHolder => FormatCacheKeyWithCacheKeyHolder(cacheKey, cacheKeyHolder),
21+
_ => null
22+
};
3023
}
3124
catch
3225
{
3326
// ignored
27+
return null;
3428
}
29+
}
3530

36-
return null;
31+
private static bool IsArgumentsEmpty(object[] arguments)
32+
{
33+
return !arguments.Any();
34+
}
35+
36+
private static string FormatCacheKeyWithString(string cacheKey, string str)
37+
{
38+
return string.Format(cacheKey, str.ToLower());
39+
}
40+
41+
private static string FormatCacheKeyWithStringList(string cacheKey, List<string> stringList)
42+
{
43+
var sortedLowerCaseValues = string.Join(",",
44+
stringList.Select(x => x.ToLower()).OrderBy(q => q));
45+
46+
return string.Format(cacheKey, sortedLowerCaseValues);
47+
}
48+
49+
private static string FormatCacheKeyWithCacheKeyHolder(string cacheKey, ICacheKeyHolder cacheKeyHolder)
50+
{
51+
return string.Format(cacheKey, cacheKeyHolder.CacheKey);
3752
}
38-
}
53+
}

0 commit comments

Comments
 (0)