public LiquidAST Generate(string template, Action <LiquidError> errorAccumulator)
        {
            // We need to pass the errors back to the parent, but we
            // also need to avoid saving in the cache if an error is generated.
            errorAccumulator = errorAccumulator ?? (err => { });
            IList <LiquidError> errors = new List <LiquidError>();

            Action <LiquidError> decoratedAccumulator = err =>
            {
                errors.Add(err);
                errorAccumulator(err);
            };

            String      hash      = CacheKey(template);
            ObjectCache cache     = MemoryCache.Default;
            var         liquidAST = cache[hash] as LiquidAST;

            if (liquidAST == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy
                {
                    SlidingExpiration = _slidingExpiration
                };
                // TODO: If there are errors, don't caceh the template
                liquidAST = _generator.Generate(template, decoratedAccumulator);

                if (!errors.Any() && liquidAST != null)
                {
                    cache.Set(hash, liquidAST, policy);
                }
            }
            return(liquidAST);
        }
Ejemplo n.º 2
0
        public LiquidAST Generate(string template, Action <LiquidError> errorAccumulator)
        {
            // We need to pass the errors back to the parent, but we
            // also need to avoid saving in the cache if an error is generated.
            errorAccumulator = errorAccumulator ?? (err => { });
            IList <LiquidError> errors = new List <LiquidError>();

            Action <LiquidError> decoratedAccumulator = err =>
            {
                errors.Add(err);
                errorAccumulator(err);
            };

            String hash = CacheKey(template);
            //ObjectCache cache = MemoryCache.Default;


            //var liquidAST = cache[hash] as LiquidAST;
            LiquidAST liquidAST;
            bool      found = _cache.TryGetValue(hash, out liquidAST);

            if (!found)
            {
                //CacheItemPolicy policy = new CacheItemPolicy
                //{
                //    SlidingExpiration = _slidingExpiration
                //};
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        // Keep in cache for this time, reset time if accessed.
                                        .SetSlidingExpiration(_slidingExpiration);
                // TODO: If there are errors, don't caceh the template
                liquidAST = _generator.Generate(template, decoratedAccumulator);

                if (!errors.Any() && liquidAST != null)
                {
                    _cache.Set(hash, liquidAST, cacheEntryOptions);
                }
            }
            return(liquidAST);
        }