0

I created some static utility methods that I use for caching objects.

public static class CacheProductView
{
    static object _lock = new object();
    static string _key = "product-view";

    public static IEnumerable<Product> Select()
    {
        var obj = CacheObject;

        if (obj == null)
        {
            lock (_lock)
            {
                obj = CacheObject;

                if (obj == null)
                {
                    obj = CreateCacheObject();
                }
            }
        }
    }

}

Here's a snippet of code from the method I use. As you can see, I use the classic .Net caching pattern, however I have a question regarding static variables inside static classes.

Is the static variable unique within the static class? For instance, if I clone the class and replace 'Product' for 'Order', will the _lock and _key objects, be scoped to the class or the application. Obviously, if the answer is the latter, giving unique names would be be needed.

All help and advice appreciated.

dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
  • Static is a bad idea in ASP.NET, why do you want that? If you really need them use the `Cache`, but even then you can wait for inconsistent data, the chances are you are running into synchronization issues when using the cache. – Tim Schmelter Mar 17 '15 at 11:33
  • How else would I lock this cache object? – dotnetnoob Mar 17 '15 at 11:37
  • 1
    If you lock your static variables or the cache you will make your ASP.NET application a single threaded app. If it's possible that the data changes i would use the database instead of using the cache (or static variables). – Tim Schmelter Mar 17 '15 at 11:38
  • I understand what you're saying Tim but its only locked whilst retrieving the data, which would be the same case if I was getting it from the database each time. – dotnetnoob Mar 17 '15 at 11:40

1 Answers1

0

Answering your question - static fields and properties are defined per type.

Great article about what goes where in types and instances
http://www.codeproject.com/Articles/20481/NET-Type-Internals-From-a-Microsoft-CLR-Perspecti

Besides that - pattern you present is not a good way to go.

Random
  • 4,519
  • 2
  • 38
  • 46
  • Thanks for your advice Random. What would you advise as a better approach? – dotnetnoob Mar 17 '15 at 11:55
  • I think caching approaches are rather vast subject and I would go with own caching implementation only when I'm seriously not happy with what frameworks have to offer. I don't know what you want to cache, how often you want to access cached data or where cached data is coming from in the first place. Maybe there is something what already supports your scenario. Static data in the Web tier is a bad small in the first place (at least to me), I try to keep my web apps as stateless as possible. – Random Mar 17 '15 at 12:08
  • Thanks Random. I understand why you take that approach - but I'll give you some background. The data i'm retrieving is via Enity Framework, so data is widely distributed amongst tables. However, in all there are about 100 products that rarely change (and by that I mean monthly, not daily or hourly). I'm tyrying to make the site smarter - there are many widget type objects that pull back snippets of data - visited, similar, slternatives etc. I don't want to go to the database x times per page to retreive it when it never changes. – dotnetnoob Mar 17 '15 at 12:16
  • @dotnetnoob What you describe should be quite common, right? :) https://msdn.microsoft.com/en-us/magazine/hh394143.aspx https://msdn.microsoft.com/en-us/data/hh949853.aspx – Random Mar 17 '15 at 22:24