Пример #1
0
 /// <summary>
 /// 监测缓存时间
 /// </summary>
 private void Take()
 {
     if (isRun)
     {
         return;
     }
     isRun = true;
     Task.Factory.StartNew(() =>
     {
         Thread.Sleep(2000);//延迟2秒
         Thread.Sleep(minCacheTime / 2);
         int num = queue.Count;
         if (num > 0)
         {
             SigleEntity <TKey, TValue> entity = null;
             long writeTime = 0;
             long curTime   = DateTime.Now.Ticks;
             do
             {
                 if (queue.TryDequeue(out entity))
                 {
                     if (!pairs.TryGetValue(entity.Key, out writeTime))
                     {
                         writeTime = entity.WriteTime;
                     }
                     //
                     if ((curTime - writeTime) / ticksMS > entity.CacheTime)
                     {
                         //移除
                         cache.Remove(entity.Key);
                         pairs.TryRemove(entity.Key, out writeTime);
                         entity.Cache.Remove(entity.Key);
                     }
                     else
                     {
                         if (nextCacheTime > entity.CacheTime)
                         {
                             nextCacheTime = entity.CacheTime;
                         }
                     }
                 }
                 num--;
             } while (num > 0);
             //
             minCacheTime  = nextCacheTime;
             nextCacheTime = int.MaxValue;
             isRun         = false;
         }
     });
 }
Пример #2
0
 /// <summary>
 /// 添加元素
 /// </summary>
 /// <param name="entity"></param>
 public void Add(SigleEntity <TKey, TValue> entity)
 {
     queue.Enqueue(entity);
     if (entity.CacheTime > 0)
     {
         pairs[entity.Key] = entity.WriteTime;
         if (minCacheTime > entity.CacheTime)
         {
             minCacheTime = entity.CacheTime;
         }
     }
     else if (entity.CacheTime == 0)
     {
         persistence[entity.Key] = null;
     }
 }
Пример #3
0
        public void Add(TKey key, TValue value, int slfTime = -1)
        {
            if (lazyObject.IsValueCreated)
            {
                lazyObject.Value.Refresh(key);
            }
            if (slfTime > -1)
            {
                SigleEntity <TKey, TValue> sigle = new SigleEntity <TKey, TValue>()
                {
                    Cache = this, CacheTime = slfTime * 1000, Key = key, WriteTime = DateTime.Now.Ticks
                };
                lazyObject.Value.Add(sigle);
            }
            switch (kvState)
            {
            case 1:
            {
                CacheEntity <WeakReference <object> > entityKV = null;
                object outValue = (object)value;
                if (dicWeakKV.TryGetValue(key, out entityKV))
                {
                    //更新
                    entityKV.element.WriteTime = DateTime.Now.Ticks;
                    entityKV.element.AcessTime = DateTime.Now.Ticks;
                    entityKV.element.Value     = new WeakReference <object>(outValue);
                }
                else
                {
                    object outKey = (object)key;
                    entityKV                   = new CacheEntity <WeakReference <object> >();
                    entityKV.element           = new CacheElement <WeakReference <object> >();
                    entityKV.element.AcessTime = DateTime.Now.Ticks;
                    entityKV.element.Hit       = 0;
                    entityKV.element.Value     = new WeakReference <object>(outValue);
                    entityKV.element.WriteTime = DateTime.Now.Ticks;
                    dicWeakKV.Add(outKey, entityKV);
                    if (config.UseMemory && (config.Policy == CachePolicy.FIFO || config.Policy == CachePolicy.LRU))
                    {
                        stack.Push(key);        //记录操作
                    }
                    else
                    {
                        dicKeys[key] = null;        //必须记录KEY
                    }
                    //

                    Interlocked.Increment(ref cacheSize);
                }
            }
            break;

            case 2:
            {
                CacheEntity <TValue> entityK;
                if (dicWeakKey.TryGetValue(key, out entityK))
                {
                    entityK.element.WriteTime = DateTime.Now.Ticks;
                    entityK.element.AcessTime = DateTime.Now.Ticks;
                    entityK.element.Value     = value;
                }
                else
                {
                    object outK = (object)key;
                    entityK                   = new CacheEntity <TValue>();
                    entityK.element           = new CacheElement <TValue>();
                    entityK.element.AcessTime = DateTime.Now.Ticks;
                    entityK.element.Hit       = 0;
                    entityK.element.WriteTime = DateTime.Now.Ticks;
                    entityK.element.Value     = value;
                    dicWeakKey.Add(outK, entityK);
                    if (config.UseMemory && (config.Policy == CachePolicy.FIFO || CachePolicy.LRU == config.Policy))
                    {
                        stack.Push(key);
                    }
                    else
                    {
                        dicKeys[key] = null;
                    }
                    Interlocked.Increment(ref cacheSize);
                }
            }
            break;

            case 3:
            {
                CacheEntity <WeakReference <object> > entityV = null;
                object outV = (object)value;
                if (dicWeakValue.TryGetValue(key, out entityV))
                {
                    entityV.element.WriteTime = DateTime.Now.Ticks;
                    entityV.element.AcessTime = DateTime.Now.Ticks;
                    entityV.element.Value     = new WeakReference <object>(outV);
                }
                else
                {
                    entityV                   = new CacheEntity <WeakReference <object> >();
                    entityV.element           = new CacheElement <WeakReference <object> >();
                    entityV.element.AcessTime = DateTime.Now.Ticks;
                    entityV.element.Hit       = 0;
                    entityV.element.Value     = new WeakReference <object>(outV);
                    entityV.element.WriteTime = DateTime.Now.Ticks;
                    dicWeakValue[key]         = entityV;
                    if (config.UseMemory)
                    {
                        stack.Push(key);
                    }
                    Interlocked.Increment(ref cacheSize);
                }
            }
            break;

            case 4:
            {
                CacheEntity <TValue> entity = null;
                if (dicCache.TryGetValue(key, out entity))
                {
                    entity.element.WriteTime = DateTime.Now.Ticks;
                    entity.element.AcessTime = DateTime.Now.Ticks;
                    entity.element.Value     = value;
                }
                else
                {
                    entity                   = new CacheEntity <TValue>();
                    entity.element           = new CacheElement <TValue>();
                    entity.element.AcessTime = DateTime.Now.Ticks;
                    entity.element.WriteTime = DateTime.Now.Ticks;
                    entity.element.Hit       = 0;
                    entity.element.Value     = value;
                    dicCache[key]            = entity;
                    if (config.UseMemory)
                    {
                        stack.Push(key);        //记录操作
                    }
                    Interlocked.Increment(ref cacheSize);
                }
            }
            break;
            }
            CheckPolicy();

            //准备持久化
            if (dicPersAdd != null)
            {
                dicPersAdd[key] = value;
            }
        }