v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 编写于 2018-06-14 17:56:44
X
using System;
using NewLife.Log;

namespace XCode.Statistics
{
    /// <summary>统计助手类</summary>
    public static class StatHelper
    {
        private static String _Last;
        /// <summary>获取 或 新增 统计对象</summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="model"></param>
        /// <param name="find"></param>
        /// <param name="onCreate"></param>
        /// <returns></returns>
        public static TEntity GetOrAdd<TEntity, TModel>(TModel model, Func<TModel, Boolean, TEntity> find, Action<TEntity> onCreate = null)
            where TModel : StatModel
            where TEntity : Entity<TEntity>, IStat, new()
        {
            if (model == null) return null;

            var st = find(model, true);
            //查询到结果保存
            if (st == null)
            {
                st = new TEntity
                {
                    Level = model.Level,
                    Time = model.Time,
                    CreateTime = DateTime.Now,
                };

                onCreate?.Invoke(st);

                // 插入失败时,再次查询
                try
                {
                    st.Insert();
                }
                catch (Exception ex)
                {
                    st = find(model, false);
                    if (st == null)
                    {
                        ex = ex.GetTrue();
                        if (ex.Message != _Last)
                        {
                            _Last = ex.Message;
                            XTrace.WriteException(ex);
                        }

                        return null;
                    }
                }
            }

            if (st != null) st.UpdateTime = DateTime.Now;

            return st;
        }
    }
}