Ejemplo n.º 1
0
 public object Get(object key)
 {
     try
     {
         if (_cachedic.ContainsKey(key))
         {
             TimeoutCacheObject tco = (TimeoutCacheObject)_cachedic[key];
             if (tco != null)
             {
                 TimeSpan ts = DateTime.Now - tco.Timestamp;
                 if (ts.TotalSeconds > _totalSeconds)
                 {
                     object movobj = null;
                     _cachedic.TryRemove(key, out movobj);
                     return(tco.Obj);
                 }
                 else
                 {
                     return(tco.Obj);
                 }
             }
             else
             {
                 return(null);
             }
         }
     }
     catch (Exception ex)
     {
         SMSLog.Debug("TimeoutCache:Get=>读取存数据异常:" + ex.Message);
     }
     return(null);
 }
Ejemplo n.º 2
0
 public void Add(object key, object obj)
 {
     try
     {
         //lock (_cachedic)
         // {
         if (!_cachedic.ContainsKey(key))
         {
             TimeoutCacheObject tco = new TimeoutCacheObject(obj, DateTime.Now);
             _cachedic.TryAdd(key, tco);
         }
         // }
     }
     catch (Exception ex)
     {
         SMSLog.Error("TimeoutCache:Add=>添加缓存数据异常:" + ex.Message);
     }
 }
Ejemplo n.º 3
0
        private void TCallback2(object obj)
        {
            SMSLog.Debug("TimeoutCache=====TCallback2======>开始清除缓存数据:" + DateTime.Now);

            try
            {
                int      c    = _cachedic.Keys.Count;
                object[] keys = new object[c];
                object[] vals = new object[c];
                _cachedic.Keys.CopyTo(keys, 0);
                _cachedic.Values.CopyTo(vals, 0);
                for (int i = 0; i < vals.Length; i++)
                {
                    try
                    {
                        TimeoutCacheObject tco = vals[i] as TimeoutCacheObject;
                        if (tco != null)
                        {
                            TimeSpan ts = DateTime.Now - tco.Timestamp;
                            if (ts.TotalSeconds > _totalSeconds)
                            {
                                if (_cachedic.ContainsKey(keys[i]))
                                {
                                    object movobj = null;
                                    _cachedic.TryRemove(keys[i], out movobj);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        SMSLog.Error("TimeoutCache=TCallback2=>删除缓存数据异常:" + ex.Message);
                    }
                }
            }
            catch (Exception e)
            {
                SMSLog.Error("TimeoutCache=TCallback2=>删除缓存数据异常:" + e.Message);
            }

            SMSLog.Debug("TimeoutCache=====TCallback2======>结束清除缓存数据:" + DateTime.Now);
        }
Ejemplo n.º 4
0
 private void TCallback(object obj)
 {
     try
     {
         SMSLog.Debug("TimeoutCache===========>开始清除缓存数据:" + DateTime.Now);
         IDictionary <object, object> tcachedic = new Dictionary <object, object>();
         tcachedic = _cachedic;
         if (tcachedic != null)
         {
             foreach (object key in tcachedic.Keys)
             {
                 try
                 {
                     TimeoutCacheObject tco = (TimeoutCacheObject)tcachedic[key];
                     TimeSpan           ts  = DateTime.Now - tco.Timestamp;
                     if (ts.TotalSeconds > _totalSeconds)
                     {
                         if (_cachedic.ContainsKey(key))
                         {
                             object movobj = null;
                             _cachedic.TryRemove(key, out movobj);
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     SMSLog.Error("TimeoutCache==>删除缓存数据异常:" + ex.Message);
                 }
             }
             tcachedic.Clear();
         }
         SMSLog.Debug("TimeoutCache===========>结束清除缓存数据:" + DateTime.Now);
     }
     catch (Exception ex)
     {
         SMSLog.Error("TimeoutCache==>清除缓存数据异常:" + ex.Message);
     }
 }