Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemoryCacheContainer{TKey, TEntity}"/> class.
        /// </summary>
        /// <param name="containerOptions">The container options.</param>
        /// <param name="retrievalOptions">The retrieval options.</param>
        public MemoryCacheContainer(MemoryCacheContainerOptions <TKey> containerOptions, CacheAutoRetrievalOptions <TKey, TEntity> retrievalOptions = null)
            : base(containerOptions, retrievalOptions)
        {
            var capacity = containerOptions?.Capacity;

            Capacity = (capacity.HasValue && capacity.Value > 1) ? capacity : null;
            var equalityComparer = containerOptions?.EqualityComparer ?? EqualityComparer <TKey> .Default;

            container = capacity == null ? new SequencedKeyDictionary <TKey, MemoryCacheItem <TEntity> >(equalityComparer) : new SequencedKeyDictionary <TKey, MemoryCacheItem <TEntity> >(capacity.Value, equalityComparer);
            Statistic = new MemoryCacheStatistic();
        }
        /// <summary>
        /// Generates the class declaration part.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="interfaces">The interfaces.</param>
        /// <returns></returns>
        protected virtual string GenerateClassDeclarationPart(string className, ICollection <Type> interfaces)
        {
            StringBuilder builder = new StringBuilder("public class ", 512);
            SequencedKeyDictionary <string, List <Type> > genericTypes = new SequencedKeyDictionary <string, List <Type> >();

            interfaces = interfaces ?? new Collection <Type>();

            foreach (var one in interfaces)
            {
                var genericParameterConstraints = GetGenericTypeConstraints(one);
                if (genericParameterConstraints.HasItem())
                {
                    foreach (var g in genericParameterConstraints)
                    {
                        genericTypes.Merge(g.Key, g.Value.ToList());
                    }
                }
            }

            builder.Append(className);

            if (genericTypes.HasItem())
            {
                builder.Append("<");
                builder.Append(CSharpCodeGenerateUtil.CombineCode(genericTypes, x => x.Key, 16, StringConstants.CommaChar));
                builder.Append(">");
            }

            if (BaseClassType != null)
            {
                builder.Append(": ");
                builder.Append(BaseClassType.ToCodeLook());
            }

            builder.AppendLine();

            if (interfaces.HasItem())
            {
                builder.Append(",");
                builder.AppendLine(CSharpCodeGenerateUtil.CombineCode(interfaces, x => x.ToCodeLook(), 32, StringConstants.CommaChar));
                builder.AppendLine();
            }

            // Add generic constraints
            foreach (var constraint in genericTypes)
            {
                CSharpCodeGenerateUtil.InternalWriteGenericConstraintsCodeLook(builder, constraint.Key, constraint.Value, new string(CodeIndent[0], CodeIndent.Length * 3));
                builder.AppendLine();
            }

            return(builder.ToString());
        }