Beispiel #1
0
        public static IServiceCollection PeekInAllClasses(
            this IServiceCollection service)
        {
            var currentAssembly = Assembly.GetEntryAssembly();
            var classes         = currentAssembly.GetTypes()
                                  .Where(x => x.IsClass &&
                                         x.GetRuntimeFields().Any(y => y.GetCustomAttribute <PeekInCacheAttribute>() != null));

            ServiceCollectionRefernce.RegisterServiceCollection(service);

            foreach (var @class in classes)
            {
                var classFields = @class.GetRuntimeFields()
                                  .Where(x => x.GetCustomAttribute <PeekInCacheAttribute>() != null)
                                  .ToArray();

                foreach (var field in classFields)
                {
                    if (field.IsStatic)
                    {
                        RegisterStaticCache(field);
                        continue;
                    }

                    if (IsAspNetCoreDefaultMemoryCache(field))
                    {
                        RegisterAspNetCoreDefaultMemoryCache(field);
                    }
                }
            }
            return(service);
        }
Beispiel #2
0
        private static object ExtractSerialzableCacheFromDefaultMemoryCache(object placeholder)
        {
            if (!(placeholder is PlaceholderMemoryCache))
            {
                return(null);
            }
            var memoryCacheObject = ServiceCollectionRefernce.GetAspDotNetDefaultMemoryCache();

            var type = memoryCacheObject.GetType();
            var rawCacheFiledInfo = type.GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance);

            var cache = rawCacheFiledInfo?.GetValue(memoryCacheObject);

            return(cache);
        }
Beispiel #3
0
 public static IApplicationBuilder UsePeekInCache(
     this IApplicationBuilder app)
 {
     ServiceCollectionRefernce.RegisterServiceProvider(app.ApplicationServices);
     return(app.UseMiddleware <PeekInAspDotNetCacheMiddleware>());
 }