public virtual void Release(ObjectCacheBase t) { t.Released(); if (!CircleCaches.ContainsKey(t.Key)) { CircleCaches.Add(t.Key, t); } }
private void Generator(int size) { while (size-- > 0) { ObjectCacheBase circle = (ObjectCacheBase)Activator.CreateInstance(Type); if (circle != null) { CircleCaches.Add(circle.Key, circle); } } }
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); }
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(); } }
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); } }
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); } }