2

I store a number of large bitmaps that I make dynamically, using session variables using the following:

   public static MySession Current
        {
            get
            {
                MySession session =
                  (MySession)HttpContext.Current.Session["__MySession__"];
                if (session == null)
                {
                    session = new MySession();
                    HttpContext.Current.Session["__MySession__"] = session;
                }
                return session;
            }
        }

Would using disk caching is better, and if so is there a good example or documentation. Thanks in advance.

hncl
  • 2,295
  • 7
  • 63
  • 129

1 Answers1

3

Storing large items in SessionState is generally a bad idea - it will limit the scalability of your application due to usage of server memory. Even if you move SessionState to SQL, it will add to the IO and storage requirements of your app.

Below, I'm assuming you have a dynamic image generation action on a controller which is then referenced, e.g. <img src='http://myserver/image/generate/wmAvatar' >, i.e. the reason that you are rendering dynamic images is for the consumption of a browser?

If the dynamic images are specific 'per user', or per session: Instead of using session state, generate and deliver the images dynamically with appropriate Http Caching headers, and they should then be cached by the browser. You may still need to handle the case for If-Modified-Since requests

If the images can be shared between multiple users, or at least re-used by the same user across sessions then yes, you could store them to disk (e.g. SSD) in a folder configured for appropriate caching (and even precompute the images if you can) and then your img links will no longer be dynamic (http://myserver/images/123456.jpg ). You will however need to handle cleanup of expired images, and also handle 404 type errors for deleted images. As above, use Http caching headers to reduce unnecessary I/O. However nowadays, caching in memory with a key value / NoSql database is also common, e.g. Redis, which can then scale in the cloud, e.g. Elasticache

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285