16.01.2013 Views

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 37: Using Visual Studio .NET to Create Web Parts 1021<br />

Instead you can use the Web Part cache to store data to enhance performance.<br />

The Web Part cache can be used in different modes: None, Database, <strong>and</strong><br />

CacheObject. The None mode disables caching. In the Database mode, all data is<br />

stored in the SharePoint database. In this mode, Shared <strong>and</strong> Personal cache entries are<br />

limited to 2 MB. If this limit is exceeded, the cache entry will be lost, <strong>and</strong> no error will<br />

be displayed. Personal storage data will be charged against the user’s quota. The final<br />

Web Part cache mode, the CacheObjectMode, uses the ASP.NET cache object to store<br />

data. This is also the default mode of the Web Part cache object. Only administrators—<br />

not developers—can set the cache type using the element in the<br />

section of the Web.config file, like this:<br />

<br />

..<br />

<br />

<br />

You can use the CacheType property of the WebPart class to determine the<br />

type of caching used by the Web server. This property is read-only, so it cannot be<br />

used to change the type of caching. Developers can retrieve the cache type at run<br />

time using the following code:<br />

if ( CacheType == CacheType.None )<br />

{<br />

output.Write("Caching is disabled.");<br />

}<br />

else<br />

{<br />

output.Write("Caching is enabled.");<br />

}<br />

The PartCacheWrite method of the WebPart class stores a value in the Web Part<br />

cache. You need to provide this method with a storage type (None, Personal, or<br />

Shared). You also need to provide a key-value pair <strong>and</strong> determine how long you<br />

want to keep a value stored in the Web Part cache. The following code example<br />

shows how to write data to the Web Part cache:<br />

PartCacheWrite(Storage.Shared, "mykey", "myvalue", TimeSpan.FromMinutes(5));<br />

The PartCacheRead method of the WebPart class can be used to retrieve values<br />

from the Web Part cache again. This is shown in the following code example:<br />

String strValue = PartCacheRead(Storage.Shared, "mykey").ToString();<br />

The PartCacheInvalidate method of the WebPart class can be used to outdate data in<br />

the Web Part cache. You can either outdate all contents of the Web Part cache, outdate<br />

contents of a specific storage type, or mark only a specified cache value of a<br />

specified storage type as outdated. The following code example shows how to outdate<br />

a specific cache value <strong>and</strong> storage type:<br />

PartCacheInvalidate(Storage.Shared, "mykey");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!