示例#1
0
        private static Cache GetCacheInternal(string cacheName, CacheConnectionOptions cacheConnectionOptions)

        {
            if (cacheName == null)
            {
                throw new ArgumentNullException("cacheId");
            }
            if (cacheName == string.Empty)
            {
                throw new ArgumentException("cacheId cannot be an empty string");
            }

            IsolationLevel mode = cacheConnectionOptions.Mode.Value;



            string cacheIdWithAlias = cacheName;

            int maxTries = 2;


            try
            {
                CacheServerConfig config = null;

                if (mode != IsolationLevel.OutProc)
                {
                    do
                    {
                        try
                        {
                            config = DirectoryUtil.GetCacheDom(cacheName,
                                                               null,
                                                               null,
                                                               mode == IsolationLevel.InProc);
                        }


                        catch (Exception ex)
                        {
                            if (mode == IsolationLevel.Default)
                            {
                                mode = IsolationLevel.OutProc;
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                        if (config != null)
                        {
                            if (config.CacheType.ToLower().Equals("clustered-cache"))
                            {
                                throw new OperationFailedException(ErrorCodes.CacheInit.CLUSTER_INIT_IN_INPROC, ErrorMessages.GetErrorMessage(ErrorCodes.CacheInit.CLUSTER_INIT_IN_INPROC));
                            }
                            switch (mode)
                            {
                            case IsolationLevel.InProc: config.InProc = true; break;

                            case IsolationLevel.OutProc: config.InProc = false; break;
                            }
                        }
                        break;
                    } while (maxTries > 0);
                }

                lock (typeof(CacheManager))
                {
                    Cache primaryCache = null;

                    lock (_cacheCollection)
                    {
                        if (!_cacheCollection.Contains(cacheIdWithAlias))
                        {
                            CacheImplBase cacheImpl = null;

                            if (config != null && config.InProc)
                            {
                                NCache.Caching.Cache ncache = null;
                                Cache cache = null;
                                maxTries = 2;

                                do
                                {
                                    try
                                    {
                                        CacheConfig cacheConfig = CacheConfig.FromDom(config);

                                        cache = new Cache(null, cacheConfig);


                                        ncache = CacheFactory.CreateFromPropertyString(cacheConfig.PropertyString, config, null, null, false, false);


                                        cacheImpl = new InprocCache(ncache, cacheConfig, cache, null, null);

                                        cache.CacheImpl = cacheImpl;

                                        if (primaryCache == null)
                                        {
                                            primaryCache = cache;
                                        }
                                        else
                                        {
                                            primaryCache.AddSecondaryInprocInstance(cache);
                                        }

                                        break;
                                    }
                                    catch (SecurityException se)
                                    {
                                        maxTries--;

                                        if (maxTries == 0)
                                        {
                                            throw se;
                                        }
                                    }
                                } while (maxTries > 0);
                            }
                            else
                            {
                                maxTries = 2;
                                do
                                {
                                    try
                                    {
                                        StatisticsCounter perfStatsCollector;
                                        if (ServiceConfiguration.PublishCountersToCacheHost)
                                        {
                                            perfStatsCollector = new CustomStatsCollector(cacheName, false);
                                            ClientConfiguration clientConfig = new ClientConfiguration(cacheName);
                                            clientConfig.LoadConfiguration();
                                            perfStatsCollector.StartPublishingCounters(clientConfig.BindIP);
                                        }
                                        else
                                        {
                                            perfStatsCollector = new PerfStatsCollector(cacheName, false);
                                        }


                                        primaryCache = new Cache(null, cacheName, perfStatsCollector);

                                        cacheImpl = new RemoteCache(cacheName, primaryCache, cacheConnectionOptions, perfStatsCollector);
                                        perfStatsCollector.InitializePerfCounters(false);
                                        primaryCache.CacheImpl = cacheImpl;

                                        break;
                                    }

                                    catch (OperationNotSupportedException ex)
                                    {
                                        throw ex;
                                    }
                                } while (maxTries > 0);
                            }

                            if (primaryCache != null)
                            {
                                primaryCache.InitializeCompactFramework();
                                _cacheCollection.AddCache(cacheIdWithAlias, primaryCache);
                            }
                        }
                        else
                        {
                            lock (_cacheCollection.GetCache(cacheIdWithAlias, false))
                            {
                                primaryCache = _cacheCollection.GetCache(cacheIdWithAlias, false) as Cache;

                                primaryCache.AddRef();
                            }
                        }
                    }

                    lock (_cache)
                    {
                        // it is first cache instance.
                        if (_cache.CacheImpl == null)
                        {
                            primaryCache.ExceptionsEnabled = ExceptionsEnabled;
                            _cache = primaryCache;
                        }
                    }

                    return(primaryCache);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#2
0
        public void LoadConfiguration()
        {
            FileStream  fs = null;
            string      c_configFileName = null;
            XmlDocument configuration    = new XmlDocument();

            try
            {
                if (_cacheId == null)
                {
                    return;
                }

                c_configFileName = DirectoryUtil.GetBaseFilePath("client.ncconf", _search, out _result);

                if (c_configFileName == null)
                {
                    return;
                }

                FileInfo fileInfo = new FileInfo(c_configFileName);
                fs = fileInfo.OpenRead();
                configuration.Load(fs);
                fileInfo = null;

                bool serverPortFound = false;

                XmlNodeList clientInfoTag = configuration.GetElementsByTagName("ncache-server");

                CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                try
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    if (clientInfoTag != null && clientInfoTag.Count > 0)
                    {
                        XmlNode portNode = clientInfoTag.Item(0);
                        if (portNode != null)
                        {
                            XmlAttributeCollection attributes = portNode.Attributes;
                            if (attributes != null)
                            {
                                string currentAttrib = string.Empty;

                                if (attributes["port"] != null && attributes["port"].Value != null)
                                {
                                    ConfigServerPort = Convert.ToInt32(attributes["port"].Value);
                                }

                                if (_cacheConnectionOptions != null && _cacheConnectionOptions.Port.HasValue)
                                {
                                    _serverPort = _cacheConnectionOptions.Port.Value;
                                }
                                else
                                {
                                    _serverPort = ConfigServerPort;
                                }

                                if (_cacheConnectionOptions != null && _cacheConnectionOptions.ClientRequestTimeOut.HasValue)
                                {
                                    _clientRequestTimeout = Convert.ToInt32(_cacheConnectionOptions.ClientRequestTimeOut.Value.TotalMilliseconds);
                                }
                                else
                                {
                                    if (attributes["client-request-timeout"] != null && attributes["client-request-timeout"].Value != null)
                                    {
                                        _clientRequestTimeout = Convert.ToInt32(attributes["client-request-timeout"].Value) * 1000;
                                    }
                                }


                                if (_cacheConnectionOptions != null && _cacheConnectionOptions.ConnectionTimeout.HasValue)
                                {
                                    _connectionTimeout = Convert.ToInt32(_cacheConnectionOptions.ConnectionTimeout.Value.TotalMilliseconds);
                                }
                                else
                                {
                                    if (attributes["connection-timeout"] != null && attributes["connection-timeout"].Value != null)
                                    {
                                        _connectionTimeout = Convert.ToInt32(attributes["connection-timeout"].Value) * 1000;
                                    }
                                }
                            }
                            serverPortFound = true;
                        }
                    }

                    if (!serverPortFound)
                    {
                        throw new Runtime.Exceptions.ConfigurationException("ncache-server missing in client confiugration");
                    }


                    XmlNodeList cacheList   = configuration.GetElementsByTagName("cache");
                    XmlNodeList cacheConfig = null;

                    for (int i = 0; i < cacheList.Count; i++)
                    {
                        XmlNode cache = cacheList.Item(i);
                        if (cache.Attributes.GetNamedItem("id").Value.ToLower().Equals(_cacheId.ToLower()))
                        {
                            if (cache.Attributes["load-balance"] != null)
                            {
                                _balanceNodes = Convert.ToBoolean(cache.Attributes["load-balance"].Value);
                            }
                            if (_cacheConnectionOptions != null && _cacheConnectionOptions.LoadBalance.HasValue)
                            {
                                _balanceNodes = _cacheConnectionOptions.LoadBalance.Value;
                            }


                            if (_cacheConnectionOptions != null && _cacheConnectionOptions.EnableClientLogs.HasValue)
                            {
                                EnableClientLogs = _cacheConnectionOptions.EnableClientLogs.Value;
                            }
                            else
                            {
                                if (cache.Attributes["enable-client-logs"] != null)
                                {
                                    EnableClientLogs = Convert.ToBoolean(cache.Attributes["enable-client-logs"].Value.ToString());
                                }
                            }


                            if (_cacheConnectionOptions != null && _cacheConnectionOptions.LogLevel.HasValue)
                            {
                                LogLevels = _cacheConnectionOptions.LogLevel.Value;
                                switch (LogLevels)
                                {
                                case LogLevel.Debug:
                                case LogLevel.Info:
                                    EnableDetailedClientLogs = true;
                                    break;

                                case LogLevel.Error:
                                    EnableDetailedClientLogs = false;
                                    break;
                                }
                            }
                            else
                            {
                                if (cache.Attributes["log-level"] != null)
                                {
                                    var logLevel = cache.Attributes["log-level"].Value.ToString().ToLower();
                                    switch (logLevel)
                                    {
                                    case "info":
                                        LogLevels = LogLevel.Info;
                                        EnableDetailedClientLogs = true;
                                        break;

                                    case "debug":
                                        LogLevels = LogLevel.Debug;
                                        EnableDetailedClientLogs = true;
                                        break;

                                    case "error":
                                        LogLevels = LogLevel.Error;
                                        EnableDetailedClientLogs = false;
                                        break;
                                    }
                                }
                            }

                            ImportHashmap = true;

                            cacheConfig = cache.ChildNodes;
                            break;
                        }
                    }

                    if (cacheConfig == null)
                    {
                        if (!string.IsNullOrEmpty(_cacheId))
                        {
                            if (_result != Search.GlobalSearch)
                            {
                                _search = _result + 1;
                                LoadConfiguration();
                            }
                        }
                        return;
                    }
                    _search = _result;

                    LoadRemoteServerMappingConfig(cacheConfig);
                    LoadRemoteServerConfig(cacheConfig);
                }
                finally
                {
                    Thread.CurrentThread.CurrentCulture = cultureInfo;
                }
            }

            catch (Runtime.Exceptions.ConfigurationException)
            {
                throw;
            }
            catch (IOException) { throw; }
            catch (Exception e)
            {
                throw new Runtime.Exceptions.ConfigurationException("An error occurred while reading client.ncconf. " + e.Message);
            }

            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }