缓存和共享类是应用程序范围内的。我知道一些细节,但情况如下:
我将参数保存在数据库中。 我将它们读取到数据表并将它们存储在缓存中。 每当我需要时,我都会将缓存变量直接广播到数据表,查询它并获取结果。 所以我并不是每次都使用sql server。
替代方案是: 读取数据库并将其存储在类共享变量中 每当需要时,我都会使用这个共享变量(数据表)并查询它。 如果这个变量为空,我从数据库填充ilt。 由于变量是数据表,我继续需要直接广播它。
哪一个是首选?或者我可以使用其中任何一个,因为没有区别。
这个类并不是一个特殊的类。仅包含从数据库读取参数的函数。为了加快速度,我开始使用 aspnet 缓存,但我注意到我不需要这样做,因为类变量可以处理它。
有什么建议吗?您能想到这种仅适用于不会改变的参数的场景的任何可能结果吗?
class Class1
private shared localVar as datatable
public shared function readParam1 as string -- this is my old method
... SELECT from params table
return value
end function
public shared function readParam2 as string -- then I implemented this
if httpcontext.cache variable is empty then
... SELECT from params table
... set cache variable
end if
value = directcast(cachevalue, string)
return value
end function
public shared function readParam3 as string -- now I'm planing to use this
if localVar is empty then
... SELECT from params table
... slocalVar = sql table
end if
value = select value with localVar.Select function
return value
end function
end class
最佳答案
共享变量是这里的赢家: 1.它们会比缓存快一点,因为缓存需要查找字典。 2.它们更具可读性,更易于使用
事实上我不记得缓存的用途。因为你存储在缓存中的任何内容都是共享数据,所以呢?事实上,我对这个问题很好奇,我将打开新的 stackoverflow 问题:asp.net shared variables vs cache
关于asp.net - aspnet 中全局变量的缓存或共享类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28516486/