自从使用Asp.net Core2.0 以来,不停摸索,查阅资料,这方面的资料是真的少,因此,在前人的基础上,摸索出了Asp.net Core2.0 缓存 MemoryCache 和 Redis的用法,并实现了简单的封装

那么,先给出几个参考资料吧

关于两种缓存:https://www.cnblogs.com/yuangang/p/5800113.html

关于redis持久化:https://blog.csdn.net/u010785685/article/details/52366977

Asp.net Core2.0 缓存 MemoryCache 和 Redis

两个nuget包,不用多说

接下来,贴出代码

首先在startup,我读取了appstting.json的数据,作为redis的配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Common;
using Application.Common.CommonObject;
using Application.CoreWork;
using Application.DAL;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Application.web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Connection.MySqlConnection = Configuration.GetConnectionString("MySqlConnection");
            Connection.SqlConnection = Configuration.GetConnectionString("SqlConnection");
            RedisConfig.Connection = Configuration.GetSection("RedisConfig")["Connection"];
            RedisConfig.DefaultDatabase =Convert.ToInt32( Configuration.GetSection("RedisConfig")["DefaultDatabase"]);
            RedisConfig.InstanceName = Configuration.GetSection("RedisConfig")["InstanceName"];
            CommonManager.CacheObj.GetMessage<RedisCacheHelper>();
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
View Code

相关文章:

  • 2021-11-14
  • 2021-05-18
  • 2022-12-23
  • 2019-11-12
  • 2020-07-22
  • 2021-10-17
  • 2022-12-23
  • 2021-09-28
猜你喜欢
  • 2021-08-23
  • 2022-12-23
  • 2021-09-22
  • 2021-09-12
  • 2022-12-23
  • 2020-10-14
相关资源
相似解决方案