【问题标题】:cache or shared class for global variables in aspnetaspnet 中全局变量的缓存或共享类
【发布时间】:2015-03-08 10:37:27
【问题描述】:

缓存和共享类是应用程序范围的。我知道一些细节,但这里是场景:

我将参数保存在数据库中。 我将它们读入数据表并将它们存储在缓存中。 每当我需要时,我将缓存变量直接转换为数据表,查询它并获得结果。 所以我不是每次都使用sql server。

替代方案是: 读取数据库并将其存储在类共享变量中 每当需要时,我都会使用这个共享变量(数据表)并查询它。 如果这个变量是空的,我会从 db 中填充 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  

【问题讨论】:

  • 如果值不会改变,我会将它们存储在共享成员中,唯一的区别是我还会在应用程序启动时加载值。

标签: asp.net vb.net class caching shared


【解决方案1】:

共享变量是这里的赢家: 1. 它们会比缓存快一点,因为缓存需要查找字典。 2.它们更易读,更容易使用

事实上,我不记得缓存的用途。因为您存储在缓存中的任何内容都是共享数据,所以呢?事实上我对这个问题很好奇我会打开新的stackoverflow问题:asp.net shared variables vs cache

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 2013-12-09
    • 1970-01-01
    • 2019-01-15
    • 2017-07-11
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多