Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Will the static get accessor of a property be cached ?

Will the static get accessor of a property be cached ?

Scheduled Pinned Locked Moved ASP.NET
asp-netcsharparchitecturehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nadia Monalisa
    wrote on last edited by
    #1

    Hello, I have 2 closely related questions, would you please help. 1. In my ASP.NET MVC application, I am encapsulating the Logged In User Name within a static property in a class as

    public static string LoggedInUserName
    {
    get
    {
    return HttpContext.Current.User.Identity.Name;
    }
    }

    Now, I am wondering, as the property is static, will that value be cached so that if a new user visit the website, the last returned user's user name will be returned by the Property ? I am expecting that, every call to the property 'LoggedInUserName' the fresh new Identity value will be returned. Am not I right ? 2. If I want to cache a value within the application life cycle, I know I can use a static field. But, is it required that the static field be public ? Or any private static field is also cached within the application life cycle as well ? Regards.

    J 1 Reply Last reply
    0
    • N Nadia Monalisa

      Hello, I have 2 closely related questions, would you please help. 1. In my ASP.NET MVC application, I am encapsulating the Logged In User Name within a static property in a class as

      public static string LoggedInUserName
      {
      get
      {
      return HttpContext.Current.User.Identity.Name;
      }
      }

      Now, I am wondering, as the property is static, will that value be cached so that if a new user visit the website, the last returned user's user name will be returned by the Property ? I am expecting that, every call to the property 'LoggedInUserName' the fresh new Identity value will be returned. Am not I right ? 2. If I want to cache a value within the application life cycle, I know I can use a static field. But, is it required that the static field be public ? Or any private static field is also cached within the application life cycle as well ? Regards.

      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      To the best of my knowledge, none of the code you write or compiled is cached. You seem to have the term cache confused with the session state of the http server (web server)and the relationship of the http context that is created for each browser that connects to the http server.

      N 1 Reply Last reply
      0
      • J jkirkerx

        To the best of my knowledge, none of the code you write or compiled is cached. You seem to have the term cache confused with the session state of the http server (web server)and the relationship of the http context that is created for each browser that connects to the http server.

        N Offline
        N Offline
        Nadia Monalisa
        wrote on last edited by
        #3

        Hi, Thank you so much for your reply, actually I meant cache not by the Http Cache. I meant, I can keep the data live in the Memory as long as the Application is live (Web.Config is not modified or application is not restarted). So, do you think, my Public static get accessor will return the same data for every time just because it is static ? I know if I used a static field inside the get accessor than, the value is not lost from memory.

        J 1 Reply Last reply
        0
        • N Nadia Monalisa

          Hi, Thank you so much for your reply, actually I meant cache not by the Http Cache. I meant, I can keep the data live in the Memory as long as the Application is live (Web.Config is not modified or application is not restarted). So, do you think, my Public static get accessor will return the same data for every time just because it is static ? I know if I used a static field inside the get accessor than, the value is not lost from memory.

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          No, The data lives in the web servers session context, your just writing code that talks to the web server if the data exist. So you also need to make sure that the data exist before you ask for the data, or else the code will bomb with an object not found. Once the session expires, usually 20 minutes, that's it, the handle to the web servers session context is deleted, and your data is cleared. You can't store session data forever, it's has a short ( finite ) lifespan. If you want to store temporary data, then you write to the database on the server side, or write a cookie on the client side. And ask the browser for the cookie if it exist thus - Request.Cookie 3rd edit: Don't worry about the code, or whether it is public or private. Technically, the object should already be global, in which you can access the data on another web page using the same code. If your using the code to remember who the user is, on many pages after they sign in, then your alright. Just make sure to check the identity when needed.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups