Exemplo n.º 1
0
        /// <summary>
        /// Valida os dados que devem expirar.
        /// </summary>
        /// <returns></returns>
        public bool Expire()
        {
            bool flag = false;

            lock (this)
                _runCount++;
            _sleepInterval    = Convert.ToInt32(ServiceConfiguration.ExpirationBulkRemoveDelay);
            _removeThreshhold = Convert.ToInt32(ServiceConfiguration.ExpirationBulkRemoveSize);
            CacheBase cacheImpl     = _context.CacheImpl;
            CacheBase cacheInternal = _context.CacheInternal;
            Cache     cacheRoot     = _context.CacheRoot;

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

            new ArrayList();
            int       num       = 0;
            Hashtable hashtable = null;

            try
            {
                this.StartLogging();
                DateTime now         = DateTime.Now;
                int      diffSeconds = CachingUtils.DiffSeconds(now);
                int      num3        = (int)Math.Ceiling((double)(cacheInternal.Count * _cleanRatio));
                if (_cacheLastAccessLoggingIntervalPassed >= _cacheLastAccessLoggingInterval)
                {
                    _cacheLastAccessLoggingInterval     = this.CacheLastAccessLoggingInterval;
                    _cacheLastAccessCountEnabled        = this.IsCacheLastAccessCountEnabled;
                    _cacheLastAccessCountLoggingEnabled = this.IsCacheLastAccessLoggingEnabled;
                    _cacheLastAccessInterval            = this.CacheLastAccessCountInterval;
                }
                else
                {
                    _cacheLastAccessLoggingIntervalPassed++;
                }
                if ((_cacheLastAccessCountEnabled && _cacheLastAccessCountLoggingEnabled) && (_cacheLastAccessLoggingIntervalPassed >= _cacheLastAccessLoggingInterval))
                {
                    _cacheLastAccessLoggingIntervalPassed = 0;
                    hashtable = new Hashtable();
                }
                lock (_mainIndex.SyncRoot)
                {
                    IDictionaryEnumerator enumerator2 = _mainIndex.GetEnumerator();
                    if (enumerator2 != null)
                    {
                        while (enumerator2.MoveNext())
                        {
                            var entry2 = enumerator2.Value as ExpiryIndexEntry;
                            var exh    = entry2.Hint;
                            if (((exh != null) && _cacheLastAccessCountEnabled) && (exh is IdleExpiration))
                            {
                                IdleExpiration expiration = exh as IdleExpiration;
                                TimeSpan       span       = (TimeSpan)(CachingUtils.GetDateTime(CachingUtils.DiffSeconds(DateTime.Now)) - CachingUtils.GetDateTime(expiration.LastAccessTime));
                                if (span.TotalMinutes >= _cacheLastAccessInterval)
                                {
                                    num++;
                                    if (hashtable != null)
                                    {
                                        hashtable.Add(enumerator2.Key, null);
                                    }
                                }
                            }
                            if ((((exh != null) && (exh.SortKey.CompareTo(diffSeconds) < 0)) && !exh.IsRoutable) && exh.DetermineExpiration(_context))
                            {
                                if (exh.NeedsReSync && (_context.DatasourceMgr != null))
                                {
                                    CacheEntry entry3 = cacheInternal.Get(enumerator2.Key, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation));
                                    if (entry3 != null)
                                    {
                                        _context.DatasourceMgr.ResyncCacheItemAsync(enumerator2.Key, exh, null, entry3.GroupInfo, entry3.QueryInfo, entry3.ResyncProviderName);
                                    }
                                    else
                                    {
                                        _context.DatasourceMgr.ResyncCacheItemAsync(enumerator2.Key, exh, null, null, null, entry3.ResyncProviderName);
                                    }
                                }
                                else
                                {
                                    list.Add(enumerator2.Key);
                                }
                                if ((num3 > 0) && (list.Count == num3))
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.Logger.Error(("LocalCache(Expire): " + exception.ToString()).GetFormatter());
            }
            finally
            {
                this.ApplyLoggs();
                ArrayList list4 = new ArrayList();
                ArrayList c     = null;
                DateTime  time1 = DateTime.Now;
                try
                {
                    if (list.Count > 0)
                    {
                        var list6 = new ArrayList();
                        for (int i = 0; (i < list.Count) && !_cacheCleared; i++)
                        {
                            list6.Add(list[i]);
                            if ((list6.Count % _removeThreshhold) == 0)
                            {
                                try
                                {
                                    if (this.IsDisposed)
                                    {
                                        break;
                                    }
                                    c    = cacheInternal.RemoveSync(list6.ToArray(), ItemRemoveReason.Expired, false, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)) as ArrayList;
                                    flag = true;
                                }
                                catch (Exception exception2)
                                {
                                    this.Logger.Error(("an error occured while removing expired items. Error " + exception2.ToString()).GetFormatter());
                                }
                                list6.Clear();
                                if ((c != null) && (c.Count > 0))
                                {
                                    list4.AddRange(c);
                                }
                                System.Threading.Thread.Sleep((int)(_sleepInterval * 1000));
                            }
                        }
                        if (!this.IsDisposed && (list6.Count > 0))
                        {
                            try
                            {
                                c    = cacheInternal.RemoveSync(list6.ToArray(), ItemRemoveReason.Expired, false, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)) as ArrayList;
                                flag = true;
                                if ((c != null) && (c.Count > 0))
                                {
                                    list4.AddRange(c);
                                }
                            }
                            catch (Exception exception3)
                            {
                                this.Logger.Error(("an error occured while removing expired items. Error " + exception3.ToString()).GetFormatter());
                            }
                        }
                    }
                    if (!this.IsDisposed && (list4.Count > 0))
                    {
                        ArrayList list7 = new ArrayList();
                        if (cacheRoot != null)
                        {
                            foreach (object obj2 in list4)
                            {
                                if (obj2 != null)
                                {
                                    list7.Add(obj2);
                                    if ((list7.Count % 100) == 0)
                                    {
                                        try
                                        {
                                            if (this.IsDisposed)
                                            {
                                                break;
                                            }
                                            cacheRoot.CascadedRemove(list7.ToArray(), ItemRemoveReason.Expired, true, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation));
                                        }
                                        catch (Exception exception4)
                                        {
                                            this.Logger.Error(("an error occured while removing dependent items. Error " + exception4.ToString()).GetFormatter());
                                        }
                                        list7.Clear();
                                    }
                                }
                            }
                            if (!this.IsDisposed && (list7.Count > 0))
                            {
                                try
                                {
                                    cacheRoot.CascadedRemove(list7.ToArray(), ItemRemoveReason.Expired, true, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation));
                                }
                                catch (Exception exception5)
                                {
                                    this.Logger.Error(("an error occured while removing dependent items. Error " + exception5.ToString()).GetFormatter());
                                }
                                list7.Clear();
                            }
                        }
                    }
                }
                finally
                {
                    _transitoryIndex.Clear();
                    lock (this)
                    {
                        _cacheCleared = false;
                    }
                    if (hashtable != null)
                    {
                        StringBuilder         builder     = new StringBuilder();
                        IDictionaryEnumerator enumerator3 = hashtable.GetEnumerator();
                        int num5 = 1;
                        while (enumerator3.MoveNext())
                        {
                            builder.Append(enumerator3.Key + ", ");
                            if ((num5 % 10) == 0)
                            {
                                builder.Append("\r\n");
                                num5 = 1;
                            }
                            else
                            {
                                num5++;
                            }
                        }
                        this.Logger.Info(builder.ToString().Trim().GetFormatter());
                    }
                }
            }
            return(flag);
        }
Exemplo n.º 2
0
        public static ExpirationHint ReadExpHint(CompactReader reader)
        {
            ExpirationHintType parent = ExpirationHintType.Parent;

            parent = (ExpirationHintType)reader.ReadInt16();
            switch (parent)
            {
            case ExpirationHintType.NULL:
                return(null);

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

            case ExpirationHintType.FixedExpiration:
            {
                FixedExpiration expiration = new FixedExpiration();
                expiration.Deserialize(reader);
                return(expiration);
            }

            case ExpirationHintType.FixedIdleExpiration:
            {
                FixedIdleExpiration expiration4 = new FixedIdleExpiration();
                expiration4.Deserialize(reader);
                return(expiration4);
            }

            case ExpirationHintType.FileDependency:
            {
                FileDependency dependency = new FileDependency();
                dependency.Deserialize(reader);
                return(dependency);
            }

            case ExpirationHintType.KeyDependency:
            {
                KeyDependency dependency2 = new KeyDependency();
                dependency2.Deserialize(reader);
                return(dependency2);
            }

            case ExpirationHintType.IdleExpiration:
            {
                IdleExpiration expiration5 = new IdleExpiration();
                expiration5.Deserialize(reader);
                return(expiration5);
            }

            case ExpirationHintType.AggregateExpirationHint:
            {
                AggregateExpirationHint hint2 = new AggregateExpirationHint();
                hint2.Deserialize(reader);
                return(hint2);
            }

            case ExpirationHintType.ExtensibleDependency:
            {
                ExtensibleDependency dependency8 = new ExtensibleDependency();
                return((ExtensibleDependency)reader.ReadObject());
            }
            }
            return(null);
        }