简体   繁体   English

ASP.NET缓存问题

[英]ASP.NET cache issue

I'm caching MyData to avoid unnecessary calls to web service but it seems there is some problem. 我正在缓存MyData以避免不必要的对Web服务的调用,但是似乎存在一些问题。 It is possible that caching is working correctly but I failed to figure out what causes problems. 缓存可能正常工作,但是我无法弄清楚是什么原因引起的。 This is how I cache my data. 这就是我缓存数据的方式。 I use static cache helper. 我使用静态缓存帮助器。 You can google "cache helper c#" if you need that piece of code. 如果您需要这段代码,则可以使用Google“缓存帮助器c#”。

     private static object locker = new object();
    private static List<SomeDataDto> myData;
    private static List<SomeDataDto> MyData{
        get{
            if (CacheHelper.Exists("MyData") == false){
                lock (locker){

                    if (CacheHelper.Exists("MyData") == true){
                        myData=(List<SomeDataDto>)CacheHelper.GetCacheObject("MyData");
                    }
                    else
                    if (CacheHelper.Exists("MyData") == false){
                        var myData = GetSomethingFromDatabase("en", true);
                    CacheHelper.Add(myData, "MyData", 360);
                }
            }
            }
            return (List<SomeDataDto>)CacheHelper.GetCacheObject("MyData");
        }
    }

I'm calling this method on my page and this method throws error (sometimes!) 我在页面上调用此方法,并且此方法引发错误(有时!)

  public SomeObjectToReturn GetOneItem(string language, string id)
    {

        return MyData.Where(x => x.Language == language.ToUpper()).SelectMany(x => x.Something).SelectMany(x => x.SomethingElse).Where(x => x.ID == id).FirstOrDefault();
    }

I call this method 15 times for 15 different id-s but it works for 10 items or 8 items or something like that. 我为15个不同的id调用此方法15次,但它适用于10个项目或8个项目或类似的项目。 If I edit config to remove cache some other id-s are not working. 如果我编辑配置以删除缓存,则其他某些ID将无法正常工作。 So: 所以:

  1. First time it works id=6 第一次工作,id = 6
  2. Second time it doesn't work id=6 第二次不起作用id = 6

Is it cache corrupted? 缓存损坏了吗? Web Service does not return all elements in list? Web Service是否不返回列表中的所有元素? (too many of them?) (太多吗?)

You have to include your return (List)CacheHelper.GetCacheObject("MyData"); 您必须包含返回(List)CacheHelper.GetCacheObject(“ MyData”); in your lock like: 在您的锁中像:

    get{
        lock (locker){
           if (CacheHelper.Exists("MyData") == false){

                if (CacheHelper.Exists("MyData") == true){
                    myData=(List<SomeDataDto>)CacheHelper.GetCacheObject("MyData");
                }
                else
                if (CacheHelper.Exists("MyData") == false){
                    var myData = GetSomethingFromDatabase("en", true);
                CacheHelper.Add(myData, "MyData", 360);
            }
          }
         return (List<SomeDataDto>)CacheHelper.GetCacheObject("MyData");
       }
    }

If you read while you are writing or deleting the behavior could be different as you expect. 如果在编写或删除时阅读,则行为可能与您预期的不同。 The List object is not thread safe. List对象不是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM