示例#1
0
 public int Compare(CacheEntity <T> element, CacheEntity <T> cache)
 {
     if (cache == null)
     {
         return(1);
     }
     if (element == null)
     {
         return(-1);
     }
     return(element.element.AcessTime.CompareTo(cache.element.AcessTime));
 }
示例#2
0
        /// <summary>
        /// 监测超时
        /// </summary>
        /// <param name="keys"></param>
        private void CheckCacheTime(LinkedList <TKey> keys)
        {
            if (4 != kvState)
            {
                return;
            }
            //
            CacheEntity <TValue> entity = null;
            long curTime = DateTime.Now.Ticks;

            lastWriteTime = long.MaxValue;
            int  leftTime = config.CacheTime * 1000;
            long timeLen  = 0;

            Parallel.ForEach(keys, (key) =>
            {
                if (!IsPersistent(key))
                {
                    //非持久元素
                    if (dicCache.TryGetValue(key, out entity))
                    {
                        timeLen = curTime - entity.element.WriteTime;
                        if (timeLen / ticksMS >= leftTime)
                        {
                            //
                            RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                            {
                                Key = key, State = 1
                            };
                            element.Value = entity.element.Value;
                            removeEntities.Enqueue(element);
                        }
                        else
                        {
                            if (lastWriteTime > entity.element.WriteTime)
                            {
                                //记录最早的写入
                                lastWriteTime = entity.element.WriteTime;
                            }
                        }
                    }
                }
            });
        }
示例#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;
            }
        }
示例#4
0
        /// <summary>
        /// 监测弱引用,包括超时到期
        /// </summary>
        /// <param name="keys"></param>
        private void WeakRef(LinkedList <TKey> keys)
        {
            if (4 == kvState)
            {
                return;
            }
            object item    = null;
            long   curTime = DateTime.Now.Ticks;
            TValue value   = default(TValue);
            CacheEntity <WeakReference <object> > kv = null;
            CacheEntity <TValue> entity = null;

            lastWriteTime = long.MaxValue;
            int leftTime = config.CacheTime * 1000;

            Parallel.ForEach(keys, (key) =>
            {
                if (!IsPersistent(key))
                {
                    //不是持久Key才判断
                    switch (kvState)
                    {
                    case 1:
                        {
                            if (dicWeakKV.TryGetValue(key, out kv))
                            {
                                if (!kv.element.Value.TryGetTarget(out item))
                                {
                                    //没有数据了
                                    dicWeakKV.Remove(key);
                                    RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                                    {
                                        Key = key, State = 2
                                    };
                                    removeEntities.Enqueue(element);
                                }
                                else if ((curTime - kv.element.WriteTime) / ticksMS > leftTime)
                                {
                                    //
                                    RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                                    {
                                        Key = key, State = 1
                                    };
                                    if (TryRemove(key, out value))
                                    {
                                        element.Value = value;
                                    }
                                    removeEntities.Enqueue(element);
                                }
                                else
                                {
                                    if (lastWriteTime > kv.element.WriteTime)
                                    {
                                        //记录最早的写入
                                        lastWriteTime = kv.element.WriteTime;
                                    }
                                }
                            }
                        }
                        break;

                    case 3:
                        {
                            if (dicWeakValue.TryGetValue(key, out kv))
                            {
                                if (!kv.element.Value.TryGetTarget(out item))
                                {
                                    //没有数据了
                                    dicWeakValue.TryRemove(key, out kv);
                                    RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                                    {
                                        Key = key, State = 2
                                    };
                                    removeEntities.Enqueue(element);
                                }
                                else if ((curTime - kv.element.WriteTime) / ticksMS > leftTime)
                                {
                                    if (lazyObject.IsValueCreated)
                                    {
                                        //是否是持久缓存元素
                                        if (!lazyObject.Value.IsPersistent(key))
                                        {
                                            RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                                            {
                                                Key = key, State = 1
                                            };
                                            if (TryRemove(key, out value))
                                            {
                                                element.Value = value;
                                            }
                                            removeEntities.Enqueue(element);
                                        }
                                    }
                                    else
                                    {
                                        //
                                        RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                                        {
                                            Key = key, State = 1
                                        };
                                        if (TryRemove(key, out value))
                                        {
                                            element.Value = value;
                                        }
                                        removeEntities.Enqueue(element);
                                    }
                                }
                                if (lastWriteTime > kv.element.WriteTime)
                                {
                                    //记录最早的写入
                                    lastWriteTime = kv.element.WriteTime;
                                }
                            }
                        }
                        break;

                    case 2:
                        {
                            if (dicWeakKey.TryGetValue(key, out entity))
                            {
                                //值检查超时
                                if ((curTime - entity.element.WriteTime) / ticksMS > leftTime)
                                {
                                    //
                                    RemoveEntity <TKey, TValue> element = new RemoveEntity <TKey, TValue>()
                                    {
                                        Key = key, State = 1
                                    };
                                    if (TryRemove(key, out value))
                                    {
                                        element.Value = value;
                                    }
                                    removeEntities.Enqueue(element);
                                }
                                if (lastWriteTime > entity.element.WriteTime)
                                {
                                    //记录最早的写入
                                    lastWriteTime = entity.element.WriteTime;
                                }
                            }
                        }
                        break;
                    }
                }
            });
        }
示例#5
0
        /// <summary>
        /// 移除
        /// </summary>
        private void RemovePolicyNo()
        {
            //获取所有Key

            LinkedList <TKey> link = null;

            if (dicKeys.Count > 0)
            {
                //记录了Key;
                link = new LinkedList <TKey>(dicKeys.Keys);
            }
            else
            {
                if (3 == kvState)
                {
                    link = new LinkedList <TKey>(dicWeakValue.Keys);
                }
                else if (4 == kvState)
                {
                    link = new LinkedList <TKey>(dicCache.Keys);
                }
            }
            WeakRef(link);
            CheckCacheTime(link);
            //
            CacheEntity <WeakReference <object> > entityweak = null;

            CacheEntity <WeakReference <object> >[] cacheWeakEntity = null;
            CacheEntity <TValue> entity = null;

            CacheEntity <TValue>[] cacheEntities = null;
            Dictionary <CacheEntity <WeakReference <object> >, TKey> dicWeak = new Dictionary <CacheEntity <WeakReference <object> >, TKey>();
            Dictionary <CacheEntity <TValue>, TKey> dic = new Dictionary <CacheEntity <TValue>, TKey>();
            int index = 0;

            switch (kvState)
            {
            case 1:
            {
                cacheWeakEntity = new CacheEntity <WeakReference <object> > [link.Count];
                dicWeak         = new Dictionary <CacheEntity <WeakReference <object> >, TKey>();
                foreach (TKey tkey in link)
                {
                    if (dicWeakKV.TryGetValue(tkey, out entityweak))
                    {
                        cacheWeakEntity[index++] = entityweak;
                        dicWeak[entityweak]      = tkey;
                    }
                }
                //
                List <CacheEntity <WeakReference <object> > > list = SortKeys(cacheWeakEntity);
                this.RemovePolicyList(list, dicWeak);
                cacheWeakEntity = null;
                list.Clear();
                dicWeak.Clear();
            }
            break;

            case 3:
            {
                cacheWeakEntity = new CacheEntity <WeakReference <object> > [link.Count];
                dicWeak         = new Dictionary <CacheEntity <WeakReference <object> >, TKey>();
                foreach (TKey tkey in link)
                {
                    if (dicWeakValue.TryGetValue(tkey, out entityweak))
                    {
                        cacheWeakEntity[index++] = entityweak;
                        dicWeak[entityweak]      = tkey;
                    }
                }
                //
                List <CacheEntity <WeakReference <object> > > list = SortKeys(cacheWeakEntity);
                this.RemovePolicyList(list, dicWeak);
                list.Clear();
                dicWeak.Clear();
                cacheWeakEntity = null;
            }
            break;

            case 2:
            {
                cacheEntities = new CacheEntity <TValue> [link.Count];
                foreach (TKey tkey in link)
                {
                    if (dicWeakKey.TryGetValue(tkey, out entity))
                    {
                        cacheEntities[index++] = entity;
                        dic[entity]            = tkey;
                    }
                }
                List <CacheEntity <TValue> > list = SortKeys(cacheEntities);
                this.RemovePolicyList(list, dic);
                dicWeak.Clear();
                list.Clear();
                cacheEntities = null;
            }
            break;

            case 4:
            {
                cacheEntities = new CacheEntity <TValue> [link.Count];
                foreach (TKey tkey in link)
                {
                    if (dicCache.TryGetValue(tkey, out entity))
                    {
                        cacheEntities[index++] = entity;
                        dic[entity]            = tkey;
                    }
                }
                //
                List <CacheEntity <TValue> > list = SortKeys(cacheEntities);
                this.RemovePolicyList(list, dic);
                //
                list.Clear();
                dic.Clear();
                cacheEntities = null;
            }
            break;

            default:
                break;
            }
            link.Clear();
        }
示例#6
0
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool TryRemove(TKey key, out TValue value)
        {
            value = default(TValue);
            bool result = false;
            CacheEntity <WeakReference <object> > kv = null;
            CacheEntity <TValue> entity = null;
            string item = null;

            switch (kvState)
            {
            case 1:
                if (dicWeakKV.TryGetValue(key, out kv))
                {
                    dicWeakKV.Remove(key);
                    result = true;
                    Interlocked.Decrement(ref cacheSize);
                }
                else
                {
                    //如果记录了KEY
                    if (dicKeys.TryRemove(key, out item))
                    {
                        Interlocked.Decrement(ref cacheSize);
                    }
                }

                break;

            case 2:
                if (dicWeakKey.TryGetValue(key, out entity))
                {
                    dicWeakKey.Remove(key);
                    result = true;
                    Interlocked.Decrement(ref cacheSize);
                }
                else
                {
                    //如果记录了KEY
                    if (dicKeys.TryRemove(key, out item))
                    {
                        Interlocked.Decrement(ref cacheSize);
                    }
                }
                break;

            case 3:
                if (dicWeakValue.TryRemove(key, out kv))
                {
                    result = true;
                    Interlocked.Decrement(ref cacheSize);
                }
                break;

            case 4:
                if (dicCache.TryRemove(key, out entity))
                {
                    result = true;
                    Interlocked.Decrement(ref cacheSize);
                }
                break;
            }
            CheckPolicy();
            return(result);
        }
示例#7
0
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetCache(TKey key, out TValue value)
        {
            bool r = false;

            value = default(TValue);
            object v;

            switch (kvState)
            {
            case 1:
            {
                CacheEntity <WeakReference <object> > entityKV = null;
                if (dicWeakKV.TryGetValue(key, out entityKV))
                {
                    if (entityKV.element.Value.TryGetTarget(out v))
                    {
                        value = (TValue)v;
                        entityKV.element.AcessTime = DateTime.Now.Ticks;
                        entityKV.element.Hit++;
                        r = true;
                    }
                    else
                    {
                        //如果value回收
                        dicWeakKV.Remove(key);
                    }
                }
            }
            break;

            case 2:
            {
                CacheEntity <TValue> entityK = null;
                if (dicWeakKey.TryGetValue(key, out entityK))
                {
                    value = entityK.element.Value;
                    entityK.element.AcessTime = DateTime.Now.Ticks;
                    entityK.element.Hit++;
                    r = true;
                }
            }
            break;

            case 3:
            {
                CacheEntity <WeakReference <object> > entityV = null;
                if (dicWeakValue.TryGetValue(key, out entityV))
                {
                    if (entityV.element.Value.TryGetTarget(out v))
                    {
                        value = (TValue)v;
                        entityV.element.AcessTime = DateTime.Now.Ticks;
                        entityV.element.Hit++;
                        r = true;
                    }
                    else
                    {
                        dicWeakValue.TryRemove(key, out entityV);
                    }
                }
            }
            break;

            case 4:
            {
                CacheEntity <TValue> entity = null;
                if (dicCache.TryGetValue(key, out entity))
                {
                    value = entity.element.Value;
                    entity.element.AcessTime = DateTime.Now.Ticks;
                    entity.element.Hit++;
                    r = true;
                }
            }
            break;
            }
            if (CachePolicy.LRU == config.Policy && config.UseMemory)
            {
                stack.Push(key);
            }
            if (!r)
            {
                //没有找到,监测持久化数据
                if (config.FindPers && Store != null && (PersistencePolicy.All == config.PersPolicy || PersistencePolicy.Expire == config.PersPolicy))
                {
                    if (PersItems == null)
                    {
                        PersItems = new PersCache <TKey, TValue>();
                    }
                    if (!PersItems.TryGetValue(key, out value))
                    {
                        if (Store.TryGetValue(key, out value))
                        {
                            PersItems.Add(key, value);
                            r = true;
                        }
                    }
                    else
                    {
                        r = true;
                    }
                }
            }
            CheckPolicy();
            return(r);
        }
示例#8
0
 public int Compare(CacheEntity <T> element, CacheEntity <T> cache)
 {
     return(element.element.WriteTime.CompareTo(cache.element.WriteTime));
 }
示例#9
0
        public bool TryGetCache(TKey key, out TValue value)
        {
            bool r = false;

            value = default(TValue);
            object v = null;

            switch (kvState)
            {
            case 1:
            {
                CacheEntity <WeakReference <object> > entityKV = null;
                if (dicWeakKV.TryGetValue(key, out entityKV))
                {
                    if (entityKV.element.Value.TryGetTarget(out v))
                    {
                        value = (TValue)v;
                        entityKV.element.AcessTime = DateTime.Now.Ticks;
                        entityKV.element.Hit++;
                        r = true;
                    }
                    else
                    {
                        //如果value回收
                        dicWeakKV.Remove(key);
                    }
                }
            }
            break;

            case 2:
            {
                CacheEntity <TValue> entityK = null;
                if (dicWeakKey.TryGetValue(key, out entityK))
                {
                    value = entityK.element.Value;
                    entityK.element.AcessTime = DateTime.Now.Ticks;
                    entityK.element.Hit++;
                    r = true;
                }
            }
            break;

            case 3:
            {
                CacheEntity <WeakReference <object> > entityV = null;
                if (dicWeakValue.TryGetValue(key, out entityV))
                {
                    if (entityV.element.Value.TryGetTarget(out v))
                    {
                        value = (TValue)v;
                        entityV.element.AcessTime = DateTime.Now.Ticks;
                        entityV.element.Hit++;
                        r = true;
                    }
                    else
                    {
                        dicWeakValue.TryRemove(key, out entityV);
                    }
                }
            }
            break;

            case 4:
            {
                CacheEntity <TValue> entity = null;
                if (dicCache.TryGetValue(key, out entity))
                {
                    value = entity.element.Value;
                    entity.element.AcessTime = DateTime.Now.Ticks;
                    entity.element.Hit++;
                    r = true;
                }
            }
            break;
            }
            if (CachePolicy.LRU == config.Policy && config.UseMemory)
            {
                stack.Push(key);
            }
            CheckPolicy();
            return(r);
        }
示例#10
0
 public int Compare(CacheEntity <T> x, CacheEntity <T> y)
 {
     return(policy.Compare(x, y));
 }
示例#11
0
 public int Compare(CacheEntity <T> element, CacheEntity <T> cache)
 {
     return(element.element.Hit.CompareTo(cache.element.Hit));
 }