0

I'm caching MyData to avoid unnecessary calls to web service but it seems there is some problem. 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.

     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. If I edit config to remove cache some other id-s are not working. So:

  1. First time it works id=6
  2. Second time it doesn't work id=6

Is it cache corrupted? Web Service does not return all elements in list? (too many of them?)

4
  • This way you cache the "pointer" to the data, but if you add or remove data, on this static "pointer" then you need again to use lock to synchronize it. So maybe there is your issue. Commented Jun 10, 2014 at 7:40
  • Without lock it is the same. I put lock because I thought it might help. Commented Jun 10, 2014 at 7:55
  • You need to lock, not the List<> but the insert/update/read when you access the List Commented Jun 10, 2014 at 8:46
  • you also set the 'myData` but you did not read and return that. Commented Jun 10, 2014 at 8:48

1 Answer 1

0

You have to include your return (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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.