Пример #1
0
        public void InitializeCache(string cache)
        {


             _cache = Alachisoft.NCache.Web.Caching.NCache.InitializeCache(cache);

        }
Пример #2
0
        public async Task <Author> GetAuthor(int id)
        {
            _cache = NCache.InitializeCache("CacheName");
            var    cacheKey = "Key";
            Author author   = null;

            if (_cache != null)
            {
                author = _cache.Get(cacheKey) as Author;
            }
            if (author == null) //Data not available in the cache
            {
                //Write code here to fetch the author
                // object from the database
                if (author != null)
                {
                    if (_cache != null)
                    {
                        _cache.Insert(cacheKey, author, null,
                                      Alachisoft.NCache.Web.Caching.Cache.NoAbsoluteExpiration,
                                      TimeSpan.FromMinutes(10),
                                      Alachisoft.NCache.Runtime.
                                      CacheItemPriority.Default);
                    }
                }
            }
            return(author);
        }
Пример #3
0
 public void Add(string key, object value)
 {
     Alachisoft.NCache.Web.Caching.Cache cache = (Alachisoft.NCache.Web.Caching.Cache)_caches[_primaryCache];
     if (cache != null)
     {
         cache.Add(key, TagUtil.CreateTaggedCacheItem(value));
     }
 }
Пример #4
0
        public void Dispose()
        {
            if (_cache == null)
                return;

            _cache.Dispose();
            _cache = null; 
        }
Пример #5
0
        public void Dispose()
        {
            if (_cache == null)
            {
                return;
            }

            _cache.Dispose();
            _cache = null;
        }
Пример #6
0
        public void InsertAsync(string sessionId, string key, object value, CacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            GetCache(sessionId, key, out obj, "", "", out cache, false);
            if (cache != null)
            {
                cache.InsertAsync(key, TagUtil.CreateTaggedCacheItem(value, dependency, absoluteExpiration, slidingExpiration, priority), DSWriteOption.None, null);
            }
        }
Пример #7
0
        public void InsertAsync(string sessionId, string key, object value)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            GetCache(sessionId, key, out obj, "", "", out cache, false);
            if (cache != null)
            {
                cache.InsertAsync(key, TagUtil.CreateTaggedCacheItem(value), null, null, null);
            }
        }
Пример #8
0
        public void RemoveAsync(string sessionId, string key)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            GetCache(sessionId, key, out obj, "", "", out cache, false);
            if (cache != null)
            {
                cache.RemoveAsync(key, null, DSWriteOption.None, null);
            }
        }
Пример #9
0
        public bool Contains(string sessionId, string key)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            GetCache(sessionId, key, out obj, "", "", out cache, false);
            if (cache != null)
            {
                return(cache.Contains(key));
            }
            return(false);
        }
Пример #10
0
 public bool Contains(string key)
 {
     Alachisoft.NCache.Web.Caching.Cache cache = (Alachisoft.NCache.Web.Caching.Cache)_caches[_primaryCache];
     if (cache != null)
     {
         return(cache.Contains(key));
     }
     else
     {
         return(false);
     }
 }
Пример #11
0
 public IEnumerator GetEnumerator()
 {
     Alachisoft.NCache.Web.Caching.Cache cache = (Alachisoft.NCache.Web.Caching.Cache)_caches[_primaryCache];
     if (cache != null)
     {
         return(cache.GetEnumerator());
     }
     else
     {
         return(null);
     }
 }
Пример #12
0
        public void InsertAsync(string sessionId, string key, CacheItem item, string group, string subGroup)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            GetCache(sessionId, key, out obj, group, subGroup, out cache, false);
            if (cache != null)
            {
                item.Tags = new Runtime.Caching.Tag[] { new Runtime.Caching.Tag(TagUtil.SESSION_TAG) };
                cache.InsertAsync(key, item, DSWriteOption.None, null);
            }
        }
Пример #13
0
 public object Remove(string key)
 {
     Alachisoft.NCache.Web.Caching.Cache cache = (Alachisoft.NCache.Web.Caching.Cache)_caches[_primaryCache];
     if (cache != null)
     {
         return(cache.Remove(key));
     }
     else
     {
         return(null);
     }
 }
Пример #14
0
        /// <summary>
        /// Constructor
        /// </summary>
        public ThreadContainer(Cache cache, int totalLoopCount, int testCaseIterations, int testCaseIterationDelay, int getsPerIteration, int updatesPerIteration, int dataSize, int expiration, int threadCount, int reportingInterval, int threadIndex)
        {
            _cache = cache;
            _totalLoopCount = totalLoopCount;
            _testCaseIterations = testCaseIterations;
            _testCaseIterationDelay = testCaseIterationDelay;
            _getsPerIteration = getsPerIteration;
            _updatesPerIteration = updatesPerIteration;
            _dataSize = dataSize;
            _expiration = expiration;
            _threadCount = threadCount;
            _reportingInterval = reportingInterval;
            _threadIndex = threadIndex;

            _pid = System.Diagnostics.Process.GetCurrentProcess().Id;
        }
Пример #15
0
        public object Get(string sessionId, string key, string group, string subGroup)
        {
            object obj = null;

            Alachisoft.NCache.Web.Caching.Cache cache = null;

            _sync.AcquireReaderLock(Timeout.Infinite);
            try
            {
                GetCache(sessionId, key, out obj, group, subGroup, out cache, true);
                return(obj);
            }
            finally
            {
                _sync.ReleaseReaderLock();
            }
        }
Пример #16
0
        public object Get(string sessionId, string key, ref LockHandle lockHandle, bool acquireLock, bool enableRetry)
        {
            object obj = null;

            Alachisoft.NCache.Web.Caching.Cache cache = null;

            _sync.AcquireReaderLock(Timeout.Infinite);
            try
            {
                GetCache(sessionId, key, out obj, null, null, out cache, true, ref lockHandle, acquireLock);
                return(obj);
            }
            finally
            {
                _sync.ReleaseReaderLock();
            }
        }
Пример #17
0
        public NCacheWrapper(MemoryCacheOptions options)
        {
            if (!NCacheConfiguration.IsConfigured)
            {
                throw new Exception("NCache initialization configuration is not provided. Please use Alachisoft.NCache.EntityFrameworkCore.NCacheConfiguration.Configure() method to configure NCache before using it.");
            }
            if (NCacheConfiguration.InitParams != null)
            {
                _nCache = Web.Caching.NCache.InitializeCache(NCacheConfiguration.CacheId, NCacheConfiguration.InitParams);
            }
            else
            {
                _nCache = Web.Caching.NCache.InitializeCache(NCacheConfiguration.CacheId);
            }

            _defaultKeyGen = new DefaultKeyGenerator();
        }
Пример #18
0
        public void Insert(string sessionId, string key, CacheItem item, LockHandle lockHandle, bool releaseLock, bool enableRetry)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            _sync.AcquireReaderLock(Timeout.Infinite);
            try
            {
                GetCache(sessionId, key, out obj, "", "", out cache, false);
                if (cache != null)
                {
                    item.Tags = new Runtime.Caching.Tag[] { new Runtime.Caching.Tag(TagUtil.SESSION_TAG) };
                    cache.Insert(key, item, lockHandle, releaseLock);
                }
            }
            finally
            {
                _sync.ReleaseReaderLock();
            }
        }
Пример #19
0
        public void Unlock(string sessionId, string key)
        {
            object obj = null;

            Alachisoft.NCache.Web.Caching.Cache cache = null;

            _sync.AcquireReaderLock(Timeout.Infinite);
            try
            {
                GetCache(sessionId, key, out obj, null, null, out cache, true);
                if (cache != null)
                {
                    cache.Unlock(key);
                }
            }
            finally
            {
                _sync.ReleaseReaderLock();
            }
        }
Пример #20
0
        public object Remove(string sessionId, string key, bool enableRetry)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            _sync.AcquireReaderLock(Timeout.Infinite);
            try
            {
                GetCache(sessionId, key, out obj, "", "", out cache, false);
                if (cache != null)
                {
                    return(cache.Remove(key));
                }
            }
            finally
            {
                _sync.ReleaseReaderLock();
            }
            return(null);
        }
Пример #21
0
        public void Insert(string sessionId, string key, CacheItem item, string group, string subGroup)
        {
            Alachisoft.NCache.Web.Caching.Cache cache = null;
            object obj = null;

            _sync.AcquireReaderLock(Timeout.Infinite);
            try
            {
                GetCache(sessionId, key, out obj, group, subGroup, out cache, false);
                if (cache != null)
                {
                    item.Group    = group;
                    item.SubGroup = subGroup;
                    item.Tags     = new Runtime.Caching.Tag[] { new Runtime.Caching.Tag(TagUtil.SESSION_TAG) };

                    cache.Insert(key, item, DSWriteOption.None, null);
                }
            }
            finally
            {
                _sync.ReleaseReaderLock();
            }
        }
Пример #22
0
        public void Dispose()
        {
            if (_connectionRecycler != null)
            {
                _connectionRecycler.Dispose();
                _connectionRecycler = null;
            }

            if (_caches == null)
            {
                return;
            }

            foreach (DictionaryEntry entry in _caches)
            {
                Alachisoft.NCache.Web.Caching.Cache cache = entry.Value as Alachisoft.NCache.Web.Caching.Cache;
                if (cache != null)
                {
                    cache.Dispose();
                }
            }
            _caches.Clear();
            _caches = null;
        }
Пример #23
0
        public OperationResult InitCache(string cacheID)
        {
            if (string.IsNullOrEmpty(cacheID))
                ThrowInvalidArgumentsException();

            OperationResult returnObject = new OperationResult();

            try
            {
                _cache = Alachisoft.NCache.Web.Caching.NCache.InitializeCache(cacheID);
                
                if (!_cache.Contains(ItemVersionKey))
                {
                    _cache.Add(ItemVersionKey, ItemVersionValue); 
                }
                returnObject = CreateReturnObject(Result.SUCCESS, null);
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }

            return returnObject;
        }
Пример #24
0
 public void Dispose()
 {
     _nCache = null;
 }
Пример #25
0
 private static bool InitializeCache()
 {
     string cacheName = ConfigurationSettings.AppSettings["CacheName"].ToString();
     try
     {
         _cache = NCache.InitializeCache(cacheName);
         _cache.Clear();
         return true;
     }
     catch(Exception e)
     {
         Console.WriteLine("Error occured while trying to initialize Cache named: [" + cacheName + "] \n"
                             + "Exception: " + e.Message);
         return false;
     }
 }
Пример #26
0
 internal void AddCache(string cacheId, Cache cache)
 {
     _caches[cacheId.ToLower()] = cache;
 }
Пример #27
0
        void GetCache(string sessionId, string key, out object value, string group, string subGroup, out Alachisoft.NCache.Web.Caching.Cache cache, bool isGet, ref LockHandle lockHandle, bool acquireLock)
        {
            value = null;

            //Implementation For Raiynair cookieless robots
            string sidPrefix = string.Empty;

            sidPrefix = sessionId.Substring(0, 4);
            if (!_settings.PrimaryCache.Contains(sidPrefix) && _isSessionCookieless)
            {
                _isSessionCookieless = false;
                cache = null;
                return;
            }
            _isSessionCookieless = false;

            cache = _caches[GetCache(sessionId)] as Alachisoft.NCache.Web.Caching.Cache;

            if (cache != null)
            {
                if (isGet)
                {
                    value = cache.Get(key, Alachisoft.NCache.Web.Caching.Cache.NoLockExpiration, ref lockHandle, acquireLock);
                }
            }
            else
            {
                if (_currentSessionCache == null || _currentSessionCache == string.Empty)
                {
                    IDictionaryEnumerator cacheDic = _caches.GetEnumerator();
                    do
                    {
                        /// This code will ensure that primary cache is used first to be lookedin.
                        if (cache == null)
                        {
                            cache = _caches[_primaryCache] as Alachisoft.NCache.Web.Caching.Cache;
                        }
                        else
                        {
                            cache = (Alachisoft.NCache.Web.Caching.Cache)cacheDic.Value;// select the cache from enumerator.
                            // primary cache is already traversed so no need to do it again.
                            if (cache.ToString().Equals(_primaryCache))
                            {
                                continue;
                            }
                        }

                        if (isGet)
                        {
                            value = cache.Get(key, Alachisoft.NCache.Web.Caching.Cache.NoLockExpiration, ref lockHandle, acquireLock);
                        }
                        else
                        {
                            if (cache.Contains(key))
                            {
                                value = new object();
                                break;
                            }
                        }
                    } while (cacheDic.MoveNext() && value == null);

                    if (value != null) // value was found from one of the caches. so use that cache for all further operations during this HTTP request.
                    {
                        _currentSessionCache = cache.ToString();
                    }
                    else// value wasn't found from any of the caches. thus use hte primary cache for all further operations.
                    {
                        _currentSessionCache = _primaryCache;
                    }
                }
                else
                {
                    cache = _caches[_currentSessionCache] as Alachisoft.NCache.Web.Caching.Cache;
                    if (isGet)
                    {
                        value = cache.Get(key, Alachisoft.NCache.Web.Caching.Cache.NoLockExpiration, ref lockHandle, acquireLock);
                    }
                }
            }
        }
Пример #28
0
 public NCacheQueryProvider(Cache cache)
 {
     _cache = cache;
 }
Пример #29
0
        public bool Load()
        {
            reloadTimer.Stop();

            bool loaded = true;
            try
            {
                cache = NCache.Web.Caching.NCache.InitializeCache(cacheName);
            }
            catch (Exception ex)
            {
                loaded = false;
                FileBasedTraceProvider.Current.WriteTrace(TraceSeverity.Exception, "Could not initialize cache due to exception: {0}", ex.Message);
            }
            Loaded = loaded;
            
            if (!loaded)
                ScheduleReload();
            return loaded;
        }
Пример #30
0
        void ScheduleReload()
        {
            Loaded = false;

            if (cache != null)
                cache.Dispose();

            cache = null;

            if (RetryInterval > 0)
            {
                reloadTimer.Interval = RetryInterval * 1000; // convert to miliseconds
                reloadTimer.Start();
            }
        }
Пример #31
0
 public void InitializeCache(string cache)
 {
     _cache = Alachisoft.NCache.Web.Caching.NCache.InitializeCache(cache);
 }
Пример #32
0
        public BulkInsertCommand(string[] keys, CacheItem[] items,  Cache parent, string cacheId)
        {
            base.name = "BulkInsertCommand";
            _parent = parent;
            base.BulkKeys = keys;
            _bulkInsertCommand = new Alachisoft.NCache.Common.Protobuf.BulkInsertCommand();
            _bulkInsertCommand.requestId = base.RequestId;

            for (int i = 0; i < keys.Length; i++)
            {
                CacheItem item = items[i];

                _insertCommand = new Alachisoft.NCache.Common.Protobuf.InsertCommand();
                _insertCommand.key = keys[i];

                Alachisoft.NCache.Caching.UserBinaryObject ubObject = Alachisoft.NCache.Caching.UserBinaryObject.CreateUserBinaryObject((byte[])item.Value);
                _insertCommand.data.AddRange(ubObject.DataList);

                if (item.AbsoluteExpiration != Cache.NoAbsoluteExpiration)
                    _insertCommand.absExpiration = item.AbsoluteExpiration.Ticks;

                if (item.SlidingExpiration != Cache.NoSlidingExpiration)
                    _insertCommand.sldExpiration = item.SlidingExpiration.Ticks;

                _insertCommand.flag = item.FlagMap.Data;
               _insertCommand.priority = (int)item.Priority;

                // Updated in Version 4.2 [Dated 20-Nov-2013; Author: Sami]
                ObjectQueryInfo objectQueryInfo = new ObjectQueryInfo();

                if (item.QueryInfo["query-info"] != null)
                    objectQueryInfo.queryInfo = ProtobufHelper.GetQueryInfoObj(item.QueryInfo["query-info"] as Hashtable);

                _insertCommand.objectQueryInfo = objectQueryInfo;

                short removeCallbackId = -1;
                short updateCallbackId = -1;
                EventDataFilter itemUpdateDataFilter = EventDataFilter.None;
                EventDataFilter itemRemovedDataFilter = EventDataFilter.None;

                if (item.CacheItemRemovedCallback != null)
                {
                    itemRemovedDataFilter = item.ItemRemovedCallabackDataFilter;
                    short[] callabackIds = _parent.EventManager.RegisterSelectiveEvent(item.CacheItemRemovedCallback, EventType.ItemRemoved, itemRemovedDataFilter);
                    removeCallbackId = callabackIds[1];
                }

                if (item.CacheItemUpdatedCallback != null)
                {
                    itemUpdateDataFilter = item.ItemUpdatedCallabackDataFilter;
                    short[] callabackIds = _parent.EventManager.RegisterSelectiveEvent(item.CacheItemUpdatedCallback, EventType.ItemUpdated, itemUpdateDataFilter);
                    updateCallbackId = callabackIds[0];
                }

                _insertCommand.removeCallbackId = removeCallbackId;
                _insertCommand.updateCallbackId = updateCallbackId;
                _insertCommand.updateDataFilter = (short)itemUpdateDataFilter;
                _insertCommand.removeDataFilter = (short)itemRemovedDataFilter;

                _bulkInsertCommand.insertCommand.Add(_insertCommand);
            }
        }
Пример #33
0
        private short _updateEventRegisrationSequenceId = REFSTART; //Significant difference from old callback numbers

        #endregion Fields

        #region Constructors

        internal EventManager(string cacheName, NCacheLogger logger,Cache cache)
        {
            _cacheName = cacheName;
            _logger = logger;
            _cache = cache;
        }
Пример #34
0
        public BulkAddCommand(string[] keys, CacheItem[] items, short onDsItemsAddedCallback,
                              Alachisoft.NCache.Web.Caching.Cache parent, string providerName, string cacheId, int methodOverload,
                              string clientId, short updateCallbackId, short removedCallbackId, EventDataFilter updateCallbackFilter,
                              EventDataFilter removeCallabackFilter, bool returnVersions,
                              CallbackType callbackType = Runtime.Events.CallbackType.PushBasedNotification)
        {
            base.name       = "BulkAddCommand";
            _parent         = parent;
            _methodOverload = methodOverload;
            _bulkAddCommand = new Alachisoft.NCache.Common.Protobuf.BulkAddCommand();
            _bulkAddCommand.datasourceItemAddedCallbackId = onDsItemsAddedCallback;
            _bulkAddCommand.providerName   = providerName;
            _bulkAddCommand.returnVersions = returnVersions;
            _bulkAddCommand.requestId      = base.RequestId;
            base.BulkKeys = keys;

            for (int i = 0; i < keys.Length; i++)
            {
                CacheItem item = items[i];

                _addCommand     = new Alachisoft.NCache.Common.Protobuf.AddCommand();
                _addCommand.key = keys[i];

                Alachisoft.NCache.Caching.UserBinaryObject ubObject =
                    Alachisoft.NCache.Caching.UserBinaryObject.CreateUserBinaryObject((byte[])item.Value);
                _addCommand.data.AddRange(ubObject.DataList);

                if (item.AbsoluteExpiration.Equals(Caching.Cache.DefaultAbsolute.ToUniversalTime()))
                {
                    _addCommand.absExpiration = 1;
                }
                else if (item.AbsoluteExpiration.Equals(Caching.Cache.DefaultAbsoluteLonger.ToUniversalTime()))
                {
                    _addCommand.absExpiration = 2;
                }
                else if (item.AbsoluteExpiration != Caching.Cache.NoAbsoluteExpiration)
                {
                    _addCommand.absExpiration = item.AbsoluteExpiration.Ticks;
                }

                if (item.SlidingExpiration.Equals(Caching.Cache.DefaultSliding))
                {
                    _addCommand.sldExpiration = 1;
                }
                else if (item.SlidingExpiration.Equals(Caching.Cache.DefaultSlidingLonger))
                {
                    _addCommand.sldExpiration = 2;
                }
                else if (item.SlidingExpiration != Caching.Cache.NoSlidingExpiration)
                {
                    _addCommand.sldExpiration = item.SlidingExpiration.Ticks;
                }

                _addCommand.flag       = item.FlagMap.Data;
                _addCommand.group      = item.Group;
                _addCommand.subGroup   = item.SubGroup;
                _addCommand.isResync   = item.IsResyncExpiredItems;
                _addCommand.priority   = (int)item.Priority;
                _addCommand.dependency = item.Dependency == null
                    ? null
                    : Alachisoft.NCache.Common.Util.DependencyHelper.GetProtoBufDependency(item.Dependency);

                _addCommand.clientID     = clientId;
                _addCommand.CallbackType = CallbackType(callbackType);


                ObjectQueryInfo objectQueryInfo = new ObjectQueryInfo();

                if (item.QueryInfo["query-info"] != null)
                {
                    objectQueryInfo.queryInfo =
                        ProtobufHelper.GetQueryInfoObj(item.QueryInfo["query-info"] as Hashtable);
                }

                if (item.QueryInfo["tag-info"] != null)
                {
                    objectQueryInfo.tagInfo = ProtobufHelper.GetTagInfoObj(item.QueryInfo["tag-info"] as Hashtable);
                }

                if (item.QueryInfo["named-tag-info"] != null)
                {
                    objectQueryInfo.namedTagInfo =
                        ProtobufHelper.GetNamedTagInfoObj(item.QueryInfo["named-tag-info"] as Hashtable, true);
                }


                _addCommand.objectQueryInfo = objectQueryInfo;


                EventDataFilter itemUpdateDataFilter  = updateCallbackFilter;
                EventDataFilter itemRemovedDataFilter = removeCallabackFilter;


                if (item.CacheItemRemovedCallback != null)
                {
                    itemRemovedDataFilter = item.ItemRemovedCallabackDataFilter;
                    short[] callabackIds = _parent.EventManager.RegisterSelectiveEvent(item.CacheItemRemovedCallback,
                                                                                       EventType.ItemRemoved, itemRemovedDataFilter);
                    removedCallbackId = callabackIds[1];
                }
                else if (item.ItemRemoveCallback != null)
                {
                    removedCallbackId     = _parent.GetCallbackId(item.ItemRemoveCallback);
                    itemRemovedDataFilter = EventDataFilter.DataWithMetadata;
                }


                if (item.CacheItemUpdatedCallback != null)
                {
                    itemUpdateDataFilter = item.ItemUpdatedCallabackDataFilter;
                    short[] callabackIds = _parent.EventManager.RegisterSelectiveEvent(item.CacheItemUpdatedCallback,
                                                                                       EventType.ItemUpdated, itemUpdateDataFilter);
                    updateCallbackId = callabackIds[0];
                }
                else if (item.ItemUpdateCallback != null)
                {
                    updateCallbackId     = _parent.GetCallbackId(item.ItemUpdateCallback);
                    itemUpdateDataFilter = EventDataFilter.None;
                }


                _addCommand.removeCallbackId = removedCallbackId;
                _addCommand.updateCallbackId = updateCallbackId;
                _addCommand.updateDataFilter = (short)itemUpdateDataFilter;
                _addCommand.removeDataFilter = (short)itemRemovedDataFilter;

                _addCommand.resyncProviderName = item.ResyncProviderName;

                if (item.SyncDependency != null)
                {
                    _addCommand.syncDependency         = new Alachisoft.NCache.Common.Protobuf.SyncDependency();
                    _addCommand.syncDependency.key     = item.SyncDependency.Key;
                    _addCommand.syncDependency.cacheId = item.SyncDependency.CacheId;
                    _addCommand.syncDependency.server  = item.SyncDependency.Server;
                    _addCommand.syncDependency.port    = item.SyncDependency.Port;
                }

                _bulkAddCommand.addCommand.Add(_addCommand);
            }
        }