zoukankan      html  css  js  c++  java
  • asp.net core 运用 Redis 配置步骤

    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中。

  • 相关阅读:
    Key-Value Memory Network
    Deep Mask Memory Network with Semantic Dependency and Context Moment for Aspect Level Sentiment Clas
    Deep Memory Network在Aspect Based Sentiment方向上的应用
    Deep Memory Network 深度记忆网络
    Self Attention 自注意力机制
    Attention基本公式及其变种
    *端策略优化算法(PPO)
    Policy Gradient 算法
    一本通 农场派对
    A
  • 原文地址:https://www.cnblogs.com/webenh/p/11597509.html
Copyright © 2011-2022 走看看