Windows Azure Caching: Difference between revisions

Content deleted Content added
Rothja (talk | contribs)
m Related Caching Technologies: Corrected table to read "Windows Azure Shared Caching" for last row and modified that description slightly.
Rothja (talk | contribs)
m Changed code samples to straight C# instead of pseudocode and improved the description.
Line 44:
</syntaxhighlight>
 
===Code ExampleExamples===
''Note that the code samples in this section are shown in [[C_Sharp_(programming_language)|C#]].''
When hosting Caching on roles, the DataCache constructor can be used to specify both the named cache and the dataCacheClient section for the cache client settings. The following code shows these variations.
 
When hosting Caching on roles, the '''DataCache''' class constructor can be used to specify both the named cache and the '''dataCacheClient''' section for the cache client settings. The following code shows thesehow to create a named cache, ''NamedCache2'', using the settings from a '''dataCacheClient''' section named variations''customClient''.
 
<syntaxhighlight lang=CSharp>
DataCache cacheCache = new DataCache("NamedCache2", "customClient");
cache = new DataCache("default");
cache = new DataCache("NamedCache1", "default");
cache = new DataCache("NamedCache2", "customClient");
</syntaxhighlight>
 
The following method shows how to use the ''Cache'' object to retrieve data from the cache. In this example, a user identifier (''userid'') is the key for the associated user information object. The code first attempts to get this user information from the cache using the ''userid'' key. If that does not succeed, the code retrieves the information with a database query and then stores the returned user data in the cache. The next time the same code is run, the user information will be returned from the cache rather than the database. This assumes that the cached data has not been expired or evicted.
 
<syntaxhighlight lang=CSharp>
functiondataType get_fooget_user_data(intstring userid) {
/* first try the cache */
data = (dataType)cache.Get(userid) ;
Line 70 ⟶ 69:
</syntaxhighlight>
 
The following method shows how to update data that is already in the cache.
 
<syntaxhighlight lang=CSharp>
functionvoid update_fooupdate_user_data(intstring userid, string dbUpdateString) {
/* first update database */
result = db_execute(dbUpdateString);
Line 79 ⟶ 78:
/* database update successful : fetch data to be stored in cache */
data = db_select("SELECT * FROM users WHERE userid = ?", userid);
/* thethen previousstore linein couldcache alsoto looksupport likefuture dataget = createDataFromDBString(dbUpdateString);calls */
/* then store in cache until next get */
Cache.Put(userid, data);
}