Ejemplo n.º 1
0
        public static ExpirationHint ReadExpHint(CompactReader reader)
        {
            ExpirationHintType expHint = ExpirationHintType.Parent;

            expHint = (ExpirationHintType)reader.ReadInt16();
            ExpirationHint tmpObj = null;

            switch (expHint)
            {
            case ExpirationHintType.NULL:
                return(null);

            case ExpirationHintType.Parent:
                tmpObj = (ExpirationHint)reader.ReadObject();
                return((ExpirationHint)tmpObj);

            case ExpirationHintType.FixedExpiration:
                FixedExpiration fe = new FixedExpiration();
                ((ICompactSerializable)fe).Deserialize(reader);
                return((ExpirationHint)fe);

            case ExpirationHintType.IdleExpiration:
                IdleExpiration ie = new IdleExpiration();
                ((ICompactSerializable)ie).Deserialize(reader);
                return((ExpirationHint)ie);

            default:
                break;
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static FixedIdleExpiration Create(PoolManager poolManager, TimeSpan idleTime, DateTime absoluteTime)
        {
            var expiration = Create(poolManager);

            Construct(expiration, FixedExpiration.Create(poolManager, absoluteTime), IdleExpiration.Create(poolManager, idleTime));

            return(expiration);
        }
Ejemplo n.º 3
0
        public static ExpirationHint ReadExpHint(CompactReader reader, PoolManager poolManager)
        {
            ExpirationHintType expHint = ExpirationHintType.Parent;

            expHint = (ExpirationHintType)reader.ReadInt16();

            switch (expHint)
            {
            case ExpirationHintType.NULL:
                return(null);

            case ExpirationHintType.Parent:
                return((ExpirationHint)reader.ReadObject());

            case ExpirationHintType.FixedExpiration:
                var fe = FixedExpiration.Create(poolManager);
                fe.Deserialize(reader);
                return(fe);

            case ExpirationHintType.TTLExpiration:
                var ttle = TTLExpiration.Create(poolManager);
                ((ICompactSerializable)ttle).Deserialize(reader);
                return(ttle);

            case ExpirationHintType.FixedIdleExpiration:
                var fie = FixedIdleExpiration.Create(poolManager);
                ((ICompactSerializable)fie).Deserialize(reader);
                return(fie);

#if !(DEVELOPMENT || CLIENT)
            case ExpirationHintType.NodeExpiration:
                var ne = NodeExpiration.Create(poolManager);
                ((ICompactSerializable)ne).Deserialize(reader);
                return(ne);
#endif
            case ExpirationHintType.IdleExpiration:
                var ie = IdleExpiration.Create(poolManager);
                ((ICompactSerializable)ie).Deserialize(reader);
                return(ie);

            case ExpirationHintType.AggregateExpirationHint:
                var aeh = AggregateExpirationHint.Create(poolManager);
                ((ICompactSerializable)aeh).Deserialize(reader);
                return(aeh);

            case ExpirationHintType.DependencyHint:
                break;

            default:
                break;
            }
            return(null);
        }
Ejemplo n.º 4
0
        public static ExpirationHint GetExpirationHint(DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            ExpirationHint hint = null;

            if (DateTime.MaxValue.ToUniversalTime().Equals(absoluteExpiration) && TimeSpan.Zero.Equals(slidingExpiration))
                return null;

            if (DateTime.MaxValue.ToUniversalTime().Equals(absoluteExpiration))
                hint = new IdleExpiration(slidingExpiration);
            else
            {
                absoluteExpiration = absoluteExpiration.ToUniversalTime();
                hint = new FixedExpiration(absoluteExpiration);
            }

            return hint;
        }
Ejemplo n.º 5
0
        public static ExpirationHint GetExpirationHint(DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            ExpirationHint hint = null;

            if (DateTime.MaxValue.ToUniversalTime().Equals(absoluteExpiration) && TimeSpan.Zero.Equals(slidingExpiration))
            {
                return(null);
            }

            if (DateTime.MaxValue.ToUniversalTime().Equals(absoluteExpiration))
            {
                hint = new IdleExpiration(slidingExpiration);
            }
            else
            {
                absoluteExpiration = absoluteExpiration.ToUniversalTime();
                hint = new FixedExpiration(absoluteExpiration);
            }

            return(hint);
        }
Ejemplo n.º 6
0
        private static ExpirationHint GetExpirationHint(PoolManager poolManager, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            ExpirationHint hint = null;

            if (ExpirationConstants.AbsoluteNoneExpiration.ToUniversalTime().Equals(absoluteExpiration) && ExpirationConstants.SlidingNoneExpiration.Equals(slidingExpiration))
            {
                return(null);
            }

            if (!ExpirationConstants.AbsoluteNoneExpiration.ToUniversalTime().Equals(absoluteExpiration.ToUniversalTime()))
            {
                absoluteExpiration = absoluteExpiration.ToUniversalTime();
                hint = FixedExpiration.Create(poolManager, absoluteExpiration);
            }
            else if (!ExpirationConstants.SlidingNoneExpiration.Equals(slidingExpiration))

            {
                hint = IdleExpiration.Create(poolManager, slidingExpiration);
            }

            return(hint);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Called by the scheduler to remove the items that has expired
        /// </summary>
        public bool Expire()
        {
            //indicates whether some items expired during this interval or not...
            bool expired = false;

            //if user has updated the file then the new values will be reloaded.
            _sleepInterval    = ServiceConfiguration.ExpirationBulkRemoveDelay;
            _removeThreshhold = ServiceConfiguration.ExpirationBulkRemoveSize;

            CacheBase cacheInst = _context.CacheImpl;
            CacheBase cache     = _context.CacheInternal;
            Cache     rootCache = _context.CacheRoot;

            if (cache == null)
            {
                throw new InvalidOperationException("No cache instance defined");
            }

            bool allowExpire = AllowClusteredExpiry;

            //in case of replication and por, only the coordinator/sub-coordinator is responsible to expire the items.
            if (!allowExpire)
            {
                return(false);
            }
            ClusteredArrayList selectedKeys = new ClusteredArrayList();
            int        oldItemsCount        = 0;
            HashVector oldeItems            = null;

            try
            {
                StartLogging();

                DateTime startTime   = DateTime.Now;
                int      currentTime = AppUtil.DiffSeconds(startTime);
                int      cleanSize   = (int)Math.Ceiling(cache.Count * _cleanRatio);

                //set the flag that we are going to expire the items.

                if (_cacheLastAccessLoggingIntervalPassed >= _cacheLastAccessLoggingInterval)
                {
                    _cacheLastAccessLoggingInterval     = CacheLastAccessLoggingInterval;
                    _cacheLastAccessCountEnabled        = IsCacheLastAccessCountEnabled;
                    _cacheLastAccessCountLoggingEnabled = IsCacheLastAccessLoggingEnabled;
                    _cacheLastAccessInterval            = CacheLastAccessCountInterval;
                }
                else
                {
                    _cacheLastAccessLoggingIntervalPassed++;
                }


                if (_cacheLastAccessCountEnabled && _cacheLastAccessCountLoggingEnabled)
                {
                    if (_cacheLastAccessLoggingIntervalPassed >= _cacheLastAccessLoggingInterval)
                    {
                        _cacheLastAccessLoggingIntervalPassed = 0;
                        oldeItems = new HashVector();
                    }
                }
                lock (_mainIndex.SyncRoot)
                {
                    IDictionaryEnumerator em = _mainIndex.GetEnumerator();

                    if (em != null)
                    {
                        while (em.MoveNext())
                        {
                            ExpirationHint hint = em.Value as ExpirationHint;
                            if (hint != null && _cacheLastAccessCountEnabled && hint is IdleExpiration)
                            {
                                IdleExpiration slidingExpHint = hint as IdleExpiration;
                                TimeSpan       diff           = AppUtil.GetDateTime(AppUtil.DiffSeconds(DateTime.Now)) - AppUtil.GetDateTime(slidingExpHint.LastAccessTime);
                                if (diff.TotalMinutes >= _cacheLastAccessInterval)
                                {
                                    oldItemsCount++;
                                    if (oldeItems != null)
                                    {
                                        oldeItems.Add(em.Key, null);
                                    }
                                }
                            }
                            if (hint == null || hint.SortKey.CompareTo(currentTime) >= 0)
                            {
                                continue;
                            }

                            if (hint.DetermineExpiration(_context))
                            {
                                selectedKeys.Add(em.Key);
                                if (cleanSize > 0 && selectedKeys.Count == cleanSize)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                if (NCacheLog.IsInfoEnabled)
                {
                    NCacheLog.Info("ExpirationManager.Expire()", String.Format("Expiry time for {0}/{1} Items: " + (DateTime.UtcNow - startTime), selectedKeys.Count, /*_expiryIndex.KeyCount*/ cache.Count));
                }
            }
            catch (Exception e)
            {
                NCacheLog.Error("ExpirationManager.Expire(bool)", "LocalCache(Expire): " + e.ToString());
            }
            finally
            {
                _context.PerfStatsColl.IncrementCacheLastAccessCountStats(oldItemsCount);

                ApplyLoggs();
                ClusteredArrayList dependentItems = new ClusteredArrayList();
                DateTime           startTime      = DateTime.Now;

                HashVector expiredItemTable = new HashVector();

                expiredItemTable.Add(ItemRemoveReason.Expired, selectedKeys);//Time based expiration
                try
                {
                    IDictionaryEnumerator ide = expiredItemTable.GetEnumerator();

                    while (ide.MoveNext())
                    {
                        selectedKeys = ide.Value as ClusteredArrayList;
                        ItemRemoveReason removedReason = (ItemRemoveReason)ide.Key;

                        if (selectedKeys.Count > 0)
                        {
                            //new architectural changes begins from here.

                            ClusteredArrayList keysTobeRemoved = new ClusteredArrayList();

                            for (int i = 0; i < selectedKeys.Count && !_cacheCleared; i++)
                            {
                                keysTobeRemoved.Add(selectedKeys[i]);
                                if (keysTobeRemoved.Count % _removeThreshhold == 0)
                                {
                                    try
                                    {
                                        if (this.IsDisposed)
                                        {
                                            break;
                                        }

                                        OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                                        object[][]       keysExposed      = keysTobeRemoved.ToInternalArray();
                                        foreach (object[] collection in keysExposed)
                                        {
                                            cache.RemoveSync(collection, removedReason, false, operationContext);
                                        }
                                        //set the flag that item has expired from cache...
                                        expired = true;

                                        if (_context.PerfStatsColl != null)
                                        {
                                            _context.PerfStatsColl.IncrementExpiryPerSecStatsBy(keysTobeRemoved.Count);
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        NCacheLog.Error("ExpiryManager.Expire", "an error occurred while removing expired items. Error " + e.ToString());
                                    }
                                    keysTobeRemoved.Clear();
                                    //we stop the activity of the current thread so that normal user operation is not affected.
                                    Thread.Sleep(_sleepInterval);
                                }
                            }

                            if (!this.IsDisposed && keysTobeRemoved.Count > 0)
                            {
                                try
                                {
                                    OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                                    object[][]       keysExposed      = keysTobeRemoved.ToInternalArray();
                                    foreach (object[] keyCollection in keysExposed)
                                    {
                                        cache.RemoveSync(keyCollection, removedReason, false, operationContext);
                                    }
                                    //set the flag that item has expired from cache...
                                    expired = true;
                                    if (_context.PerfStatsColl != null)
                                    {
                                        _context.PerfStatsColl.IncrementExpiryPerSecStatsBy(keysTobeRemoved.Count);
                                    }
                                }
                                catch (Exception e)
                                {
                                    NCacheLog.Error("ExpiryManager.Expire", "an error occurred while removing expired items. Error " + e.ToString());
                                }
                            }
                        }
                    }
                }
                finally
                {
                    _transitoryIndex.Clear();
                    lock (this)
                    {
                        _cacheCleared = false;
                    }

                    if (oldeItems != null)
                    {
                        StringBuilder         sb  = new StringBuilder();
                        IDictionaryEnumerator ide = oldeItems.GetEnumerator();
                        int count = 1;
                        while (ide.MoveNext())
                        {
                            sb.Append(ide.Key + ", ");

                            if (count % 10 == 0)
                            {
                                sb.Append("\r\n");
                                count = 1;
                            }
                            else
                            {
                                count++;
                            }
                        }

                        NCacheLog.Info(sb.ToString().Trim());
                    }
                }
            }
            return(expired);
        }
Ejemplo n.º 8
0
        public static ExpirationHint GetExpirationHintObj(long absoluteExpiration, long slidingExpiration, string serializationContext)
        {
            ExpirationHint hint = null;
            //We expect Web.Cache to send in UTC DateTime, AbsoluteExpiration is dealt in UTC
            if (absoluteExpiration != 0 && absoluteExpiration != DateTime.MaxValue.ToUniversalTime().Ticks) hint = new FixedExpiration(new DateTime(absoluteExpiration, DateTimeKind.Utc));
            if (slidingExpiration != 0) hint = new IdleExpiration(new TimeSpan(slidingExpiration));


            return hint;
        }
Ejemplo n.º 9
0
        private static ExpirationHint GetExpirationHint(ExpirationPolicy policy, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            ExpirationHint hint = null;

            if (policy.SlidingExpiration.LongerEnabled || policy.SlidingExpiration.DefaultEnabled || policy.AbsoluteExpiration.LongerEnabled || policy.AbsoluteExpiration.DefaultEnabled)
            {
                if ((absoluteExpiration == AbsoluteDefaultExpiration.ToUniversalTime() || absoluteExpiration == AbsoluteLongerExpiration.ToUniversalTime() || (slidingExpiration == SlidingDefaultExpiration || slidingExpiration == SlidingLongerExpiration)))
                {
                    #region In case user provides sliding expiration as an enum

                    if (absoluteExpiration == DateTime.MaxValue)
                    {
                        if (slidingExpiration == SlidingLongerExpiration && policy.SlidingExpiration.LongerEnabled)
                        {
                            hint = new IdleExpiration(new TimeSpan(policy.SlidingExpiration.Longer * TICKS));
                        }
                        else if (slidingExpiration == SlidingDefaultExpiration && policy.SlidingExpiration.DefaultEnabled)
                        {
                            hint = new IdleExpiration(new TimeSpan(policy.SlidingExpiration.Default * TICKS));
                        }
                    }
                    #endregion

                    #region In case user provides absolute expiration as an enum

                    if (slidingExpiration == TimeSpan.Zero)
                    {
                        if (absoluteExpiration == AbsoluteLongerExpiration.ToUniversalTime() && policy.AbsoluteExpiration.LongerEnabled)// check if Longer is enabled in config
                        {
                            hint = new FixedExpiration(DateTime.Now.AddSeconds(policy.AbsoluteExpiration.Longer).ToUniversalTime());
                        }
                        else if (absoluteExpiration == AbsoluteDefaultExpiration.ToUniversalTime() && policy.AbsoluteExpiration.DefaultEnabled)
                        {
                            hint = new FixedExpiration(DateTime.Now.AddSeconds(policy.AbsoluteExpiration.Default).ToUniversalTime());
                        }
                    }
                    #endregion

                    // If no expirations were enabled we have to assign the default expiration
                }
                else
                {
                    if (hint == null)
                    {
                        hint = new FixedExpiration(DateTime.Now.AddSeconds(policy.AbsoluteExpiration.Default).ToUniversalTime());
                    }
                }
            }

            if (DateTime.MaxValue.ToUniversalTime().Equals(absoluteExpiration) && TimeSpan.Zero.Equals(slidingExpiration))
            {
                return(null);
            }

            if (DateTime.MaxValue.ToUniversalTime().Equals(absoluteExpiration))
            {
                hint = new IdleExpiration(slidingExpiration);
            }
            else
            {
                absoluteExpiration = absoluteExpiration.ToUniversalTime();
                hint = new FixedExpiration(absoluteExpiration);
            }

            return(hint);
        }
Ejemplo n.º 10
0
        public static ExpirationHint ReadExpHint(CompactReader reader)
        {
            ExpirationHintType expHint = ExpirationHintType.Parent;

            expHint = (ExpirationHintType)reader.ReadInt16();
            ExpirationHint tmpObj = null;

            switch (expHint)
            {
            case ExpirationHintType.NULL:
                return(null);

            case ExpirationHintType.Parent:
                tmpObj = (ExpirationHint)reader.ReadObject();
                return((ExpirationHint)tmpObj);

            case ExpirationHintType.FixedExpiration:
                FixedExpiration fe = new FixedExpiration();
                ((ICompactSerializable)fe).Deserialize(reader);
                return((ExpirationHint)fe);

            case ExpirationHintType.TTLExpiration:
                TTLExpiration ttle = new TTLExpiration();
                ((ICompactSerializable)ttle).Deserialize(reader);
                return((ExpirationHint)ttle);

            case ExpirationHintType.TTLIdleExpiration:
                TTLIdleExpiration ttlie = new TTLIdleExpiration();
                ((ICompactSerializable)ttlie).Deserialize(reader);
                return((ExpirationHint)ttlie);

            case ExpirationHintType.FixedIdleExpiration:
                FixedIdleExpiration fie = new FixedIdleExpiration();
                ((ICompactSerializable)fie).Deserialize(reader);
                return((ExpirationHint)fie);

            case ExpirationHintType.FileDependency:
                FileDependency fd = new FileDependency();
                ((ICompactSerializable)fd).Deserialize(reader);
                return((ExpirationHint)fd);

            case ExpirationHintType.KeyDependency:
                KeyDependency kd = new KeyDependency();
                ((ICompactSerializable)kd).Deserialize(reader);
                return((ExpirationHint)kd);

#if !(DEVELOPMENT || CLIENT)
            case ExpirationHintType.NodeExpiration:
                NodeExpiration ne = new NodeExpiration();
                ((ICompactSerializable)ne).Deserialize(reader);
                return((ExpirationHint)ne);
#endif
            case ExpirationHintType.Sql7CacheDependency:
                Sql7CacheDependency s7cd = new Sql7CacheDependency();
                ((ICompactSerializable)s7cd).Deserialize(reader);
                return((ExpirationHint)s7cd);

            case ExpirationHintType.OleDbCacheDependency:
                OleDbCacheDependency oledbDependency = new OleDbCacheDependency();
                ((ICompactSerializable)oledbDependency).Deserialize(reader);
                return((ExpirationHint)oledbDependency);

            case ExpirationHintType.SqlYukonCacheDependency:
                SqlYukonCacheDependency sycd = new SqlYukonCacheDependency();
                ((ICompactSerializable)sycd).Deserialize(reader);
                return((ExpirationHint)sycd);

            case ExpirationHintType.OracleCacheDependency:
                OracleCacheDependency orclcd = new OracleCacheDependency();
                ((ICompactSerializable)orclcd).Deserialize(reader);
                return((ExpirationHint)orclcd);


            case ExpirationHintType.IdleExpiration:
                IdleExpiration ie = new IdleExpiration();
                ((ICompactSerializable)ie).Deserialize(reader);
                return((ExpirationHint)ie);

            case ExpirationHintType.AggregateExpirationHint:
                AggregateExpirationHint aeh = new AggregateExpirationHint();
                ((ICompactSerializable)aeh).Deserialize(reader);
                return((ExpirationHint)aeh);

            case ExpirationHintType.DBCacheDependency:
                DBCacheDependency dbcd = new DBCacheDependency();
                ((ICompactSerializable)dbcd).Deserialize(reader);
                return((ExpirationHint)dbcd);

            case ExpirationHintType.ExtensibleDependency:
                ExtensibleDependency ed = new ExtensibleDependency();
                ed = (ExtensibleDependency)reader.ReadObject();
                return((ExpirationHint)ed);

            case ExpirationHintType.NosDBCacheDependency:
                NosDBCacheDependency nosDbd = new NosDBCacheDependency();
                ((ICompactSerializable)nosDbd).Deserialize(reader);
                return(nosDbd);

            case ExpirationHintType.DependencyHint:
                break;

            default:
                break;
            }
            return(null);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Called by the scheduler to remove the items that has expired
        /// </summary>
        public bool Expire()
        {
            //indicates whether some items expired during this interval or not...
            bool expired = false;

            //if user has updated the file then the new values will be reloaded.

            _sleepInterval    = ServiceConfiguration.ExpirationBulkRemoveDelay;
            _removeThreshhold = ServiceConfiguration.ExpirationBulkRemoveSize;

            //notification is sent for a max of 100k data if multiple items...
            //otherwise if a single item is greater than 100k then notification is sent for
            //that item only...
            int notifThreshold = 30 * 1024;

            CacheBase cacheInst = _context.CacheImpl;
            CacheBase cache     = _context.CacheInternal;
            Cache     rootCache = _context.CacheRoot;

            object[] keys   = null;
            object[] values = null;

            if (cache == null)
            {
                throw new InvalidOperationException("No cache instance defined");
            }


            if (_context.ReaderMgr != null)
            {
                _context.ReaderMgr.ExpireReader(CleanInterval);
            }


            bool allowExpire = AllowClusteredExpiry;

            //in case of replication and por, only the coordinator/sub-coordinator is responsible to expire the items.
            if (!allowExpire)
            {
                return(false);
            }
            ClusteredArrayList selectedKeys = new ClusteredArrayList();
            ClusteredArrayList dependencyChangedSelectedKeys = new ClusteredArrayList();
            int        oldItemsCount = 0;
            HashVector oldeItems     = null;

            try
            {
                StartLogging();

                DateTime startTime   = DateTime.Now;
                int      currentTime = AppUtil.DiffSeconds(startTime);


                if (_context.IsDbSyncCoordinator)
                {
                    _cdbSyncMgr.AcquireSyncData();
                    // get the modified keys. and bulk remove them all. this returns
                    // keys from both sql and oledb providers.
                    IDictionary dbkeys = _cdbSyncMgr.GetExpiredKeys();
                    if (dbkeys.Count > 0)
                    {
                        ClusteredArrayList expire = dbkeys["expire-items"] as ClusteredArrayList;
                        ClusteredArrayList resync = dbkeys["resync-items"] as ClusteredArrayList;

                        if (_context.CacheImpl == null)
                        {
                            return(false);
                        }

                        if (expire.Count > 0)
                        {
                            OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                            operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);
                            _topLevelCache.CascadedRemove(expire, ItemRemoveReason.DependencyChanged, true, operationContext);
                            _cdbSyncMgr.FlushSyncData();

                            //set the flag that item has expired from cache.
                            expired = true;
                        }

                        if (resync.Count > 0 && _context.DsMgr != null)
                        {
                            IEnumerator e = resync.GetEnumerator();
                            while (e.MoveNext())
                            {
                                CacheEntry oldEntry = cache.Get(e.Current, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation));
                                if (oldEntry != null)
                                {
                                    _context.DsMgr.ResyncCacheItemAsync(e.Current, oldEntry.ExpirationHint, null, oldEntry.GroupInfo, oldEntry.QueryInfo, oldEntry.ResyncProviderName);
                                }
                                else
                                {
                                    _context.DsMgr.ResyncCacheItemAsync(e.Current, oldEntry.ExpirationHint, null, null, null, oldEntry.ResyncProviderName);
                                }
                            }
                        }
                    }
                }

                int cleanSize = (int)Math.Ceiling(cache.Count * _cleanRatio);

                //set the flag that we are going to expire the items.

                if (_cacheLastAccessLoggingIntervalPassed >= _cacheLastAccessLoggingInterval)
                {
                    _cacheLastAccessLoggingInterval     = CacheLastAccessLoggingInterval;
                    _cacheLastAccessCountEnabled        = IsCacheLastAccessCountEnabled;
                    _cacheLastAccessCountLoggingEnabled = IsCacheLastAccessLoggingEnabled;
                    _cacheLastAccessInterval            = CacheLastAccessCountInterval;
                }
                else
                {
                    _cacheLastAccessLoggingIntervalPassed++;
                }


                if (_cacheLastAccessCountEnabled && _cacheLastAccessCountLoggingEnabled)
                {
                    if (_cacheLastAccessLoggingIntervalPassed >= _cacheLastAccessLoggingInterval)
                    {
                        _cacheLastAccessLoggingIntervalPassed = 0;
                        oldeItems = new HashVector();
                    }
                }


                lock (_mainIndex.SyncRoot)
                {
                    IDictionaryEnumerator em = _mainIndex.GetEnumerator(); //added by muds

                    if (em != null)
                    {
                        while (em.MoveNext())
                        {
                            ExpirationHint hint = em.Value as ExpirationHint;
                            if (hint != null && _cacheLastAccessCountEnabled && hint is IdleExpiration)
                            {
                                IdleExpiration slidingExpHint = hint as IdleExpiration;
                                TimeSpan       diff           = AppUtil.GetDateTime(AppUtil.DiffSeconds(DateTime.Now)) - AppUtil.GetDateTime(slidingExpHint.LastAccessTime);
                                if (diff.TotalMinutes >= _cacheLastAccessInterval)
                                {
                                    oldItemsCount++;
                                    if (oldeItems != null)
                                    {
                                        oldeItems.Add(em.Key, null);
                                    }
                                }
                            }
                            if (hint == null || hint.SortKey.CompareTo(currentTime) >= 0)
                            {
                                continue;
                            }

                            if (!allowExpire && hint.IsRoutable)
                            {
                                continue;
                            }

                            if (hint.DetermineExpiration(_context))
                            {
                                if (hint.NeedsReSync && _context.DsMgr != null)
                                {
                                    //get old entry to know existing groupinfo and queryinfo for tag purposes.
                                    CacheEntry oldEntry = cache.Get(em.Key, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation));

                                    if (oldEntry != null)
                                    {
                                        _context.DsMgr.ResyncCacheItemAsync(em.Key, hint, null, oldEntry.GroupInfo, oldEntry.QueryInfo, oldEntry.ResyncProviderName);
                                    }
                                    else
                                    {
                                        _context.DsMgr.ResyncCacheItemAsync(em.Key, hint, null, null, null, oldEntry.ResyncProviderName);
                                    }
                                }
                                else
                                {
                                    if (hint.GetExpiringHint() is FixedExpiration || hint.GetExpiringHint() is IdleExpiration)
                                    {
                                        selectedKeys.Add(em.Key);
                                    }
                                    else
                                    {
                                        dependencyChangedSelectedKeys.Add(em.Key);
                                    }
                                }
                                if (cleanSize > 0 && selectedKeys.Count == cleanSize)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }


                if (NCacheLog.IsInfoEnabled)
                {
                    NCacheLog.Info("ExpirationManager.Expire()", String.Format("Expiry time for {0}/{1} Items: " + (DateTime.UtcNow - startTime), selectedKeys.Count, /*_expiryIndex.KeyCount*/ cache.Count));
                }
            }
            catch (Exception e)
            {
                NCacheLog.Error("ExpirationManager.Expire(bool)", "LocalCache(Expire): " + e.ToString());
            }
            finally
            {
                _context.PerfStatsColl.IncrementCacheLastAccessCountStats(oldItemsCount);


                ApplyLoggs();
                ClusteredArrayList dependentItems = new ClusteredArrayList();
                ClusteredArrayList removedItems   = new ClusteredArrayList();
                DateTime           startTime      = DateTime.Now;

                HashVector expiredItemTable = new HashVector();

                expiredItemTable.Add(ItemRemoveReason.Expired, selectedKeys);                            //Time based expiration
                expiredItemTable.Add(ItemRemoveReason.DependencyChanged, dependencyChangedSelectedKeys); //FileDependency or any other

                try
                {
                    IDictionaryEnumerator ide = expiredItemTable.GetEnumerator();

                    while (ide.MoveNext())
                    {
                        selectedKeys = ide.Value as ClusteredArrayList;
                        ItemRemoveReason removedReason = (ItemRemoveReason)ide.Key;

                        if (selectedKeys.Count > 0)
                        {
                            //new architectural changes begins from here.

                            ClusteredArrayList keysTobeRemoved = new ClusteredArrayList();

                            for (int i = 0; i < selectedKeys.Count && !_cacheCleared; i++)
                            {
                                keysTobeRemoved.Add(selectedKeys[i]);
                                if (keysTobeRemoved.Count % _removeThreshhold == 0)
                                {
                                    try
                                    {
                                        if (this.IsDisposed)
                                        {
                                            break;
                                        }

                                        OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                                        operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);
                                        object[][] keysExposed = keysTobeRemoved.ToInternalArray();
                                        foreach (object[] keyCollection in keysExposed)
                                        {
                                            ArrayList removed = cache.RemoveSync(keyCollection, removedReason, false, operationContext) as ArrayList;
                                            if (removed != null)
                                            {
                                                removedItems.AddRange(removed);
                                            }
                                        }

                                        //set the flag that item has expired from cache...
                                        expired = true;


                                        if (_context.PerfStatsColl != null)
                                        {
                                            _context.PerfStatsColl.IncrementExpiryPerSecStatsBy(keysTobeRemoved.Count);
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        NCacheLog.Error("ExpiryManager.Expire", "an error occurred while removing expired items. Error " + e.ToString());
                                    }
                                    keysTobeRemoved.Clear();
                                    if (removedItems != null && removedItems.Count > 0)
                                    {
                                        dependentItems.AddRange(removedItems);
                                        removedItems.Clear();
                                    }
                                    //we stop the activity of the current thread so that normal user operation is not affected.
                                    Thread.Sleep(_sleepInterval * 1000);
                                }
                            }

                            if (!this.IsDisposed && keysTobeRemoved.Count > 0)
                            {
                                try
                                {
                                    OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                                    operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);

                                    object[][] keysExposed = keysTobeRemoved.ToInternalArray();
                                    foreach (object[] keyCollection in keysExposed)
                                    {
                                        ArrayList removed = cache.RemoveSync(keyCollection, removedReason, false, operationContext) as ArrayList;
                                        if (removed != null)
                                        {
                                            removedItems.AddRange(removed);
                                        }
                                    }
                                    //set the flag that item has expired from cache...
                                    expired = true;


                                    if (_context.PerfStatsColl != null)
                                    {
                                        _context.PerfStatsColl.IncrementExpiryPerSecStatsBy(keysTobeRemoved.Count);
                                    }

                                    if (removedItems != null && removedItems.Count > 0)
                                    {
                                        dependentItems.AddRange(removedItems);
                                        removedItems.Clear();
                                    }
                                }
                                catch (Exception e)
                                {
                                    NCacheLog.Error("ExpiryManager.Expire", "an error occurred while removing expired items. Error " + e.ToString());
                                }
                            }
                        }
                    }

                    if (!this.IsDisposed && dependentItems.Count > 0)
                    {
                        ClusteredArrayList removableList = new ClusteredArrayList();
                        if (rootCache != null)
                        {
                            foreach (object depenentItme in dependentItems)
                            {
                                if (depenentItme == null)
                                {
                                    continue;
                                }
                                removableList.Add(depenentItme);
                                if (removableList.Count % 100 == 0)
                                {
                                    try
                                    {
                                        if (this.IsDisposed)
                                        {
                                            break;
                                        }
                                        OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                                        operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);

                                        rootCache.CascadedRemove(removableList, ItemRemoveReason.DependencyChanged, true, operationContext);


                                        if (_context.PerfStatsColl != null)
                                        {
                                            _context.PerfStatsColl.IncrementExpiryPerSecStatsBy(removableList.Count);
                                        }
                                    }
                                    catch (Exception exc)
                                    {
                                        NCacheLog.Error("ExpiryManager.Expire", "an error occurred while removing dependent items. Error " + exc.ToString());
                                    }
                                    removableList.Clear();
                                }
                            }
                            if (!this.IsDisposed && removableList.Count > 0)
                            {
                                try
                                {
                                    OperationContext operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);
                                    operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);

                                    rootCache.CascadedRemove(removableList, ItemRemoveReason.DependencyChanged, true, operationContext);


                                    if (_context.PerfStatsColl != null)
                                    {
                                        _context.PerfStatsColl.IncrementExpiryPerSecStatsBy(removableList.Count);
                                    }
                                }
                                catch (Exception exc)
                                {
                                    NCacheLog.Error("ExpiryManager.Expire", "an error occurred while removing dependent items. Error " + exc.ToString());
                                }
                                removableList.Clear();
                            }
                        }
                    }
                }
                finally
                {
                    _transitoryIndex.Clear();

                    lock (this)
                    {
                        _cacheCleared = false;
                    }

                    if (oldeItems != null)
                    {
                        StringBuilder         sb  = new StringBuilder();
                        IDictionaryEnumerator ide = oldeItems.GetEnumerator();
                        int count = 1;
                        while (ide.MoveNext())
                        {
                            sb.Append(ide.Key + ", ");

                            if (count % 10 == 0)
                            {
                                sb.Append("\r\n");
                                count = 1;
                            }
                            else
                            {
                                count++;
                            }
                        }

                        NCacheLog.Info(sb.ToString().Trim());
                    }
                }
            }
            return(expired);
        }
Ejemplo n.º 12
0
 public static ExpirationHint ReadExpHint(CompactReader reader)
 {
     ExpirationHintType expHint = ExpirationHintType.Parent;
     expHint = (ExpirationHintType)reader.ReadInt16();
     ExpirationHint tmpObj = null;
     switch (expHint)
     {
         case ExpirationHintType.NULL:
             return null;
         
         case ExpirationHintType.Parent:
             tmpObj = (ExpirationHint)reader.ReadObject();
             return (ExpirationHint)tmpObj;                    
         
         case ExpirationHintType.FixedExpiration:
             FixedExpiration fe = new FixedExpiration();
             ((ICompactSerializable)fe).Deserialize(reader);
             return (ExpirationHint)fe;                    
         
         case ExpirationHintType.IdleExpiration:
             IdleExpiration ie = new IdleExpiration();
             ((ICompactSerializable)ie).Deserialize(reader);
             return (ExpirationHint)ie;
         
         default:
             break;            
     }
     return null;
 }