Example #1
0
        private CacheTemplate GetCacheTemplateFromProductRepository()
        {
            var type  = typeof(IProductRepository);
            var model = new CacheTemplate(type);

            var getProduct      = type.GetMethod(nameof(IProductRepository.GetProduct));
            var getProductAsync = type.GetMethod(nameof(IProductRepository.GetProductAsync));

            var getProductConfig = new MethodCacheConfiguration()
            {
                Name = getProduct.Name,
                ExpirationSeconds = 60,
                KeyTemplate       = "product_{id}"
            };
            var getProductAsyncConfig = new MethodCacheConfiguration()
            {
                Name = getProduct.Name,
                ExpirationSeconds = 60,
                KeyTemplate       = "product_async_{id}"
            };

            model.AddMethod(new CacheMethodTemplate(getProduct, getProductConfig));
            model.AddMethod(new CacheMethodTemplate(getProductAsync, getProductAsyncConfig));

            return(model);
        }
        private dynamic GetOrUpdateDynamic(MethodInfo targetMethod, object[] args, MethodCacheConfiguration methodCacheConfiguration)
        {
            Type returnType      = targetMethod.ReturnType;
            bool async           = returnType.BaseType == typeof(Task);
            Type realReturnType  = async ? returnType.GenericTypeArguments[0] : returnType;
            Type typeMethodCache = typeof(MethodCacheProxy <>).MakeGenericType(realReturnType);

            dynamic methodCache = Activator.CreateInstance(typeMethodCache, targetMethod, Decorated, args);

            methodCache.CacheFormatter     = CacheFormatter;
            methodCache.CacheConfiguration = methodCacheConfiguration;
            return(async ? methodCache.GetValueAsync() : methodCache.GetValue());
        }
Example #3
0
        private CacheTemplate GetCacheTemplateFromGroupRepository()
        {
            var type  = typeof(IGroupRepository);
            var model = new CacheTemplate(type);

            var getGroup       = type.GetMethod(nameof(IGroupRepository.GetGroup));
            var getGroupConfig = new MethodCacheConfiguration()
            {
                Name = getGroup.Name,
                ExpirationSeconds = 60,
                KeyTemplate       = "group_{id}"
            };

            model.AddMethod(new CacheMethodTemplate(getGroup, getGroupConfig));
            return(model);
        }
Example #4
0
        public CacheMethodTemplate(MethodInfo methodInfo = null, MethodCacheConfiguration methodConfiguration = null)
        {
            if (methodInfo == null)
            {
                return;
            }

            var returnType = methodInfo.ReturnType;

            Async                  = returnType.BaseType == typeof(Task);
            ReturnType             = returnType.GetDeclarationType();
            ReturnTypeBase         = Async ? returnType.GenericTypeArguments[0].GetDeclarationType() : ReturnType;
            Name                   = methodInfo.Name;
            ParametersNames        = string.Join(",", methodInfo.GetParameters().Select(x => x.Name));
            ParametersDeclarations = string.Join(",", methodInfo.GetParameters().Select(x => x.GetVarDeclaration()));
            Enabled                = methodConfiguration != null;
            CacheExpirationSeconds = methodConfiguration?.ExpirationSeconds ?? 0;
            CacheKeyTemplate       = methodConfiguration?.KeyTemplate;
        }