Example #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IRedisKeyResolver keyResolver, IDistributedCache cache, IConnectionMultiplexer redis)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseSession();

            app.Use(async(context, next) =>
            {
                var tempSessionKey = Keys.LastRequestTime + "Temp";

                await context.Session.LoadAsync();
                context.Session.SetString(Keys.LastRequestTime, context.Session.GetString(tempSessionKey) ?? string.Empty);
                context.Session.SetString(tempSessionKey, DateTime.Now.ToString());
                await context.Session.CommitAsync();

                await next.Invoke();
            });

            app.Use(async(context, next) =>
            {
                await next.Invoke();

                await cache.SetStringAsync(
                    keyResolver.GetKeyWithPrefix(Keys.LastRequestTime),
                    DateTime.Now.ToString(),
                    new DistributedCacheEntryOptions {
                    SlidingExpiration = TimeSpan.FromHours(1)
                });
            });

            app.Use(async(context, next) =>
            {
                await next.Invoke();

                var db = redis.GetDatabase();
                await db.StringIncrementAsync(new RedisKey("Counter"), flags: CommandFlags.FireAndForget);
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
        public async Task <IActionResult> Info()
        {
            var appInfoViewModel = new AppInfoViewModel();

            appInfoViewModel.LastRequestTime = await _cache.GetStringAsync(_keyResolver.GetKeyWithPrefix(Keys.LastRequestTime));

            await HttpContext.Session.LoadAsync();

            appInfoViewModel.CurrentUserLastRequestTime = HttpContext.Session.GetString(Keys.LastRequestTime);

            var redisValue = await _redis.GetDatabase().StringGetAsync(new RedisKey("Counter"));

            appInfoViewModel.RedisCounter = redisValue.ToString();

            return(View(appInfoViewModel));
        }