Example #1
0
        /// <summary>
        /// 获取缓存的一级属性的值,如超过指定时间间隔,则重新计算
        /// </summary>
        /// <param name="propIndex"></param>
        /// <param name="factoryFunc"></param>
        /// <param name="cache"></param>
        /// <returns></returns>
        public double GetBasePropsValue(int propIndex, Func <double> factoryFunc, bool cache = true)
        {
            if (cache)
            {
                long age;
                long nowTicks = TimeUtil.CurrentTicksInexact;
                PropsValueFactory propsValueFactory;
                lock (mutex)
                {
                    if (!basePropsValueDict.TryGetValue(propIndex, out propsValueFactory))
                    {
                        propsValueFactory = new PropsValueFactory()
                        {
                            propIndex     = propIndex,
                            nextCalcTicks = nowTicks,
                            factoryFunc   = factoryFunc,
                        };

                        basePropsValueDict[propIndex] = propsValueFactory;
                    }

                    if (propsValueFactory.nextCalcTicks > nowTicks)
                    {
                        return(propsValueFactory.propValue);
                    }

                    propsValueFactory.Age++;
                    age = propsValueFactory.Age;
                }

                double propValue = propsValueFactory.factoryFunc();
                lock (mutex)
                {
                    if (propsValueFactory.Age == age)
                    {
                        propsValueFactory.nextCalcTicks = nowTicks + GameManager.FlagRecalcRolePropsTicks;
                        propsValueFactory.propValue     = propValue;
                    }

                    return(propValue);
                }
            }

            return(factoryFunc());
        }
Example #2
0
        public double GetExtPropsValue(int propIndex, Func <double> factoryFunc)
        {
            long nowTicks = TimeUtil.CurrentTicksInexact;
            PropsValueFactory propsValueFactory;
            long age;

            lock (this.mutex)
            {
                if (!this.extPropValueDict.TryGetValue(propIndex, out propsValueFactory))
                {
                    propsValueFactory = new PropsValueFactory
                    {
                        propIndex     = propIndex,
                        nextCalcTicks = nowTicks,
                        factoryFunc   = factoryFunc
                    };
                    this.extPropValueDict[propIndex] = propsValueFactory;
                }
                if (propsValueFactory.nextCalcTicks > nowTicks)
                {
                    return(propsValueFactory.propValue);
                }
                propsValueFactory.Age += 1L;
                age = propsValueFactory.Age;
            }
            double propValue = propsValueFactory.factoryFunc();
            double result;

            lock (this.mutex)
            {
                if (propsValueFactory.Age <= age)
                {
                    propsValueFactory.nextCalcTicks = nowTicks + GameManager.FlagRecalcRolePropsTicks;
                    propsValueFactory.propValue     = propValue;
                }
                result = propValue;
            }
            return(result);
        }