예제 #1
0
        public DefaultRedisCacheProvider(string appName)
        {
            _context = new Lazy <DefaultRedisContext>(() =>
            {
                return(CachingContainer.IsRegistered <DefaultRedisContext>(appName)
                    ? CachingContainer.GetService <DefaultRedisContext>(appName)
                    : CachingContainer.GetInstances <DefaultRedisContext>(appName));
            });

            _keySuffix         = appName;
            _defaultExpireTime = new Lazy <long>(() => long.Parse(_context.Value._defaultExpireTime));
            _connectTimeout    = new Lazy <int>(() => int.Parse(_context.Value._connectTimeout));

            if (CachingContainer.IsRegistered <ICacheClient <IDatabase> >(CacheTargetType.Redis.ToString()))
            {
                _addressResolver = CachingContainer.GetService <IAddressResolver>();
                _cacheClient     = new Lazy <ICacheClient <IDatabase> >(() =>
                                                                        CachingContainer.GetService <ICacheClient <IDatabase> >(CacheTargetType.Redis.ToString()));
            }
            else
            {
                _cacheClient = new Lazy <ICacheClient <IDatabase> >(() =>
                                                                    CachingContainer.GetInstances <ICacheClient <IDatabase> >(CacheTargetType.Redis.ToString()));
            }
        }
        /// <summary>
        /// redis数据上下文
        /// </summary>
        /// <param name="args">参数</param>
        public DefaultRedisContext(params object[] args)
        {
            if (CachingContainer.IsRegistered <IHashAlgorithm>())
            {
                _hashAlgorithm = CachingContainer.GetService <IHashAlgorithm>();
            }
            else
            {
                _hashAlgorithm = CachingContainer.GetInstances <IHashAlgorithm>();
            }
            foreach (var arg in args)
            {
                var properties = arg.GetType().GetProperties();
                var field      = GetType()
                                 .GetField(string.Format("_{0}", properties[0].GetValue(arg)),
                                           BindingFlags.NonPublic |
                                           BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (properties.Count() == 3)
                {
                    _cachingContextPool = new Lazy <Dictionary <string, List <string> > >(
                        () =>
                    {
                        var dataContextPool = new Dictionary <string, List <string> >();
                        var lArg            = arg as List <object>;
                        Debug.Assert(lArg != null, nameof(lArg) + " != null");
                        foreach (var tmpArg in lArg)
                        {
                            var props = tmpArg.GetType().GetTypeInfo().GetProperties();
                            var items = props[2].GetValue(tmpArg) as object[];
                            if (items != null)
                            {
                                var list = (from item in items
                                            let itemProperties = item.GetType().GetProperties()
                                                                 select itemProperties[1].GetValue(item)
                                                                 into value
                                                                 select value.ToString()).ToList();
                                dataContextPool.Add(props[1].GetValue(tmpArg).ToString(), list);
                            }
                        }

                        return(dataContextPool);
                    }
                        );
                }
                else
                {
                    if (field != null)
                    {
                        field.SetValue(this, properties[1].GetValue(arg));
                    }
                }
            }

            dicHash = new ConcurrentDictionary <string, ConsistentHash <ConsistentHashNode> >();
            InitSettingHashStorage();
        }
예제 #3
0
        public DefaultMemoryCacheProvider(string appName)
        {
            _context = new Lazy <DefaultRedisContext>(() =>
            {
                return(CachingContainer.IsRegistered <DefaultRedisContext>(CacheTargetType.Redis.ToString())
                    ? CachingContainer.GetService <DefaultRedisContext>(appName)
                    : CachingContainer.GetInstances <DefaultRedisContext>(appName));
            });

            _keySuffix         = appName;
            _defaultExpireTime = new Lazy <long>(() => long.Parse(_context.Value._defaultExpireTime));
        }