ASP.NET Caching and File Dependencies

I want the ASP.NET cache element to be returned when a particular file is affected, but the following code does not work:

                       HttpContext.Current.Cache.Insert(
                            "Key",
                            SomeObject,
                            new CacheDependency(Server.MapPath("SomeFile.txt")),
                            DateTime.MaxValue,
                            TimeSpan.Zero,
                            CacheItemPriority.High,
                            null);

"SomeFile.txt" does not seem to be checked when I click on the cache, and changing it does not invalidate this element.

What am I doing wrong?

+3
source share
5 answers

Solution to the problem:

This was a unique and interesting problem, so I will document the cause and solution here as an answer for future search engines.

Something I forgot about in my question was that this cache insert occurred in a service class that implements a singleton pattern.

In a nutshell:

public class Service 
{
        private static readonly Service _Instance = new Service();
        static Service () { }
        private Service () { }

        public static Service Instance
        {
            get { return _Instance; }
        }

        // The expensive data that this service exposes      
        private someObject _data = null;

        public someObject Data
        {
            get
            {
                 if (_data == null)
                     loadData();
                 return _data;
            }
        }


        private void loadData()
        {
            _data = GetFromCache();
            if (_data == null)
            {
                 // Get the data from our datasource
                 _data = ExpensiveDataSourceGet();

                 // Insert into Cache
                 HttpContext.Current.Cache.Insert(etc);
            }
        }
}

, . , , , , .

Cache.Insert , , , - . , , , "_data" reset null, singleton .

, , , , .

?

 HttpContext.Current.Cache.Insert(
     "Key",
      SomeObject,
      new CacheDependency(Server.MapPath("SomeFile.txt")),
      DateTime.MaxValue,
      TimeSpan.Zero,
      CacheItemPriority.High,
      delegate(string key, object value, CacheItemRemovedReason reason)
      {
          _data = null;
      }
 );

, ... .

? .

+5

ASP.NET , CacheDependency? , , CacheDependency .

+1

, :

var d = new CacheDependency(Server.MapPath("SomeFile.txt"));

~\App_Data.

0

. , , .

  • ?

  • . , - "". :.

    public partial class _Default : System.Web.UI.Page
    {
       CacheDependency dep;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dep = new CacheDependency(Server.MapPath("SomeFile.txt"));
            HttpContext.Current.Cache.Insert(
                        "Key",
                        new Object(),
                        dep,
                        DateTime.MaxValue,
                            TimeSpan.Zero, CacheItemPriority.High, null);
        }
    
    
        if (dep.HasChanged)
            Response.Write("changed!");
        else
            Response.Write("no change :(");   }}
    
0

The only way to reproduce this behavior is if the path provided by the CacheDependency constructor does not exist. CacheDependency will not throw an exception if the path does not exist, so it can be a little misleading.

0
source

Source: https://habr.com/ru/post/1707377/


All Articles