using System; using System.Collections.Generic; using System.Text; namespace Thousandto.Core.Base { /// <summary> /// 缓存数据 -- 根据外部控制决定是否更新缓存 /// </summary> /// <typeparam name="T"></typeparam> public class CacheDataByCommand<T> where T : UnityEngine.Object { public delegate T CacheDataByCommandMethod(); public delegate bool CacheDataDecision(); private CacheDataByCommandMethod _getMethod = null; private CacheDataDecision _decision = null; // 缓存数据更新控制 private bool _cached = false; private T _cacheData; public CacheDataByCommand(CacheDataByCommandMethod getFunc, CacheDataDecision decisionFunc) { _getMethod = getFunc; _decision = decisionFunc; if (_getMethod != null) { _cacheData = _getMethod(); } } ~CacheDataByCommand() { _getMethod = null; _decision = null; _cacheData = null; } public T GetCacheData() { if (_decision != null) { _cached = _decision(); } if (!_cached) { _cached = true; if (_getMethod != null) { _cacheData = _getMethod(); } } return _cacheData; } public void Release() { if (_cacheData != null) { _cacheData = null; } } } }