src/Datory/Caching/CachingUtils.cs

(开头部分) 6KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using CacheManager.Core;

namespace Datory.Caching
{
    public static class CachingUtils
    {
        private static readonly ConcurrentDictionary<string, ICacheManager<object>> CacheManagers = new ConcurrentDictionary<string, ICacheManager<object>>();

        public static async Task<ICacheManager<object>> GetCacheManagerAsync(IRedis redis)
        {
            string key;
            if (redis == null || string.IsNullOrEmpty(redis.ConnectionString))
            {
                key = "Memory";
            }
            else
            {
                key = "Distributed:" + redis.ConnectionString;
            }
            if (CacheManagers.TryGetValue(key, out var tc))
            {
                return tc;
            }

            var cacheManager = await CreateAsync(redis);

            CacheManagers[key] = cacheManager;
            return cacheManager;
        }

        private static async Task<ICacheManager<object>> CreateAsync(IRedis redis)
        {
            if (redis != null && !string.IsNullOrEmpty(redis.ConnectionString))
            {
                var (isConnectionWorks, _) = await redis.IsConnectionWorksAsync();
                if (isConnectionWorks)
                {
                    return CacheFactory.Build(settings =>
                    {
                        settings
                            .WithRedisConfiguration("redis", config =>
                            {
                                if (!string.IsNullOrEmpty(redis.Password))
                                {
                                    config.WithPassword(redis.Password);
                                }
                                if (redis.AllowAdmin)
                                {
                                    config.WithAllowAdmin();
                                }

                                config
                                    .WithDatabase(redis.Database)
                                    .WithEndpoint(redis.Host, redis.Port);
                            })
                            .WithMaxRetries(1000)
                            .WithRetryTimeout(100)
后面还有 126 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。