Best practices for using the Azure AppFabric Caching Service?

I have successfully started using the Azure AppFabric Caching Service, but I'm not sure what the best way to create a DataCacheFactory object is. Now I create it for every cache call, but apparently this is not an ideal way to do this ...

Some recommend calling Singleton. But I'm not sure I understand how this will be implemented (not the actual Singleton class, but how to tie it all together).

Today I have a CacheProvider class that is created for me using Ninject, where I can perform Get / Put / Remove operations. For each of these methods, I create a DataCacheFactory object and then call .GetDefaultCache () to get a DataCache object, where I call Put / Get / Remove, respectively. I do this with one method, which looks like this:

private T Cache<T>(Func<DataCache, T> cacheAction) { using (DataCacheFactory dataCacheFactory = new DataCacheFactory()) { DataCache dataCache = dataCacheFactory.GetDefaultCache(); return cacheAction(dataCache); } } 

Now I'm sure this is not a very smart idea, instead I should get the DataCache object via Singleton, where the DataCacheFactory object is created only once. BUT, how will this object survive between requests? And how does it work s> 1 instance on Azure?

Hope this all makes sense and that someone with more experience than me (3 hours) can help me.

+4
source share
1 answer

Singleton objects live in the application area. Just declare your private DataCache object at the global static level, provide a Get property that creates an instance of the object if it is null or returns an object if it is not null.

Thus, you will pay the cost of configuring and connecting the cache only once for each application.

Each instance will create an instance of its own DataCache object, and this is normal.

The actual data in which the cache is stored is not stored in your local computer memory, but in the dedicated Azure cache servers, which are distributed mega-fast.

+6
source

All Articles