public void AddItemToCache()
        {
            var value = fibonacciFactory.GetFibonacciNumber(6);
            var key   = value.GetHashCode().ToString();


            cacheService.Set(key, value);
            var valueFromCache = cacheService.Get(key);

            Assert.IsNotNull(valueFromCache);
            Assert.AreEqual(value, valueFromCache);
        }
        public IEnumerator <long> GetEnumerator()
        {
            var a = 0L;
            var b = 1L;
            var c = 1L;

            for (var i = 1; i <= _count; i++)
            {
                yield return(c);

                var nextKey   = (i + 1).ToString();
                var nextValue = _cache.Get(nextKey);

                if (nextValue != null)
                {
                    Console.WriteLine("Cache hit");

                    c = nextValue.Value;
                }
                else
                {
                    Console.WriteLine("Cache miss");

                    c = a + b;
                    _cache.Set(nextKey, c);
                }

                a = b;
                b = c;
            }
        }
        private int EvaluateWithCache(int index)
        {
            int lastIndex = cache.GetLastIndex();

            if (index <= lastIndex)
            {
                return(cache.Get(index));
            }

            int currentValue;
            int previousValue;

            if (lastIndex == 0)
            {
                lastIndex     = 1;
                currentValue  = FirstElement;
                previousValue = ZeroElement;
            }
            else
            {
                currentValue  = cache.Get(lastIndex);
                previousValue = lastIndex == 2 ? FirstElement : cache.Get(lastIndex - 1);
            }

            while (++lastIndex <= index)
            {
                this.NextValues(ref previousValue, ref currentValue);
                cache.Set(lastIndex, currentValue);
            }

            return(currentValue);
        }
Beispiel #4
0
        public List <ulong> Calculate(uint step)
        {
            var intStep = Convert.ToInt32(step);

            var resultSet = new List <ulong> {
                0, 1
            };

            if (step < 2)
            {
                return new List <ulong> {
                           0
                }
            }
            ;

            if (step == 2)
            {
                return(resultSet);
            }

            var cacheStep = 2;

            if (_isCacheEnabled)
            {
                resultSet = _cache.Get(CacheKey) ?? resultSet;
                cacheStep = resultSet.Count;

                if (cacheStep >= step)
                {
                    return(resultSet.GetRange(0, intStep));
                }
            }

            for (var i = cacheStep; i < step; i++)
            {
                resultSet.Add(resultSet[i - 1] + resultSet[i - 2]);
            }

            if (_isCacheEnabled)
            {
                _cache.Set(CacheKey, resultSet);
            }

            return(resultSet);
        }
    }
Beispiel #5
0
        public int?GetFibonacciNumber(int index)
        {
            if (index < 1)
            {
                throw new ArgumentException(nameof(index));
            }

            var number = fibonacciCache.Get(index.ToString());

            if (!number.HasValue)
            {
                number = fibonacciFactory.GetFibonacciNumber(index);
                fibonacciCache.Set(index.ToString(), number);
            }

            return(number);
        }