How to remove image from cache when Image is changed RRS feed

  • Question

  • Hello Everyone, I have perform Image Caching by following instruction of this article http://www.codeproject.com/KB/aspnet/CachingImagesInASPNET.aspx Based on it, I have one question. What if I have a portal in which user can change their profile image, and as soon as user change their profile image, new image should display rather than cache image, is it possible that i can remove previously stored user image from cache? One way is to change name of image, but it is not possible because of nature of application, so how can i change image instantly on change of user image? Example: User4.jpg is name of image, this image name will remain same when User upload new image, but new image will never appears until cache expires so he will always see old User4.jpg image despite he just changed the image. Is there any way I can disallow caching of Image for particular folder? or any solution for this problem? Its very hard for me to figure out solution for this problem, your help would very much appreciated.

    • Moved by nobugz Tuesday, May 5, 2009 8:17 PM forums.asp.net (From:.NET Base Class Library)
    Tuesday, May 5, 2009 1:35 PM


All replies

  • Hello Vivek,


    Thank you for your post!  I would suggest posting your question in one of the MS Forum,

    HomeASP.NET Forums » General ASP.NET » State Management 

    located here:  http://forums.asp.net/22.aspx 

    Have a great day!

    Shrikant Maske Tier 2 Application Support Server and Tools Online Engineering Live Services Team
    • Proposed as answer by Shrikant Maske Wednesday, May 6, 2009 12:29 AM
    • Unproposed as answer by dng_online Wednesday, May 6, 2009 3:19 AM
    Wednesday, May 6, 2009 12:29 AM
  • Hi Shrikant,

    That dosen't solve my problem.

    Please referred this article http://www.codeproject.com/KB/aspnet/CachingImagesInASPNET.aspx   I am looking for answer in this context.

    I have code as mentioned below:  Any help would be much appreciated.  Thanks
       public class CachingHandler : IHttpHandler
            public bool IsReusable
                get { return true; }
            //public ArrayList ChangeImagesFileName = new ArrayList();
            public void ProcessRequest(HttpContext context)
                //string file = context.Server.MapPath(context.Request.FilePath.Replace(".ashx", ""));
                string file = context.Server.MapPath(context.Request.FilePath);
                string filename = file.Substring(file.LastIndexOf('\\') + 1);
                string extension = file.Substring(file.LastIndexOf('.') + 1);
                //Get Last Modified Time of Image file
                //FileInfo fileAttributes = new FileInfo(file);
                //DateTime dt = fileAttributes.LastWriteTimeUtc;
                CachingSection config = (CachingSection)context.GetSection("CachingHandler/Caching");
                if (config != null)
                    //if ((dt - DateTime.UtcNow).Hours > 48) //Retrieve from cache only if file is not modified in last 48 hours.
                    //    context.Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(0));
                    //    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    //    //Remove from Caching because Image is modified in last 48 hours.
                    //    context.Response.Cache.SetValidUntilExpires(true);
                    FileExtension fileExtension = config.FileExtensions[extension];
                    if (fileExtension != null)
                        context.Response.ContentType = fileExtension.ContentType;
                //Get Last Modified Time of Image file
                FileInfo fileAttributes = new FileInfo(file);
                DateTime dt = fileAttributes.LastWriteTimeUtc;            
                if ((dt - DateTime.UtcNow).Hours > 48 && filename.Substring(0, 4).ToUpper() != "USER") //Retrieve from cache only if file is not modified in last 48 hours.
                    context.Response.AddHeader("content-disposition", "inline; filename=" + filename);
                    context.Response.AddHeader("content-disposition", "inline; filename=" + filename);

    Wednesday, May 6, 2009 3:18 AM
    • Proposed as answer by dotnetguts_mvp Saturday, May 16, 2009 1:25 PM
    • Marked as answer by dng_online Saturday, May 16, 2009 1:28 PM
    Saturday, May 16, 2009 1:24 PM
  • Start a log firle to track the dates that the changes take effec tthen all you will have to do is to remove any item diffenrent by dat eform the original posted image
    Sunday, May 17, 2009 1:04 AM