예제 #1
0
 internal Topic(string topicName, CacheImplBase cacheImpl, PerfStatsCollector2 perfStatsCollector,
                MessageManager parent)
 {
     _topicName          = topicName;
     _subscriptions      = new Dictionary <string, TopicSubscription>(StringComparer.InvariantCultureIgnoreCase);
     _cacheImpl          = cacheImpl;
     _perfStatsCollector = perfStatsCollector;
     _parent             = parent;
     _publisherId        = new Shorter().GuidString;
     _readerWriterLock   = new ReaderWriterLock();
 }
예제 #2
0
        private static Cache InitializeCacheInternal(string cacheId, CacheInitParams initParams)
        {
            if (cacheId == null)
            {
                throw new ArgumentNullException("cacheId");
            }
            if (cacheId == string.Empty)
            {
                throw new ArgumentException("cacheId cannot be an empty string");
            }

            CacheMode mode = initParams.Mode;

            int maxTries = 2;

            try
            {
                CacheServerConfig config = null;

                if (mode != CacheMode.OutProc)
                {
                    do
                    {
                        try
                        {
                            config = DirectoryUtil.GetCacheDom(cacheId, mode == CacheMode.InProc);
                        }
                        catch (Exception ex)
                        {
                            if (mode == CacheMode.Default)
                            {
                                mode = CacheMode.OutProc;
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                        if (config != null)
                        {
                            switch (mode)
                            {
                            case CacheMode.InProc: config.InProc = true; break;

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

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

                    lock (s_webCaches)
                    {
                        if (!s_webCaches.Contains(cacheId))
                        {
                            CacheImplBase cacheImpl = null;

                            if (config != null && config.InProc)
                            {
                                Alachisoft.NCache.Caching.Cache ncache = null;
                                Cache cache = null;
                                maxTries = 2;
                                do
                                {
                                    try
                                    {
                                        CacheConfig cacheConfig = CacheConfig.FromDom(config);

                                        if (Web.Caching.APILogging.DebugAPIConfiguraions.LoggingEnabled)
                                        {
                                            cache = new WrapperCache(new Cache(null, cacheConfig));
                                        }
                                        else
                                        {
                                            cache = new Cache(null, cacheConfig);
                                        }

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

                                        cacheImpl       = new InprocCache(ncache, cacheConfig, cache);
                                        cache.CacheImpl = cacheImpl;

                                        if (primaryCache == null)
                                        {
                                            primaryCache = cache;
                                        }
                                        break;
                                    }
                                    catch (Exception) { }
                                } while (maxTries > 0);
                            }
                            else
                            {
                                maxTries = 2;
                                do
                                {
                                    try
                                    {
                                        // does an AddRef() internally.

                                        PerfStatsCollector2 perfStatsCollector = new PerfStatsCollector2(cacheId, false);

                                        if (Web.Caching.APILogging.DebugAPIConfiguraions.LoggingEnabled)
                                        {
                                            primaryCache = new WrapperCache(new Cache(null, cacheId, perfStatsCollector));
                                        }
                                        else
                                        {
                                            primaryCache = new Cache(null, cacheId, perfStatsCollector);
                                        }
                                        cacheImpl = new RemoteCache(cacheId, primaryCache, initParams, perfStatsCollector);

                                        perfStatsCollector.InitializePerfCounters(false);
                                        primaryCache.CacheImpl = cacheImpl;

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

                            if (primaryCache != null)
                            {
                                primaryCache.InitializeCompactFramework();
                                s_webCaches.AddCache(cacheId, primaryCache);
                            }
                        }
                        else
                        {
                            lock (s_webCaches.GetCache(cacheId))
                            {
                                primaryCache = s_webCaches.GetCache(cacheId) as Cache;
                                primaryCache.AddRef();
                            }
                        }
                    }

                    lock (s_webCache)
                    {
                        // it is first cache instance.
                        if (s_webCache.CacheImpl == null)
                        {
                            primaryCache.ExceptionsEnabled = ExceptionsEnabled;
                            s_webCache = primaryCache;
                        }
                    }
                    return(primaryCache);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #3
0
파일: Cache.cs 프로젝트: javithalion/NCache
        /// <summary>
        /// Initializes a new instance of the Cache class.
        /// </summary>
        /// <param name="objectCache"></param>
        /// <param name="cacheId"></param>        
        internal Cache(CacheImplBase objectCache, string cacheId, PerfStatsCollector2 perfStatsCollector)
        {
            _cacheImpl = objectCache;
            _cacheId = cacheId;
            if (_cacheImpl != null)
            {
                _serializationContext = _cacheImpl.Name; //Sets the serialization context.
            }

            _eventManager = new EventManager(_cacheId, null, this);
            _listener = new CacheEventsListener(this, _eventManager);
            _asyncListener = new CacheAsyncEventsListener(this);

            _perfStatsCollector = perfStatsCollector;

            AddRef();
        }
예제 #4
0
파일: Cache.cs 프로젝트: javithalion/NCache
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or 
        /// resetting unmanaged resources.
        /// </summary>
        /// <param name="disposing"></param>
        /// <remarks>The behavior of this method depends upon the cache's sharing mode (inproc/outproc) 
        /// specified in the configuration.
        /// <para>If the application uses the cache in <b>inproc</b> mode, calling Dispose closes the cache 
        /// and releases the resources used by it. If in a cluster the calling node leaves the 
        /// cluster as well. 
        /// </para>
        /// <para>If the application uses the cache in <b>outproc</b> mode, calling Dispose releases the 
        /// reference to the cache object. The cache itself remains operational and cluster remains intact. 
        /// </para>
        /// </remarks>
        private void Dispose(bool disposing)
        {
            try
            {
                lock (this)
                {
                    _refCount--;
                    if (_refCount > 0) return;
                    else if (_refCount < 0) _refCount = 0;

                    // remove from the cache table.
                    lock (NCache.Caches)
                    {
                        if (_cacheId != null)
                            NCache.Caches.Remove(_cacheId);
                    }

                    if (_cacheImpl != null) _cacheImpl.Dispose(disposing);
                    _cacheImpl = null;

                    if (disposing) GC.SuppressFinalize(this);
                }
            }
            catch (Exception ex) { }
        }
예제 #5
0
        private static Cache InitializeCacheInternal(string cacheId, CacheInitParams initParams,
                                                     bool isRemoveCache = true)
        {
            if (cacheId == null)
            {
                throw new ArgumentNullException("cacheId");
            }
            if (cacheId == string.Empty)
            {
                throw new ArgumentException("cacheId cannot be an empty string");
            }

            CacheMode mode = initParams.Mode;


            int maxTries = 2;

            try
            {
                CacheServerConfig config = null;

                if (mode != CacheMode.OutProc)
                {
                    do
                    {
                        try
                        {
                            config = DirectoryUtil.GetCacheDom(cacheId, mode == CacheMode.InProc);
                        }


                        catch (Exception ex)
                        {
                            if (mode == CacheMode.Default)
                            {
                                mode = CacheMode.OutProc;
                            }
                            else
                            {
                                throw ex;
                            }
                        }

                        if (config != null)
                        {
                            if (config.CacheType.ToLower().Equals("clustered-cache"))
                            {
                                throw new Exception("Cluster cache cannot be initialized in In-Proc mode.");
                            }

                            switch (mode)
                            {
                            case CacheMode.InProc:
                                config.InProc = true;
                                break;

                            case CacheMode.OutProc:
                                config.InProc = false;
                                break;
                            }
                        }

                        break;
                    } while (maxTries > 0);
                }

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

                    lock (s_webCaches)
                    {
                        if (!s_webCaches.Contains(cacheId))
                        {
                            CacheImplBase cacheImpl = null;

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

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

                                    cache = new Cache(null, cacheConfig);


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


                                    cacheImpl       = new InprocCache(ncache, cacheConfig, cache);
                                    cache.CacheImpl = cacheImpl;

                                    if (primaryCache == null)
                                    {
                                        primaryCache = cache;
                                    }

                                    else
                                    {
                                        primaryCache.AddSecondaryInprocInstance(cache);
                                    }

                                    break;
                                } while (maxTries > 0);
                            }
                            else
                            {
                                maxTries = 2;
                                do
                                {
                                    try
                                    {
                                        PerfStatsCollector2 perfStatsCollector =
                                            new PerfStatsCollector2(cacheId, false);

                                        primaryCache = new Cache(null, cacheId, perfStatsCollector);
                                        cacheImpl    = new RemoteCache(cacheId, primaryCache, initParams,
                                                                       perfStatsCollector);

                                        perfStatsCollector.InitializePerfCounters(false);

                                        primaryCache.CacheImpl = cacheImpl;

                                        break;
                                    }


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

                            if (primaryCache != null)
                            {
                                s_webCaches.AddCache(cacheId, primaryCache);
                            }
                        }
                        else
                        {
                            lock (s_webCaches.GetCache(cacheId))
                            {
                                primaryCache = s_webCaches.GetCache(cacheId) as Cache;

                                primaryCache.AddRef();
                            }
                        }
                    }

                    lock (s_webCache)
                    {
                        if (s_webCache.CacheImpl == null)
                        {
                            primaryCache.ExceptionsEnabled = ExceptionsEnabled;
                            s_webCache = primaryCache;
                        }
                    }

                    return(primaryCache);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #6
0
파일: Cache.cs 프로젝트: javithalion/NCache
        /// <summary>
        /// Initializes a new instance of the Cache class.
        /// </summary>
        /// <param name="objectCache"></param>
        /// <param name="config"></param>
        internal Cache(CacheImplBase objectCache, CacheConfig config)
        {
            _cacheImpl = objectCache;
            _config = config;
            _cacheId = config.CacheId;

            if (_cacheImpl != null)
            {
                _serializationContext = _cacheImpl.Name; //Sets the serialization context.
                _cacheId = _cacheImpl.Name;
            }
            _eventManager = new EventManager(_cacheId, null, this);
            _listener = new CacheEventsListener(this, _eventManager);
            _asyncListener = new CacheAsyncEventsListener(this);
             AddRef();
        }
예제 #7
0
        public override object SafeDeserialize(object serializedObject, string serializationContext, BitSet flag, CacheImplBase cacheImpl)
        {
            object deserialized = serializedObject;

            try
            {
                if (serializedObject is byte[] && cacheImpl.SerializationEnabled)
                {
                    if (flag != null && flag.IsBitSet(BitSetConstants.BinaryData)) return serializedObject;

                    deserialized = CompactBinaryFormatter.FromByteBuffer((byte[])serializedObject, serializationContext);
                }
            }
            catch (Exception exception)
            {
                //Kill the exception; it is possible that object was serialized by Java
                //or from any other domain which can not be deserialized by us.
                deserialized = serializedObject;

                //throw;
            }

            return deserialized;
        }
예제 #8
0
        public override object SafeSerialize(object serializableObject, string serializationContext, ref BitSet flag, CacheImplBase cacheImpl, ref long size)
        {
            if (serializableObject != null && cacheImpl.SerializationEnabled)
            {
                System.Type type = serializableObject.GetType();

                if (typeof(byte[]).Equals(type) && flag != null)
                {
                    flag.SetBit(BitSetConstants.BinaryData);
                    size = serializableObject is byte[] ? ((byte[])serializableObject).Length : 0;
                    return serializableObject;
                }
                size = serializableObject is byte[] ? ((byte[])serializableObject).Length : 0;
                serializableObject = CompactBinaryFormatter.ToByteBuffer(serializableObject, serializationContext);
            }
            return serializableObject;
        }
예제 #9
0
 public override object SafeDeserialize(object serializedObject, string serializationContext, BitSet flag, CacheImplBase cacheImpl)
 {
     return serializedObject;
 }
예제 #10
0
 public virtual object SafeDeserialize(object serializedObject, string serializationContext, BitSet flag, CacheImplBase cacheImpl)
 {
     return(null);
 }
예제 #11
0
 public virtual object SafeSerialize(object serializableObject, string serializationContext, ref BitSet flag, CacheImplBase cacheImpl, ref long size)
 {
     return(null);
 }
예제 #12
0
 public virtual object SafeSerialize(object serializableObject, string serializationContext, ref BitSet flag, CacheImplBase cacheImpl, ref long size)
 {
     return null;
 }
예제 #13
0
 public virtual object SafeDeserialize(object serializedObject, string serializationContext, BitSet flag, CacheImplBase cacheImpl)
 {
     return null;
 }