Ejemplo n.º 1
0
        public void ExpireCallback(IAsyncResult ar)
        {
            ExpireDelegate dlgt = (ExpireDelegate)ar.AsyncState;

            dlgt.EndInvoke(ar);
            return;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sends two messages, where the second one is always green and occurs longevity seconds after the first.
 /// Use same as function sendStatusMessage, but add parameter
 /// longevity, which is the number of seconds to wait before sending a green status,
 /// assuming no other status messages have been sent to this remoteTest in the meantime
 /// and assuming that you don't call this function with an initial green status.
 /// The second message uses the expiredMsg parameter as it's info.
 /// THIS IS NOT THREADSAFE!
 /// </summary>
 /// <param name="remoteTest"></param>
 /// <param name="status"></param>
 /// <param name="info"></param>
 /// <param name="expiredMsg"></param>
 /// <param name="longevity"></param>
 public void sendExpiringStatusMessage(string remoteTest, Status status, string info, string expiredMsg, int longevity)
 {
     sendStatusMessage(remoteTest, status, info, true);
     if (status != Status.GREEN)
     {
         lock (_lastMessages)
         {
             _lastMessages[remoteTest] = DateTime.Now;
         }
         ExpireDelegate dlgt = new ExpireDelegate(this.Expire);
         dlgt.BeginInvoke(longevity, remoteTest, expiredMsg, new AsyncCallback(this.ExpireCallback), dlgt);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Expire items as appropriate.
        /// </summary>
        /// <remarks>
        /// Callers must lock m_Index.
        /// </remarks>
        /// <param name='getting'></param>
        protected virtual void Expire(bool getting)
        {
            if (getting && (m_Strategy == CacheStrategy.Aggressive))
            {
                return;
            }

            DateTime now = DateTime.UtcNow;

            if (now < m_nextExpire)
            {
                return;
            }

            m_nextExpire = now + m_expiresTime;

            if (m_DefaultTTL.Ticks != 0)
            {
                foreach (CacheItemBase item in new List <CacheItemBase>(m_Index))
                {
                    if (item.expires.Ticks == 0 ||
                        item.expires <= now)
                    {
                        m_Index.Remove(item);
                        m_Lookup.Remove(item.uuid);
                    }
                }
            }

            switch (m_Strategy)
            {
            case CacheStrategy.Aggressive:
                int target = (int)((float)Size * 0.9);
                if (Count < target)     // Cover ridiculous cache sizes
                {
                    return;
                }

                target = (int)((float)Size * 0.8);

                m_Index.Sort(new SortLRUrev());

                ExpireDelegate doExpire = OnExpire;

                if (doExpire != null)
                {
                    List <CacheItemBase> candidates =
                        m_Index.GetRange(target, Count - target);

                    foreach (CacheItemBase i in candidates)
                    {
                        if (doExpire(i.uuid))
                        {
                            m_Index.Remove(i);
                            m_Lookup.Remove(i.uuid);
                        }
                    }
                }
                else
                {
                    m_Index.RemoveRange(target, Count - target);

                    m_Lookup.Clear();

                    foreach (CacheItemBase item in m_Index)
                    {
                        m_Lookup[item.uuid] = item;
                    }
                }

                break;

            default:
                break;
            }
        }