如何在c#.net中实现持久化缓存? [英] How to implement persistent cache in c#.net?

查看:577
本文介绍了如何在c#.net中实现持久化缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些数据存储到持久缓存中然后如何在c#.net中实现持久缓存?



我尝试过:



public static void Main()

{

var dictionary = new PersistentDictionary< string,> (姓名);

Console.WriteLine(你的名字是什么?);

string firstName = Console.ReadLine();

if(dictionary.ContainsKey(firstName))

{

Console.WriteLine(欢迎回来{0} {1},

firstName,

字典[firstName]);

}

其他

{

Console.WriteLine(

我不认识你,{0}。你的姓氏是什么?,

firstName);

字典[firstName] =缺点ole.ReadLine();

}

}

I want to store some data into persistent cache then how to implement persistent cache in c#.net?

What I have tried:

public static void Main()
{
var dictionary = new PersistentDictionary<string,>("Names");
Console.WriteLine("What is your first name?");
string firstName = Console.ReadLine();
if (dictionary.ContainsKey(firstName))
{
Console.WriteLine("Welcome back {0} {1}",
firstName,
dictionary[firstName]);
}
else
{
Console.WriteLine(
"I don't know you, {0}. What is your last name?",
firstName);
dictionary[firstName] = Console.ReadLine();
}
}

推荐答案

我认为持久性你的意思是永久性的?您可以使用会话记住该会话中有关用户的信息(谷歌asp.net会话)。如果您想要在人们返回您的网站时记住这些内容,您需要为该人生成一个GUID并将其存储在他们计算机上的cookie中,并将您想要记住的数据存储在数据库中,进行裁判GUID。当用户访问您的网站时,您将阅读GUID并检索他们的数据。



他们当然可以删除您无法控制的cookie,他们将被遗忘。您必须记住http是无状态的,因此存储关于用户的状态永远不会100%可靠。



至于您的代码示例,我相信其含义该类中的Persistent仅指该类的实例。因此,如果您有一个包含5个项目的字典并且添加了第6个,那么不是第6个元素被添加到字典中,而是字典保持不变并且返回包含6个项目的新字典。我猜这不是你想要的。
I assume by persistent you mean permanent? You can use the Session to remember things about the user on that session (google "asp.net session"). If you want to remember things from people when they return to your site you'll need to generate a GUID for that person and store it in a cookie on their machine and also store the data you want to remember about them in a database, refereeing the GUID. When the user visits your site you'll read the GUID and retrieve their data.

They can, of course, delete the cookie over which you have no control, they will be forgotten. You have to remember that http is "stateless" so storing state about users is never going to be 100% reliable.

As to your code sample, I believe the meaning of Persistent in that class refers only to the instance of that class. So if you have a dictionary with 5 items and you add a 6th, then rather than the 6th element being added to the dictionary what happens is that the dictionary remains unchanged and a new dictionary with 6 items is returned instead. I'm guessing that's not what you want.


这篇关于如何在c#.net中实现持久化缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆