C# ListPool 缓存数据结构

可重复使用(缓存)结构

1.利用静态类&静态方法获取某个数据结构。

2.利用静态变量在所有类成员中公用的原理,达到使用时(分情况)获取,不使用时不释放而是缓存以便下次使用的目的。


适用环境

  1.该数据结构存储的数据为临时数据,短时间使用后会被释放。

  2.某一帧内多次重复使用同一数据结构。(例如for循环中,当然你用循环外临时变量也是ok的)

  3.某些特殊情况,比如多线程的频繁交互。

使用方式:

  1.通过静态方法获取数据结构

  2.用完还给静态方法(一定记住)


代码:

使用方式:
        private void Start() 
        {
            List<PaintToolbarItem> list = ListPool<PaintToolbarItem>.Pop();
               
           // 使用

            ListPool<PaintToolbarItem>.Push(list);
        }        
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Game
{
    public class CSObjectPool<T> where T : class, new()
    {
        private Stack<T> m_ObjStack = new Stack<T>();
        private Action<T> m_OnPop;
        private Action<T> m_OnPush;
        
        
        public CSObjectPool(Action<T> onPop, Action<T> onPush)
        {
            m_OnPop = onPop;
            m_OnPush = onPush;
        }
        
        
        /// <summary>
        /// 取出
        /// </summary>
        public T Pop()
        {
            T obj = null;
            if(m_ObjStack.Count == 0)
            {
                obj = new T();
            }
            else
            {
                obj = m_ObjStack.Pop();
            }
            
            if(obj != null && m_OnPop != null)
            {
                m_OnPop(obj);
            }
            return obj;
        }
        
        /// <summary>
        /// 放回
        /// </summary>
        public void Push(T obj)
        {
            if(obj == null)
                return;
            
            if(m_ObjStack.Count > 0 && ReferenceEquals(m_ObjStack.Peek(), obj))
            {
                Debug.LogError(string.Format("CSObjectPool error. Trying to push object that is already in the pool, obj = {0}", obj));
                return;
            }
            
            if(m_OnPush != null)
            {
                m_OnPush(obj);
            }
            m_ObjStack.Push(obj);
        }
    }
}
View Code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Game
{
    public static class ListPool<T>
    {
        private static CSObjectPool<List<T>> m_Pools = new CSObjectPool<List<T>>(null, x => x.Clear());

        /// <summary>
        /// 取出
        /// </summary>
        public static List<T> Pop()
        {
            return m_Pools.Pop();
        }
        /// <summary>
        /// 放回
        /// </summary>
        public static void Push(List<T> list)
        {
            m_Pools.Push(list);
        }
    }


    public static class HashSetPool<T>
    {
        private static CSObjectPool<HashSet<T>> m_Pools = new CSObjectPool<HashSet<T>>(null, x => x.Clear());

        /// <summary>
        /// 取出
        /// </summary>
        public static HashSet<T> Pop()
        {
            return m_Pools.Pop();
        }
        /// <summary>
        /// 放回
        /// </summary>
        public static void Push(HashSet<T> hashSet)
        {
            m_Pools.Push(hashSet);
        }
    }

}
View Code

猜你喜欢

转载自www.cnblogs.com/jwv5/p/11387241.html
今日推荐