它是确定以使用静态变量在ASP.net缓存信息? [英] Is it OK to use static variables to cache information in ASP.net?

查看:371
本文介绍了它是确定以使用静态变量在ASP.net缓存信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在上ASP.net项目管理应用程序在C#3.5。为了减少点击到数据库,我缓存大量的使用静态变量的信息。例如,用户的列表保存在存储器中的静态类。这个类在从启动数据库的所有信息读取,并不断更新,每当更改后的数据库,但它从来没有需要从DATEBASE阅读。

At the moment I am working on a project admin application in C# 3.5 on ASP.net. In order to reduce hits to the database, I'm caching a lot of information using static variables. For example, a list of users is kept in memory in a static class. The class reads in all the information from the database on startup, and will update the database whenever changes are made, but it never needs to read from the datebase.

类坪其他Web服务器(如果它们存在的话)与在相同的时间,因为对数据库的写入的更新信息。该侦测机制是使用随机可用端口到高速缓存对象注册一个Windows服务。它被用于其他事情以及

The class pings other webservers (if they exist) with updated information at the same time as a write to the database. The pinging mechanism is a Windows service to which the cache object registers using a random available port. It is used for other things as well.

数据量是不是所有的伟大。目前我使用它只是缓存的用户(密码哈希,权限,姓名,电子邮件等),它只是节省了一堆电话到数据库正在取得进展。

The amount of data isn't all that great. At the moment I'm using it just to cache the users (password hashes, permissions, name, email etc.) It just saves a pile of calls being made to the database.

如果有任何陷阱,以这种方法我在想,和/或是否有更好的方法来缓存数据?

I was wondering if there are any pitfalls to this method and/or if there are better ways to cache the data?

推荐答案

一个陷阱:静态字段每个应用程序域范围,并且增加的负载会使服务器生成池中有更多的应用程序域。这不一定是一个问题,如果你只从静态看,但你会在内存中得到重复的数据,并且每次创建或回收的应用程序域时,你会得到一击。

A pitfall: A static field is scoped per app domain, and increased load will make the server generate more app domains in the pool. This is not necessarily a problem if you only read from the statics, but you will get duplicate data in memory, and you will get a hit every time an app domain is created or recycled.

最好使用Cache对象 - 这是针对这样的事情

Better to use the Cache object - it's intended for things like this.

编辑:原来我错了应用程序域(如在评论中指出) - 的的应用程序的将荷载作用下生成的多个实例,但他们会在同一个AppDomain中所有运行。 (但你还是应该使用Cache对象!)

Turns out I was wrong about AppDomains (as pointed out in comments) - more instances of the Application will be generated under load, but they will all run in the same AppDomain. (But you should still use the Cache object!)

这篇关于它是确定以使用静态变量在ASP.net缓存信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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