Example #1
0
    /// <summary>
    /// 获取
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public static T Get <T>(string key)
    {
        T             t     = default(T);
        CacheBase <T> cache = GetType <T>(key);

        if (cache != null)
        {
            t = cache.Get();
        }
        return(t);
    }
        public async Task <IEnumerable <string> > Get()
        {
            var result = await _cache.Get("key", Task.FromResult("obj1"));

            return(new string[] { result });
        }
Example #3
0
 /// <summary>
 /// Does the lazy loading of object. This method is virtual so containers can customize object
 /// fetching logic.
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 protected virtual object FetchObject(object key, OperationContext operationContext)
 {
     operationContext.Add(OperationContextFieldName.GenerateQueryInfo, true);
     return(_cache.Get(key, operationContext));
 }
        /// <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);
        }
Example #5
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);
        }
Example #6
0
 public override Present Realize(string refCredit)
 {
     return(cache.Get(future, refCredit));
 }