Question

I have a Cache WorkerRole in Azure. I was planning to override the OnStart behavior of the role to pre-fill my cache and to use my own scheduler to fill the cache from within the worker role itself. This isn't scalable, I know this, but I don't need it to be.

So, my question is, is this even possible? I know I can override the OnStart, but how would I access the cache? From my other roles I would configure the web.config and just do:

DataCacheFactory cacheFactory = new DataCacheFactory();
DataCache cache = cacheFactory.GetDefaultCache();

But would changing the Web.config create any issues? Would I do this?

<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />

Or would I do:

<autoDiscover isEnabled="true" identifier="CacheRoleName" />

Anyone done this before and have any advice on how to set this up?

Était-ce utile?

La solution

I tried this real quick and it worked just fine by adding the Nuget package Microsoft.WindowsAzure.Caching to the Worker role hosting the dedicated cache. The NuGet package added the autoDiscover element above and I simply updated the identifier to be the name of the role. I overrode the OnStart and added the items to cache just as you would anywhere else (the lines you have in the question will get you access to the default cache).

I was able to read data out of the cache from a web role I had also added to the project.

A word of caution here though. You've already mentioned that the worker role on OnStart loading itself is a scaling concern, and it is somewhat. During initial deployment you can't guarantee the order of how the roles and instances will come online. This means that the consumers (web roles, etc.) of the cache may be available before the cache cluster is ready to go. They would need to know how to deal with getting empty values from the cache or not finding what they wanted. This is why the Cache Aside pattern is used heavily. In other words, don't make the mistake in believing that pre-filling the cache in this manner will ensure that data is present in the cache when requested. Any number of things could occur that would make that not be true. If the consumers of the cache don't know how to get the data from something other than the cache you may end up in a bad situation.

Also, if you have more than one instance of the dedicated cache worker they will attempt to run this code each time they start up or get recycled. Make sure these calls are idempotent and if you have values that will slowly change in your cache that you aren't inadvertently overriding them with "start up" values.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top