Пример #1
0
        internal override CacheEventDescriptor RegisterCacheNotification(CacheDataNotificationCallback cacheDataNotificationCallback, Runtime.Events.EventType eventType, Runtime.Events.EventDataFilter datafilter)
        {
            CacheEventDescriptor result = null;
            string exceptionMessage     = null;

            try
            {
                result = _webCache.RegisterCacheNotification(cacheDataNotificationCallback, eventType, datafilter);
            }
            catch (Exception e)
            {
                exceptionMessage = e.Message;
                throw;
            }
            finally
            {
                try
                {
                    if (_debugConfigurations.IsInLoggingInterval())
                    {
                        APILogItem logItem = new APILogItem();
                        logItem.Signature        = "RegisterCacheNotification(string key, CacheDataNotificationCallback selectiveCacheDataNotificationCallback, Runtime.Events.EventType eventType, Runtime.Events.EventDataFilter datafilter)";
                        logItem.ExceptionMessage = exceptionMessage;
                        _apiLogger.Log(logItem);
                    }
                }
                catch (Exception)
                { }
            }
            return(result);
        }
Пример #2
0
        internal CacheEventDescriptor RegisterGeneralEvents(CacheDataNotificationCallback callback, EventType eventType,
                                                            EventDataFilter datafilter)
        {
            if (callback != null)
            {
                if (_addEventPool == null)
                {
                    _addEventPool = new ResourcePool();
                }

                if (_removeEventPool == null)
                {
                    _removeEventPool = new ResourcePool();
                }

                if (_updateEventPool == null)
                {
                    _updateEventPool = new ResourcePool();
                }

                CacheEventDescriptor discriptor =
                    CacheEventDescriptor.CreateCacheDiscriptor(eventType, _cacheName, callback, datafilter);

                if (!RegisterGeneralDiscriptor(discriptor, eventType))
                {
                    return(null);
                }

                return(discriptor);
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
 internal CacheEventArg(string key, string cachename, EventType eventType, EventCacheItem item,
                        CacheEventDescriptor discriptor, CacheItemRemovedReason reason)
     : base(cachename, eventType, item)
 {
     _reason     = reason;
     _descriptor = discriptor;
 }
Пример #4
0
        //Creation is CacheDesriptor's responsiblity
        static internal CacheEventDescriptor CreateCacheDiscriptor(EventType eventType, string cacheName, CacheDataNotificationCallback callback, EventDataFilter datafilter)
        {
            CacheEventDescriptor descriptor = new CacheEventDescriptor();

            descriptor.RegisteredAgainst             = eventType;
            descriptor.CacheName                     = cacheName;
            descriptor.CacheDataNotificationCallback = callback;
            descriptor.IsRegistered                  = true;
            descriptor.DataFilter                    = datafilter;
            return(descriptor);
        }
Пример #5
0
        /// <summary>
        /// TheadSafe and no locks internally
        /// </summary>
        /// <param name="key"></param>
        /// <param name="eventType">Should contain one type i.e. should not be used as a flag.
        /// Every EventType should be executed from another thread</param>
        /// <param name="item"></param>
        /// <param name="oldItem"></param>
        /// <param name="reason"></param>
        /// <param name="notifyAsync"></param>
        internal void RaiseGeneralCacheNotification(string key, EventType eventType, EventCacheItem item,
                                                    EventCacheItem oldItem, CacheItemRemovedReason reason, bool notifyAsync)
        {
            try
            {
                object[] registeredDiscriptors = null;

                ResourcePool eventPool = GetEventPool(eventType);
                if (eventPool != null)
                {
                    registeredDiscriptors = eventPool.GetAllResourceKeys();
                }

                if (registeredDiscriptors != null && registeredDiscriptors.Length > 0)
                {
                    for (int i = 0; i < registeredDiscriptors.Length; i++)
                    {
                        CacheEventDescriptor discriptor = registeredDiscriptors[i] as CacheEventDescriptor;

                        if (discriptor == null)
                        {
                            continue;
                        }

                        var arg = CreateCacheEventArgument(discriptor.DataFilter, key, _cacheName, eventType, item, oldItem,
                                                           reason);
                        arg.Descriptor = discriptor;

                        if (notifyAsync)
                        {
#if !NETCORE
                            discriptor.CacheDataNotificationCallback.BeginInvoke(key, arg, asyn, null);
#elif NETCORE
                            TaskFactory factory = new TaskFactory();
                            Task        task    = factory.StartNew(() => discriptor.CacheDataNotificationCallback(key, arg));
#endif
                        }
                        else
                        {
                            discriptor.CacheDataNotificationCallback.Invoke(key, arg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (_logger != null && _logger.IsErrorEnabled)
                {
                    _logger.CriticalInfo(ex.ToString());
                }
            }
        }
Пример #6
0
 internal CacheEventArg(string key, string cachename, EventType eventType, EventCacheItem item, CacheEventDescriptor discriptor, EventCacheItem olditem)
     : base(cachename, eventType, item, olditem)
 {
     _descriptor = discriptor;
 }
Пример #7
0
 //Creation is CacheDesriptor's responsiblity
 internal static CacheEventDescriptor CreateCacheDiscriptor(EventType eventType, string cacheName, CacheDataNotificationCallback callback, EventDataFilter datafilter)
 {
     CacheEventDescriptor descriptor = new CacheEventDescriptor();
     descriptor.RegisteredAgainst = eventType;
     descriptor.CacheName = cacheName;
     descriptor.CacheDataNotificationCallback = callback;
     descriptor.IsRegistered = true;
     descriptor.DataFilter = datafilter;
     return descriptor;
 }
Пример #8
0
 internal CacheEventArg(string key, string cachename, EventType eventType, EventCacheItem item, CacheEventDescriptor discriptor, CacheItemRemovedReason reason)
     : base(cachename, eventType, item)
 {
     _reason = reason;
     _descriptor = discriptor;
 }
Пример #9
0
 internal CacheEventArg(string key, string cachename, EventType eventType, EventCacheItem item, CacheEventDescriptor discriptor, EventCacheItem olditem)
     : base(cachename, eventType, item, olditem)
 {
     _descriptor = discriptor;
 }
Пример #10
0
        internal EventHandle UnregisterDiscriptor(CacheEventDescriptor discriptor)
        {
            if (discriptor == null || !discriptor.IsRegistered)
            {
                return(null);
            }

            foreach (EventType type in Enum.GetValues(typeof(EventType)))
            {
                ResourcePool pool = null;

                #region Pool selection

                if ((type & discriptor.RegisteredAgainst) != 0)
                {
                    pool = GetEventPool(type);
                }

                if (pool == null)
                {
                    continue;
                }

                #endregion

                short           registrationSequenceId = -1;
                bool            unregisterNotification = false;
                EventDataFilter maxDataFilter          = EventDataFilter.None;

                lock (SyncLockGeneral)
                {
                    object retVal = pool.RemoveResource(discriptor);

                    if (retVal == null)
                    {
                        continue;
                    }
                    unregisterNotification = pool.Count == 0;

                    if (!unregisterNotification)
                    {
                        object[] pooledDescriptors = pool.GetAllResourceKeys();

                        if (pooledDescriptors != null)
                        {
                            for (int i = 0; i < pooledDescriptors.Length; i++)
                            {
                                CacheEventDescriptor pooledDescriptor = pooledDescriptors[i] as CacheEventDescriptor;

                                if (pooledDescriptor.DataFilter > maxDataFilter)
                                {
                                    maxDataFilter = pooledDescriptor.DataFilter;
                                }

                                if (maxDataFilter == EventDataFilter.DataWithMetadata)
                                {
                                    break;
                                }
                            }
                        }
                    }


                    discriptor.IsRegistered = false;

                    //keeps a sequence number
                    switch (type)
                    {
                    case EventType.ItemAdded:
                        //Data filter is being updated
                        if (maxDataFilter != _generalAddDataFilter)
                        {
                            _generalAddDataFilter  = maxDataFilter;
                            registrationSequenceId = ++_addEventRegistrationSequence;
                        }

                        if (unregisterNotification)
                        {
                            _generalAddDataFilter = EventDataFilter.None;
                        }
                        break;

                    case EventType.ItemRemoved:
                        if (maxDataFilter != _generalRemoveDataFilter)
                        {
                            _generalRemoveDataFilter = maxDataFilter;
                            registrationSequenceId   = ++_removeEventRegistrationSequenceId;
                        }

                        if (unregisterNotification)
                        {
                            _generalAddDataFilter = EventDataFilter.None;
                        }
                        break;

                    case EventType.ItemUpdated:
                        if (maxDataFilter != _generalUpdateDataFilter)
                        {
                            _generalUpdateDataFilter = maxDataFilter;
                            registrationSequenceId   = ++_updateEventRegisrationSequenceId;
                        }

                        if (unregisterNotification)
                        {
                            _generalAddDataFilter = EventDataFilter.None;
                        }

                        break;
                    }
                }

                if (_cache != null)
                {
                    if (unregisterNotification)
                    {
                        //client is no more interested in event, therefore unregister it from server
                        _cache.UnregiserGeneralCacheNotification(type);
                    }
                    else if (registrationSequenceId != -1)
                    {
                        //only caused update of data filter either upgrade or downgrade
                        _cache.RegisterCacheNotificationDataFilter(type, maxDataFilter, registrationSequenceId);
                    }
                }
            }

            return(null);
        }
Пример #11
0
        private bool RegisterGeneralDiscriptor(CacheEventDescriptor discriptor, EventType eventType)
        {
            if (discriptor == null)
            {
                return(false); //FAIL CONDITION
            }
            EventHandle handle = null;

            foreach (EventType type in Enum.GetValues(typeof(EventType)))
            {
                ResourcePool pool = null;
                bool         registrationUpdated = false;

                #region Pool selection

                if ((type & eventType) != 0)
                {
                    pool = GetEventPool(type);
                }

                if (pool == null)
                {
                    continue;
                }

                #endregion

                short registrationSequenceId = -1;

                lock (SyncLockGeneral)
                {
                    pool.AddResource(discriptor, 1); // Everytime a new Discriptor is forcefully created

                    //Keeps a sequence number

                    switch (type)
                    {
                    case EventType.ItemAdded:
                        if (discriptor.DataFilter > _generalAddDataFilter ||
                            _addEventRegistrationSequence == REFSTART)
                        {
                            registrationUpdated    = true;
                            registrationSequenceId = ++_addEventRegistrationSequence;
                            _generalAddDataFilter  = discriptor.DataFilter;
                        }
                        else
                        {
                            registrationSequenceId = _addEventRegistrationSequence;
                        }

                        break;

                    case EventType.ItemRemoved:
                        if (discriptor.DataFilter > _generalRemoveDataFilter ||
                            _removeEventRegistrationSequenceId == REFSTART)
                        {
                            registrationUpdated      = true;
                            registrationSequenceId   = ++_removeEventRegistrationSequenceId;
                            _generalRemoveDataFilter = discriptor.DataFilter;
                        }
                        else
                        {
                            registrationSequenceId = _removeEventRegistrationSequenceId;
                        }

                        break;

                    case EventType.ItemUpdated:
                        if (discriptor.DataFilter > _generalUpdateDataFilter ||
                            _updateEventRegisrationSequenceId == REFSTART)
                        {
                            registrationUpdated      = true;
                            registrationSequenceId   = ++_updateEventRegisrationSequenceId;
                            _generalUpdateDataFilter = discriptor.DataFilter;
                        }
                        else
                        {
                            registrationSequenceId = _updateEventRegisrationSequenceId;
                        }

                        break;
                    }

                    //Although the handle doesnt matter in general events
                    if (handle == null)
                    {
                        handle = new EventHandle(registrationSequenceId);
                    }
                }

                if (_cache != null && registrationSequenceId != -1)
                {
                    _cache.RegisterCacheNotificationDataFilter(type, discriptor.DataFilter, registrationSequenceId);
                }
            }

            discriptor.IsRegistered = true;
            discriptor.Handle       = handle;
            return(true);
        }