Wednesday, December 18, 2013

Enable/disable output caching in SharePoint 2010 using PowerShell script

A SharePoint Publishing site can also be configured to make use of the ASP.NET output cache to store rendered ASPX pages in memory. You can only make use of this cache when the Publishing features are activated on your site collection. This means that only publishing pages can be cached.
When a Publishing page is first requested, SharePoint assembles the page dynamically: it first fetches the page layout from the content database and then fetches the contents of the page. The page layout comes from the content database; the data on the page can be data coming from the content database or files already cached in the BLOB cache. Once the page is assembled, the resulting HTML stream is stored in the output cache. On a next request the HTML is served from the cache: the controls are not created, the page life cycle doesn’t start, and no code-behind is executed. The only things that are not covered by the output cache, are images and other resources like JavaScript files and CSS files. These need to be handled by other caching mechanisms.
This way pages are served more quickly and CPU load is reduced on the web front-end servers and on the SQL Server content database because pages won’t have to be reassembled on each request. Output caching is considered as one of the most performance improving techniques.

Here a a link to original article about Output Caching.


Here is a PowerShell script to enable or disable on output caching in SharePoint 2010 site:

$TotalCount = 0;
$UpdatedCount = 0;
#Initiate array for output export
$Results=@();


function SetCacheSettingsOnSite($site)
{
       if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($site.RootWeb))
       {
              $cacheSettings = new-object Microsoft.SharePoint.Publishing.SiteCacheSettingsWriter($site.Url);
        if($disable -eq $true)
             {
                     if($cacheSettings.EnableCache -eq $true)
                     {
                           $cacheSettings.EnableCache = $false;
                           $cacheSettings.SetAnonymousPageCacheProfileId($site,1);
                           $cacheSettings.SetAuthenticatedPageCacheProfileId($site,1);
                           $cacheSettings.Update()
                           if($output -eq $true) {Write-Host "De-activated output cache on site $site"}
                           Set-Variable -Name UpdatedCount -Value ($UpdatedCount+1) -Scope 1;
                           $OutputLog = New-Object PSObject -Property @{SiteUrl=$site.Url;Status="Deactivated";Time=Get-Date}; Set-Variable -Name Results -Value ($Results+=$OutputLog) -Scope 1;
                     }
                     else
                     {
                           if($output -eq $true) {Write-Host "Output cache already de-actived on site $site"}
                           $OutputLog = New-Object PSObject -Property @{SiteUrl=$site.Url;Status="Skipped. Already De-Activated";Time=Get-Date}; Set-Variable -Name Results -Value ($Results+=$OutputLog) -Scope 1;
                     }
              }
              else
              {
             
                     if($cacheSettings.EnableCache -eq $false)
                     {
                           $cacheSettings.EnableCache = $true;     
                           $cacheSettings.EnableDebuggingOutput = $true;
                           $cacheSettings.SetAnonymousPageCacheProfileId($site,1);
                           $cacheSettings.SetAuthenticatedPageCacheProfileId($site,4);
                           $cacheSettings.SetFarmCacheFlushFlag();                      
                           $cacheSettings.Update();
                           if($output -eq $true) {Write-Host "Activated output cache on site $site"}
                           Set-Variable -Name UpdatedCount -Value ($UpdatedCount+1) -Scope 1;
                           $OutputLog = New-Object PSObject -Property @{SiteUrl=$site.Url;Status="Activated";Time=Get-Date}; Set-Variable -Name Results -Value ($Results+=$OutputLog) -Scope 1;
                     }
                     else
                     {
                           if($output -eq $true) {Write-Host "Output cache already active on site $site"}
                           $OutputLog = New-Object PSObject -Property @{SiteUrl=$site.Url;Status="Skipped. Already active";Time=Get-Date}; Set-Variable -Name Results -Value ($Results+=$OutputLog) -Scope 1;
                     }
              }
       }
       $site.Dispose();
}

$Results | select-object SiteUrl, Status, Time | Export-csv -path "CachingActivationResults.csv" -notype
Write-Host "Done. Updated:$UpdatedCount  Total Processed:$TotalCount"  -ForegroundColor Green

$rootSite.Dispose()


No comments:

Post a Comment