Example #1
0
        internal CacheEventDescriptor RegisterGeneralEvents(CacheDataNotificationCallback callback, EventType eventType, EventDataFilter datafilter)
        {
            if (callback != null)
            {
                //Avoiding new ResourcePool(inside = new Hashtable) at constructor level
                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);

                //Registers the handl)
                bool registeredDescriptor = RegisterGeneralDiscriptor(discriptor, EventsUtil.GetEventTypeInternal(eventType));
                if (!registeredDescriptor)
                {
                    return(null);
                }

                return(discriptor);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        //Creation is CacheDesriptor's responsibility
        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);
        }
Example #3
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(EventsUtil.GetEventTypeInternal(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;
                                }
                            }
                        }
                    }


                    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(EventsUtil.GetEventTypeInternal(type));
                    }
                    else if (registrationSequenceId != -1)
                    {
                        //only caused update of data filter either upgrade or downgrade
                        _cache.RegisterCacheNotificationDataFilter(EventsUtil.GetEventTypeInternal(type), maxDataFilter, registrationSequenceId);
                    }
                }
            }
            return(null);
        }
Example #4
0
        private bool RegisterGeneralDiscriptor(CacheEventDescriptor discriptor, EventTypeInternal eventType)
        {
            if (discriptor == null)
            {
                return(false); //FAIL CONDITION
            }
            EventHandle handle = null;

            foreach (EventTypeInternal type in Enum.GetValues(typeof(EventTypeInternal)))
            {
                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 EventTypeInternal.ItemAdded:
                        if (discriptor.DataFilter > _generalAddDataFilter || _addEventRegistrationSequence == REFSTART)
                        {
                            registrationUpdated    = true;
                            registrationSequenceId = ++_addEventRegistrationSequence;
                            _generalAddDataFilter  = discriptor.DataFilter;
                        }
                        else
                        {
                            registrationSequenceId = _addEventRegistrationSequence;
                        }
                        break;

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

                    case EventTypeInternal.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);
        }
Example #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(EventsUtil.GetEventTypeInternal(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 bitSet = new BitSet();

                        if (_cache.SerializationFormat == Common.Enum.SerializationFormat.Json)
                        {
                            bitSet.SetBit(BitSetConstants.JsonData);
                        }

                        if (item != null)
                        {
                            item.SetValue(_cache.SafeDeserialize <object>(item.GetValue <object>(), _cache.SerializationContext, bitSet, UserObjectType.CacheItem));
                        }

                        if (oldItem != null)
                        {
                            oldItem.SetValue(_cache.SafeDeserialize <object>(oldItem.GetValue <object>(), _cache.SerializationContext, bitSet, UserObjectType.CacheItem));
                        }

                        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
                            //TODO: ALACHISOFT (BeginInvoke is not supported in .Net Core thus using TaskFactory)
                            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());
                }
            }
        }
Example #6
0
 internal CacheEventArg(string key, string cachename, EventType eventType, EventCacheItem item, CacheEventDescriptor discriptor, CacheItemRemovedReason reason)
     : base(cachename, eventType, item)
 {
     _reason     = reason;
     _descriptor = discriptor;
 }
Example #7
0
 internal CacheEventArg(string key, string cachename, EventType eventType, EventCacheItem item, CacheEventDescriptor discriptor, EventCacheItem olditem)
     : base(cachename, eventType, item, olditem)
 {
     _descriptor = discriptor;
 }