asp.net core 運用 Redis 配置步驟

來源:互聯網
上載者:User

標籤:logs   資料   ddd   添加   nec   red   div   name   oid   

Redis可以用來儲存session或直接儲存索引值對

首先要有asp.net core的項目,可以是webapi 或者MVC項目,

還有有本地的Redis或者在遠程伺服器上,具體的安裝就不講述了 以下是具體配置過程:

1.安裝 "Microsoft.Extensions.Caching.Redis.Core": "1.0.3"(版本根據自己的好項目的需求自行選擇,本次以1.0.3為例展示)

2.配置startup.cs

 public void ConfigureServices(IServiceCollection services)    {          services.AddDistributedRedisCache(options =>            {                options.InstanceName = "Session:";                options.Configuration = Configuration.GetConnectionString("Redis");// "139.196.228.246:6379,password=eland2swzl,defaultdatabase=1";//             });    }

 defaultdatabase 定義了資料儲存的位置 1 就是預設載db1中

InstanceName 定義了添加的資料所在的檔案路徑以及首碼,“:”是層次的分隔字元,比如“school:class:student_” 添加的資料("name":"zhangsan")就會放在db1中school檔案夾下,class檔案夾下的student_name中

3.配置controller和應用

public class CustomerController : Controller {      IDistributedCache _distributedCache;       public CustomerController( IDistributedCache distributedCache)          {                _distributedCache = distributedCache;          }       public string Get()         {         //將資料放入redis中                _distributedCache.SetString(“name”, "zhangsan");           var value = _distributedCache.GetString("name");          return value ;          }  }

 

以上即是Redis的使用配置,如果想要吧session的資料直接儲存到Redis中需要添加下Session的包以及做一下配置,session就會自動儲存在redis中。

asp.net core 運用 Redis 配置步驟

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.