示例#1
0
        private static void RegisterStaticCache(FieldInfo field)
        {
            var attribute  = (PeekInCacheAttribute)field.GetCustomAttribute(typeof(PeekInCacheAttribute));
            var classCache = field.GetValue(null);

            CacheRefernceStore.Register(attribute.CacheName, classCache);
        }
示例#2
0
        private static void RegisterAspNetCoreDefaultMemoryCache(FieldInfo field)
        {
            var attribute  = (PeekInCacheAttribute)field.GetCustomAttribute(typeof(PeekInCacheAttribute));
            var classCache = new PlaceholderMemoryCache(attribute.CacheName);

            CacheRefernceStore.Register(attribute.CacheName, classCache);
        }
示例#3
0
        public static IServiceCollection PeekInAClass(
            this IServiceCollection service, Type classType)
        {
            var classFields = classType.GetRuntimeFields()
                              .Where(x => x.GetCustomAttribute <PeekInCacheAttribute>() != null).ToArray();

            foreach (var field in classFields)
            {
                var attribute  = (PeekInCacheAttribute)field.GetCustomAttribute(typeof(PeekInCacheAttribute));
                var classCache = field.GetValue(null);
                CacheRefernceStore.Register(attribute.CacheName, classCache);
            }
            return(service);
        }
示例#4
0
        private static object DetermineSerialzableCache(string cacheName)
        {
            object cache = CacheRefernceStore.GetCache(cacheName);
            var    type  = cache.GetType();

            if (type.IsSerializable)
            {
                return(cache);
            }

            if (type.Name.Equals("MemoryCache"))
            {
                return(ExtractSerialzableCacheFromMemoryCache(cache));
            }

            if (type.Name.Equals("PlaceholderMemoryCache"))
            {
                return(ExtractSerialzableCacheFromDefaultMemoryCache(cache));
            }

            return(null);
        }
示例#5
0
        private static string RequestPeekInCacheContent(HttpRequest request)
        {
            if (request.Method != "GET")
            {
                return(null);
            }

            if (!request.Path.StartsWithSegments(new PathString("/PeekInCache")))
            {
                return(null);
            }

            var cacheName = request.Path.Value.Split('/').Last();


            if (!CacheRefernceStore.Exists(cacheName))
            {
                return(null);
            }

            var cache = DetermineSerialzableCache(cacheName);

            return(cache != null?JsonConvert.SerializeObject(cache) : null);
        }