Example #1
0
 public virtual void Release(ObjectCacheBase t)
 {
     t.Released();
     if (!CircleCaches.ContainsKey(t.Key))
     {
         CircleCaches.Add(t.Key, t);
     }
 }
Example #2
0
 private void Generator(int size)
 {
     while (size-- > 0)
     {
         ObjectCacheBase circle = (ObjectCacheBase)Activator.CreateInstance(Type);
         if (circle != null)
         {
             CircleCaches.Add(circle.Key, circle);
         }
     }
 }
Example #3
0
        public virtual ObjectCacheBase Acquire()
        {
            if (CircleCaches.Count == 0)
            {
                // 生产太多,会多创建空壳。后面从缓存中选取,可能是空壳,进一步需要初始化
                Generator(1);
            }
            var itr = CircleCaches.Values.GetEnumerator();

            itr.MoveNext();
            ObjectCacheBase circle = itr.Current;

            circle.Acquired();
            CircleCaches.Remove(circle.Key);
            return(circle);
        }
Example #4
0
        public void Release(ObjectCacheBase obj)
        {
            if (obj == null)
            {
                return;
            }
            Type type = obj.GetType();

            Debug.Assert(type.IsSubclassOf(typeof(ObjectCacheBase)), "wrong type");
            if (Pools.ContainsKey(type))
            {
                Pools[type].Release(obj);
            }
            else
            {
                obj.Released();
            }
        }
Example #5
0
        public void RemoveExpired(int lifeSeconds)
        {
            var   itr       = CircleCaches.Values.GetEnumerator();
            float timeStamp = Time.realtimeSinceStartup;

            ExpiredKeys.Clear();
            while (itr.MoveNext())
            {
                ObjectCacheBase item = itr.Current;
                if (item.IsExpired(LifeTimeSecond))
                {
                    ExpiredKeys.Add(item.Key);
                }
            }
            foreach (uint key in ExpiredKeys)
            {
                CircleCaches.Remove(key);
            }
        }
Example #6
0
        public virtual void Acquire(int num, List <ObjectCacheBase> result)
        {
            if (CircleCaches.Count < num)
            {
                Generator(num - CircleCaches.Count);
            }
            var itr = CircleCaches.Values.GetEnumerator();

            while (itr.MoveNext() && result.Count < num)
            {
                ObjectCacheBase circle = itr.Current;
                circle.Acquired();
                result.Add(circle);
            }

            for (int i = 0; i < num; ++i)
            {
                CircleCaches.Remove(result[i].Key);
            }
        }