简体   繁体   English

在ASP.NET Core应用程序中临时存储数据

[英]Storing data temporarily in an ASP.NET Core application

I am using a singleton dictionary collection to store a collection of users so that the users can access the api using their guid to access their credit card details. 我正在使用一个单例字典集合来存储用户集合,以便用户可以使用其GUID访问api来访问其信用卡详细信息。

Is there a more secure way to store data rather than using a singleton dictionary collection? 有没有比使用单例字典集合更安全的数据存储方式? It's only for temporary storage - no database involved. 它仅用于临时存储-不涉及数据库。

I would probably question a single guid being the keys to the kingdom in terms of credit card details... Infact I would also question giving out full credit card details under any circumstances from an API. 我可能会质疑单个guid是信用卡详细信息王国的钥匙...实际上,我也会质疑在任何情况下从API给出完整的信用卡详细信息。

But let's pretend the question is more about temporary storage in general. 但是,让我们假设问题更多是关于临时存储的。

In memory storage is fine, but you will run into two problems. 在内存中存储是可以的,但是您会遇到两个问题。

  1. When your app is restarted, the cache is "lost". 重新启动您的应用程序后,缓存将“丢失”。
  2. You will be unable to scale horizontally as the cache is for a single app. 您将无法水平扩展,因为缓存是针对单个应用程序的。 Other machines within your server pool will have to build their own cache (And you will have other things like sticky sessions to worry about etc). 服务器池中的其他计算机将必须构建自己的缓存(并且您还有其他问题,例如粘性会话,等等)。

A better option is indeed Redis. 确实,Redis是一个更好的选择。 You will need to download redis from here assuming you are on windows and install it. 如果您在Windows上,则需要从此处下载redis并进行安装。 Obviously when deployed you can use something like Azure Redis etc. And for the code, luckily Microsoft have done most of the work for you. 显然,在部署时,您可以使用Azure Redis等。对于代码,幸运的是Microsoft已为您完成了大部分工作。 Taken mostly from this post : http://dotnetcoretutorials.com/2017/01/06/using-redis-cache-net-core/ 主要来自这篇文章: http : //dotnetcoretutorials.com/2017/01/06/using-redis-cache-net-core/

Install this package in your .net core app. 在您的.net核心应用中安装此软件包。

Install-Package Microsoft.Extensions.Caching.Redis

In your configure services method in your startup.cs, you need to add Redis as a cache service. 在startup.cs的configure services方法中,您需要将Redis添加为缓存服务。 You do it like so : 您这样做:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddDistributedRedisCache(option =>
    {
        option.Configuration = "127.0.0.1";
        option.InstanceName = "master";
    });
}

Then you can just ask for the IDistributedCache in your controllers/services. 然后,您可以仅在控制器/服务中请求IDistributedCache。 Something like the following : 类似于以下内容:

public class HomeController : Controller
{
    private readonly IDistributedCache _distributedCache;

    public HomeController(IDistributedCache distributedCache)
    {
        _distributedCache = distributedCache;
    }

    [HttpGet]
    public async Task<string> Get()
    {
        //You can access _distributedCache here. 
    }
}

What happens when application restarts? 应用程序重新启动时会发生什么? Don't you lose data after that? 之后您不会丢失数据吗? If don't (your list is hardcoded) i think it's nothing wrong with collection. 如果没有(您的列表是硬编码的),我认为收藏没有错。 But after each app restart you lose your data and need to reenter again, you can try like https://redis.io/ for caching user collection. 但是,在每个应用程序重新启动后,您将丢失数据并需要重新输入,可以尝试像https://redis.io/一样缓存用户集合。 After application restart you won't lose your collection data. 重新启动应用程序后,您将不会丢失您的收集数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM