Enabling BLOB Cache in SharePoint using PowerShell

One of the ways of improving performance of your SharePoint is by enabling the BLOB Cache. The BLOB cache is a disk-based cache that stores binary large objects (BLOB’s) on the web server to reduce the load on the database server by avoiding unneccessary round trips to that server. Read all about it on TechNet.

The BLOB Cache is disabled by default and enabling it needs to be done on the web application level in the web.config. There’s a specific setting in the web.config where you can enable this.

<BlobCache location="C:\BlobCache\15" path="\.(gif|jpg|jpeg|jpe|jfif|bmp|dib|tif|tiff|themedbmp|themedcss|themedgif|themedjpg|themedpng|ico|png|wdp|hdp|css|js|asf|avi|flv|m4v|mov|mp3|mp4|mpeg|mpg|rm|rmvb|wma|wmv|ogg|ogv|oga|webm|xap)$" maxSize="10" enabled="false" />

To enable it, you just change the value of the “enabled” attribute to true and there… it’s done.

But wait, if you have multiple web servers, you need to do this on each server. Not quite amusing. Let’s do something about that. If you use a SPWebConfigModification, the change is pushed automatically to all web servers which have the WFE role. Time to flex those fingers and create some PowerShell code.

    Enables and configures the SharePoint BLOB Cache. 

    Enables and configures the SharePoint BLOB Cache. 

    File Name: Enable-BlobCache.ps1
    Author   : Bart Kuppens
    Version  : 2.0

    Specifies the URL of the Web Application for which the BLOB cache should be enabled. 

    Specifies the location of the BLOB Cache. 	 

    PS > .\Enable-BlobCache.ps1 -Url http://intranet.westeros.local -Location d:\BlobCache\Intranet

   This script enables the BLOB cache for the http://intranet.westeros.local web application and stores
   it under d:\blobcache\intranet
   [Parameter(Mandatory=$true, ValueFromPipeline=$false, Position=0)] 
   [Parameter(Mandatory=$true, ValueFromPipeline=$false, Position=1)] 

# Load the SharePoint PowerShell snapin if needed
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
   Add-PSSnapin Microsoft.SharePoint.PowerShell

$webApp = Get-SPWebApplication $Url
$modifications = $webApp.WebConfigModifications | ? { $_.Owner -eq "BlobCacheMod" }
if ($modifications.Count -ne $null -and $modifications.Count -gt 0)
   Write-Host -ForegroundColor Yellow "Modifications have already been added!"

# Enable Blob cache
[Microsoft.SharePoint.Administration.SPWebConfigModification] $config1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
$config1.Path = "configuration/SharePoint/BlobCache" 
$config1.Name = "enabled"
$config1.Value = "true"
$config1.Sequence = 0
$config1.Owner = "BlobCacheMod" 
$config1.Type = 1 

# add max-age attribute
[Microsoft.SharePoint.Administration.SPWebConfigModification] $config2 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
$config2.Path = "configuration/SharePoint/BlobCache" 
$config2.Name = "max-age"
$config2.Value = "86400"
$config2.Sequence = 0
$config2.Owner = "BlobCacheMod" 
$config2.Type = 1 

# Set the location
[Microsoft.SharePoint.Administration.SPWebConfigModification] $config3 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
$config3.Path = "configuration/SharePoint/BlobCache" 
$config3.Name = "location"
$config3.Value = $Location
$config3.Sequence = 0
$config3.Owner = "BlobCacheMod" 
$config3.Type = 1

#Add mods to webapp and apply to web.config

This script accepts 2 parameters:

  • Url : URL of the web application for which you want to enable the BLOBCache
  • Location : the place on disk where the cache is stored

In the script, 3 updates are performed:

  • Enabled=”true” : Enables the BLOB Cache
  • max-age=”86400″ : Sets the age (in seconds) that a browser caches BLOBs
  • location : Sets the path on disk where the cache is stored

By Bart

Bart is a certified SharePoint consultant / architect at CTG Belgium NV with a broad professional experience in IT, a background in software development with a specialisation in Microsoft products and technologies and a solid knowledge and experience in Microsoft SharePoint Products and Technologies. He started as a COBOL developer on a mainframe environment and grew into software development for Windows platforms. Participated in projects varying from migrations of existing applications to development of Web applications and Windows applications. Became fascinated by the SharePoint 2007 platform and strongly believed in the added business value of this platform. Is since then fully committed to SharePoint and focuses on SharePoint implementations, migrations, integrations, design and coaching. Stays on top of new developments within the SharePoint technology stack and related technologies.

%d bloggers like this: