Content deleted Content added
No edit summary Tags: references removed Visual edit Mobile edit Mobile web edit |
mNo edit summary |
||
Line 47:
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 how to create a named cache, ''NamedCache2'', using the settings from a '''dataCacheClient''' section named ''customClient''.
<syntaxhighlight lang=
</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=
dataType GetUserData(string
{
dataType data = null;
// Attempt to retrieve the user data from the cache:
object dataObject = Cache.Get(
if (dataObject != null)
Line 66:
{
// If it doesn't exist in the cache, retrieve it from the database:
data = GetUserDataFromDatabase("SELECT * FROM users WHERE
// Put the returned data in the cache for future requests:
Cache.Add(
}
Line 78:
The following method shows how to update data that is already in the cache.
<syntaxhighlight lang=
void UpdateUserData(string
{
// Update the user information in the database:
result = UpdateUserDataInDatabase(
if (result)
{
// If successfully updated, update the cache:
Cache.Put(
}
}
Line 94:
The following call removes the item from the cache.
<syntaxhighlight lang=
Cache.Remove(
</syntaxhighlight>
|