Content deleted Content added
m →Related Caching Technologies: Corrected table to read "Windows Azure Shared Caching" for last row and modified that description slightly. |
m Changed code samples to straight C# instead of pseudocode and improved the description. |
||
Line 44:
</syntaxhighlight>
===Code
''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
<syntaxhighlight lang=CSharp>
DataCache
</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>
/* 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>
/* 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);
/*
Cache.Put(userid, data);
}
|