u3d对象池

无聊,发一贴代码睡觉,对象池

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Cache with LRU
/// </summary>
/// <typeparam name="T"></typeparam>
public class MemoryCache<T> :IEnumerable{

    protected class NodeInfo<T> {
        public string key;
        public T obj;
    }
    List<NodeInfo<T>> cacheQueue;
    int cacheSize;
	/// <summary>
    /// Cache with LRU
	/// </summary>
	/// <param name="size">cache size</param>
    public MemoryCache(int size){
        cacheQueue = new List<NodeInfo<T>>();
        cacheSize = size;
    }
    /// <summary>
    /// reset cache size
    /// </summary>
    /// <param name="size"></param>
    public void SetCacheSize(int size) {
        cacheSize = size;
    }
    /// <summary>
    /// try get obj from cache
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public T GetFromCache(string key) {
        var cacheObj = cacheQueue.Find(n => {
            return n.key == key;
        });
        if (cacheObj == null) {
            return default(T);
        }
        else {
            cacheQueue.Remove(cacheObj);
            cacheQueue.Insert(0, cacheObj);
            return cacheObj.obj;
        }
    }
    /// <summary>
    /// add or update cache
    /// </summary>
    /// <param name="key"></param>
    /// <param name="obj"></param>
    public void Put(string key,T obj) {
        var cacheObj = cacheQueue.Find(n => {
            return n.key == key;
        });
        if (cacheObj == null) {
            var newNode = new NodeInfo<T>();
            newNode.key = key;
            newNode.obj = obj;
            cacheQueue.Insert(0, newNode);
        }
        else {
            cacheObj.obj = obj;
            cacheQueue.Remove(cacheObj);
            cacheQueue.Insert(0, cacheObj);
        }
        Clear(cacheSize);
    }
    /// <summary>
    /// remove node by key
    /// </summary>
    /// <param name="key"></param>
    public void Remove(string key) {
        var cacheNode = cacheQueue.Find(n => {
            return n.key == key;
        });
        if (cacheNode!=null) {
            cacheQueue.Remove(cacheNode);
        }
    }
    /// <summary>
    /// loop remove item until less than hold num
    /// </summary>
    /// <param name="hold"></param>
    public void Clear(int hold) {
        if (hold<0) {
            hold = 0;
        }
        while (cacheQueue.Count > hold) {
            cacheQueue.RemoveAt(cacheQueue.Count - 1);
        }
        //Resources.UnloadUnusedAssets();
    }
    public int GetCacheSize() {
        return cacheQueue.Count;
    }
    public void PrintAll() {
        for (int i = cacheQueue.Count-1; i >= 0; i--) {
            var node = cacheQueue[i];
            Debug.Log(node.key+":"+node.obj);
        }
    }

    public IEnumerator GetEnumerator() {
        return new MCEnum(this.cacheQueue);
    }
    protected class MCEnum : IEnumerator {
        List<NodeInfo<T>> cacheQueue;
        public MCEnum(List<NodeInfo<T>> cacheQueue) {
            this.cacheQueue = cacheQueue;
        }
        int iter = -1;
        public object Current {
            get {
                if (iter < cacheQueue.Count && iter >= 0) {
                    return cacheQueue[iter].obj;
                }
                return null;
            }
        }

        public bool MoveNext() {
            iter++;
            if (iter < cacheQueue.Count) {
                return true;
            }
            return false;
        }

        public void Reset() {
            iter = -1;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值