Developer’s guide understand caching


Published on

A developers and ITPros information about internal SharePoint 2010 caching capabilities.
Tips on how to make your SharePoint application run faster and scale up, and plan for caching.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • As systems become more dynamic and there is less static content, it takes a lot of resources to render even a simple page or a form.SharePoint stores most of the content it needs to serve up in the content database, so any time we can save a trip, we are helping the performance of our application.
  • A typical request to a SharePoint list view page looks like this:Go to DB get master page, go to DB get page, go do DB and read all web parts from page, merge master page with page, desterilize all web parts, run all controls code on page, list view web part – go to DB get list items, format them to html and JavaScript, write page into the response. For each CSS, JavaScript or Image that the page have, the client will make an additional request to server that will make a request to the DB that will return these files.
  • Is the data static (seldom changes), somewhat static (changes occasionally), or dynamic (changes constantly)?Is the data the same for all users, or does it change? For example, does it change depending on the user who is logged on, the part of the site being accessed, or the time of year (seasonal information)?Is the data public or does it require a higher level of security?Is the data easily accessible or does it require a long time to return the data? For example, is it returning from a long-running database query or from remote databases that can have some network latency in the data transfers?What is the size of the data?
  • * you can extend same web app to have one cached and one not* When first enabling it, performance will decrease while cache store is being built. After that performance will increase. Take that into account. This is done for each WFE separately.* Default 10GB, modify to have at least 20% more than the actual content you intend to cache.* Implemented using Microsoft.SharePoint.Publishing through PublishingHTTPModule. Does not require the publishing feature to be active, but will not work on SharePoint Foundation.* Files updates automatically in cache once changed on server. May take a short while to be updated.
  • Separate disk from the ULS log and other log services, or they will compete on disk usage and damage performance.SharePoint is optimized for speed, and will not cover up for you if you delete files manually!It’s a performance consideration, not a bug. Those files will be restored to cache when they are updated on the DB or when the BLOB cache is flushed.
  • Modify: enabled=“true”, maxSize to # of GB you want it to grow to, modify the regular expression filtering by file types you wish to cache.Optional parameter that is not in there by default: max-age=“86400”. This specify further caching expiration on the client browser to reduce the load even further, using client cache headers.BLOB Creates a sub folder based on your IIS site ID
  • Ghost files are files in the content data base, that by default do not have their binary content stored, instead they are linked to a local file that must be present on the file system (SharePoint Root folder) of every WFE server.When accessing a Ghost file, SharePoint goes to the DB to retrieve it, but instead of pulling up and receiving the binary content – the DB returns empty content to the WFE. This reduces the load from the DB and conserve a lot of network (ab)use. Then the WFE loads the file content from the local file system path provided for this file (as defined in the site template, feature etc…).* Unghosted files have far worst performance than ordinary files that were not ghosted originally.* Reghosting– you can use SharePoint Designer, which in turn clears the binary content information of the file, making it ghosted again. Note: reghosting files directly on the DB will work, but is not supported by Microsoft.Example: if you have 100 sites, all using the same master page size 200KB, this results in 20000KB of data in the DB. Ghosting it will only have 1 copy on each WFE and nothing in the content DB.
  • (If the file is to be added to a document library and you need full document library features on it such as check-in/out and version history, use GhostableInLibrary)
  • storing objects in memory on the WFE application poolIf your server is “rich on resources” increase the amount of memory your site can use for object cache, and consider increasing the “security tolerance” factor.“Security tolerance” (“Cross list query multiplier”), is basically telling SharePoint to retrieve more information than the current user has/requests in order to store it in the cache and be able to serve the same information later to more privileged users. This is very important when you are using a lot of unique list, folder and item permissions. 1-10 higher value for more unique permissions . a request is made to the object cache, it is executed 2 times – once as super user and once as super reader. Not as the current user. This is done to make sure we preserve draft view permissions, as a user may have permissions for an item but not to view it’s draft versions, in this case he will get the version stored by the super reader, otherwise he will get the super user version. SharePoint object cache checks the access control lists (ACLs) for the current user to determine which version of these files to return. This theory, allows the object cache to store only 2 versions of the data that will be able to serve all users.
  • On by default, 100 MB per site“security tolerance” factor (1 to 10)Admin can cap the total memory used in all site collections object cache under the web application in web.config file:<ObjectCachemaxSize="100" />Note: If you have more than 1 site collection, this default value is wrong for you, as each site’s default is 100MB. Consider changing this default value. I guess it was meant for publishing sites where they usually have only 1 site collection per web application.
  • By default super user is the site “System Account” and super reader is “Local Service”, Problem with these defaults are:* If the user uses the super user account to log into the site and ever checked out a file for example (some use the SharePoint System Account… ), the cache will store the checked out version and not the published version. This will make the cache double check and reload those items when ever a user try to run a query that uses this cache, thus eliminating the benefit of the object cache.* For super reader, the Local Service does not resolve correctly in claims authentication application. This means, it will not be able to run the super reader query. Features that explicitly try to use the object cache, in this situation, will receive “access denied” to all users regardless to the current user permissions. Such features OOB are: publishing infrastructure, CQWP, meta data navigation or navigation.
  • run against a list, if send true. Uses queryInfo.WebUrl to determine the web or list to run on.
  • Caches multiple versions based on profiles, permissions or custom logic (Cache profiles)Cached pages are sent “as is” with no server code running at all. Important when implementing server side “counter” control. Javascript counters will work, like Google analyticsWhen a page request is answered with a cached version: the server preserve many resources allowing it to serve more requests, faster. However, it increases the cost of WFE memory usage while almost eliminating CPU, IO, networking and data sources usage.
  • “Check for changes” option in cache profile to invalidate cached pages as soon as the original page is modified. This check happens on every request to a cached version and hurts the overall performance, but you can feel more free to extend the cache duration.set persistent sessions (or sticky sessions). Otherwise users may get inconsistent content while one WFE updated the cache and the other one did not. This may result in a confusing UI.check the “enable debug cache information on pages”. This writes a string in the HTML (view source only) with additional cache information such as the date and time and the profile that this cache version was build.
  • - Set useCacheProfileOverrides=“true” to enforce caching to be used on all site collections-Set varyByRights=“false” to remove the requirement that users must have identical effective permissions on all securable objects to see the same cached page as any other user. This is useful when you break permissions on an assets library (like when implementing you tube style site), but content pages are under the same permissions.- Set cacheForEditRights=“true” to prevent people with edit rights from seeing cached pages.
  • set to “browser” to identify major browser versions such as IE6/7/8/9, Netscape 4, etc. useful when servicing different CSS / Script for different browsers.
  • Builds a “cache key” string based on any custom conditions for each request and returns it to the output cache manager. Each string will have its own cached version.
  • Cache version number: build a version number that represents you current data. It could be the “time last modified” of something that will always change when your source is updated, or an auto incremented number in your remote DB.When accessing the cache, query the current version and compare with your cached version to know if you need to invalidate your cache.
  • Developer’s guide understand caching

    1. 1. Developer’s guideHow to enhance your SPperformance: Understand caching
    2. 2. Shai PetelVP Research and DevelopmentMCPD, MCT, MVP: | @shaibs |
    3. 3. TopicsWhy do we need cache?Understand CachingOther performance coding tips
    4. 4. TopicsWhy do we need cache?Understand CachingOther performance coding tips
    5. 5. SharePoint performance challengePages• Dynamic list forms• Dynamic pages and page layoutsContent• Dynamic content structure• Extensible (field types)• Stored in Content DBCustomizations• Custom UI elements (Web parts, user controls, field controls)• Other API plugins (event handlers, workflows)
    6. 6. SharePoint Page Processing
    7. 7. What can be done better? Minimizing round trips to DB on every call Reduce ACL rules Pre-compiling user controls and pages Pushing supporting files from DB to each WFE Saving the output HTML of pages that do not change frequently Reusing DB connections and requests when possible
    8. 8. What does SharePoint already do? Ghost files on WFE Caches our .NET code in native code (If put in GAC) Pre-compiles user controls Shares context objects (SPContext.Current Site, Web, List) with all controls for a single request Apply some sort of object caching by default (good example is BCS)
    9. 9. When is caching good for me?Information updatesSecurity Views & RiskSourcesSize
    10. 10. TopicsWhy do we need cache?Understand CachingOther performance coding tips
    11. 11. Understand CachingBLOB Cache Ghosting files Object Cache Page output Custom cache cache profiles
    12. 12. BLOB cache What is? WFE local disk-based cache One per web application Reduce database load and network traffic Limit by file types Built on demand Configure cache size PublishingHTTPModule Only document library Security trimmed
    13. 13. BLOB cache When to use? Ideal for publishing with many anonymous visitors. Sites that contain lots of media assets that are read-only or rarely changes. Support site that has PDF and Doc user manuals for download Collaboration sites for creative team– don’t cache images For super heavy use, store BLOB on a separate hard drive DO NOT delete files directly from BLOB cache storage!
    14. 14. BLOB cache How to configure? Edit web.config, “<BlobCache … />” existing tag <BlobCache location=“D:BLOBWebApp1" path=".(gif|jpg|jpeg|jpe|jfif|bmp|dib|tif| tiff|ico|png|wdp|hdp|css|js|asf|avi|flv|m4v|mov|mp3|mp4|mpeg|mpg|rm|rmvb|wma|wmv)$" maxSize="10" enabled=“true" /> Flush BLOB cache PoSH: $webApp = Get-SPWebApplication “http://mywebapp:port“ [Microsoft.SharePoint.Publishing.PublishingCache]::FlushBlobCache($ webApp) Write-Host "Flushed the BLOB cache for:" $webApp STSADM: stsadm –o setproperty –propertyname blobcacheflushcount –propertyvalue 11 –url http://mywebapp::port Microsoft.SharePoint.Publishing.PublishingCache.FlushBlobCache( C#: Microsoft.SharePoint.Administration.SPWebApplication.Lookup( new Uri("http://mywebapp:port") ) );
    15. 15. Ghosting files What Is? No content in content DB table Reduces load and (ab)use of DB and network Ghost files != BLOB cache, yet similar Updates only need to be done to local template file When modifying a ghost file, only the differences will be stored (unghost) Reset back to definition (reghost) is possible Saves on expensive DB storage
    16. 16. Ghosting files When to use? Template files that do not change much, such as master pages, page layouts, forms, images, CSS files, JS files, ASPX files. Recommended to use whenever you deploy files using a feature. If you expect a lot of site-level customizations of the file, do not mark it as ghost.
    17. 17. Ghosting files How to configure? Include the files you need in a module element <File Path="SiteAssetsProjectFolderActionPage.aspx" Url="SiteAssets/ProjectFolder/ActionPage.aspx" Type="Ghostable" /> Set Type to Ghostable / GhostableInLibrary
    18. 18. Object cache What is? Reduces traffic between WFE and DB Stores lists, libraries, site settings and page layouts, as well as cross list queries Cost memory on WFE “Security tolerance” (“Cross list query multiplier”) Query DB 2 times! Portal super user and super reader Some API objects and OOB controls are cached automatically by using the standard API, but there is more! 
    19. 19. Object Cache When to use? When performance is important, memory is cheap and minor data inconsistency is acceptable* When your data does not update often* You can enhance performance even more by settingobject cache not to check for changes on every query
    20. 20. Object Cache How to configure? Requires “SharePoint Server Publishing” feature Site collection level setting, web application memory cap Set memory limit, duration, and “security tolerance” Flush object cache is done via site collection administration web UI.
    21. 21. Object Cache How to configure?Configure Super User (must have full control) and Super Reader (must have full read access)Default users are wrong!
    22. 22. Object Cache How to configure?Map users to service: Create users in central admin Next, use PoSH to specify super user and super reader account names: $wa = Get-SPWebApplication -Identity "<WebApplication>“ $wa.Properties["portalsuperuseraccount"] = "<SuperUser>“ $wa.Properties["portalsuperreaderaccount"] = "<SuperReader>“ $wa.Update() Perform IIS Reset to complete the operation.
    23. 23. Object CacheHow to configure?
    24. 24. Object cache How to use? Add using to Microsoft.SharePoint.Publishing Run the following code using (SPSite site = new SPSite("http://mywebapp:port")) { CrossListQueryInfo crossListQueryInfo = new CrossListQueryInfo(); crossListQueryInfo.Lists = "<Lists BaseType="5" />"; //Load all items that have "important" in their title crossListQueryInfo.Query = "<Where><Contains><FieldRef Name="Title" /><Value Type="Text">important</Value></Contains></Where>"; crossListQueryInfo.RowLimit = 100; crossListQueryInfo.UseCache = true; crossListQueryInfo.ViewFields = "<FieldRef Name="Title" />"; crossListQueryInfo.Webs = "<Webs Scope="SiteCollection" />"; crossListQueryInfo.WebUrl = site.ServerRelativeUrl; CrossListQueryCache crossListQueryCache = new CrossListQueryCache(crossListQueryInfo); SiteDataResults results = crossListQueryCache.GetSiteDataResults(site, false); }
    25. 25. Page output cache What is? Native ASP.NET technology Caches the HTML output of ASPX page Caches multiple versions When a page request is answered with a cached version:  No calls to any data source  No load/merge master page, page layout, field and web controls  Does not run any server side code Cache profiles  A more precise and customized output cache mechanism on top of ASP.NET  A list-style set of rules that can be applied to page layouts, site, site collection
    26. 26. Page output cache When to use? To be used on content pages (publishing / wiki) When you have few contributors and a lot of readers / visitors When pages do not change frequently If you plan to have many requests for the same pages (landing page)
    27. 27. Page output cache How to configure? Requires “SharePoint Server Publishing” feature “Check for changes” option in cache profile NLB - set persistent sessions (or sticky sessions) Monitoring cache behaviour: check the “enable debug cache information on pages”
    28. 28. Page output cache How to configure? Configure on web application (in web.config) <OutputCacheProfiles useCacheProfileOverrides="false" varyByHeader="" varyByParam="*" varyByCustom="" varyByRights="true" cacheForEditRights="false" /> Configure at site collection, allow publishing sub-sites & page layouts to override
    29. 29. Page output cache How to configure?Sitecollectionsettings
    30. 30. Page output cache How to configure?Sub site settings:
    31. 31. Page output cache How to configure?Page layout settings:
    32. 32. Page output cache How to configure?Create new cache profiles Use “Vary by X” to enable this same profile to store different versions of the cache based on specific criteria: Header: by language for example Query string parameter: http://site/page.aspx?ID=1 User rights: based on the ACL evaluation of each user. Users with same permissions will get the same cached content. Custom: “browser”, or custom settings (code)
    33. 33. Page output cache How to configure?New cacheprofile:
    34. 34. Page output cache How to configure?ASP.NET cacheability:Member name DescriptionNoCache Sets the Cache-Control: no-cache header. Without a field name, the directive applies to the entire request and a shared (proxy server) cache must force a successful revalidation with the origin Web server before satisfying the request. With a field name, the directive applies only to the named field; the rest of the response may be supplied from a shared cache.Private Default value. Sets Cache-Control: private to specify that the response is cacheable only on the client and not by shared (proxy server) caches.Public Sets Cache-Control: public to specify that the response is cacheable by clients and shared (proxy) caches.Server Specifies that the response is cached only at the origin server. Similar to the NoCache option. Clients receive a Cache-Control: no-cache directive but the document is cached on the origin server. Equivalent to ServerAndNoCache.ServerAndNoCache Applies the settings of both Server and NoCache to indicate that the content is cached at the server but all others are explicitly denied the ability to cache the response.ServerAndPrivate Indicates that the response is cached at the server and at the client but nowhere else. Proxy servers are not allowed to cache the response.
    35. 35. Custom cache profiles What is?A custom class that inherits from IVaryByCustomHandlerDeployed to GAC and registered in global.asaxBuilds a “cache key” string
    36. 36. Custom cache profiles When to use?When you want to use output cache, butyour application renders different HTMLbased on custom rules, like:User profile association (Like: HR info, VaryByLocation, VaryByLastName)Device/Environment rules (like: allow scripts? Allow CSS?)
    37. 37. Custom cache profiles How to configure? Register the event handler in global.asax file://Register the VaryByCustom string in the Global.asax file.<%@ Assembly Name="Microsoft.SharePoint"%><%@ Assembly Name=“CacheExample.VaryByCustomExample"%><%@ Application Language="C#" Inherits="CacheExample.VaryByCustomExample" %> Pass string parameters to “vary by custom parameter” to trigger your handler (Optional)
    38. 38. Custom cache profiles How to use?Example, Vary by CSS availability, support XMLHttp, and is user site admin. Parameter(s) to beset in profile “Vary by custom parameter”: “SupportsCSS;SupportsXmlHttp;SiteAdmin”using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.SharePoint.ApplicationRuntime;using System.Web;using Microsoft.SharePoint;namespace CacheExamples{ public class VaryByCustomExample : SPHttpApplication, IVaryByCustomHandler { … }}
    39. 39. Custom cache profiles How to use?//supported strings in "Vary by custom string" tolook for in order for our logic to kick inpublic enum SupportedParams { SupportsCSS,SupportsXmlHttp, SiteAdmin }public override void Init(){ base.Init(); this.RegisterGetVaryByCustomStringHandler( (Microsoft.SharePoint.ApplicationRuntime. IVaryByCustomHandler)this );}
    40. 40. public string GetVaryByCustomString(HttpApplication app, HttpContextctx, string custom) { //look for parameters specified in the cache profile StringBuilder sb = new StringBuilder(); string[] strings = custom.Split(;); foreach (string str in strings) { SupportedParams param; if (!Enum.TryParse<SupportedParams>(str, out param)) continue;//Not in our enum... Skip switch (param) { case SupportedParams.SupportsCSS: sb.Append(ctx.Request.Browser.SupportsCss + ";"); break; case SupportedParams.SupportsXmlHttp: sb.Append(ctx.Request.Browser.SupportsXmlHttp + ";"); break; case SupportedParams.SiteAdmin: sb.Append(SPContext.Current .Web.UserIsSiteAdmin.ToString() + ";"); break; default: continue; } } return sb.ToString().TrimEnd(;);}
    41. 41. TopicsWhy do we need cache?Understand CachingOther performance coding tips
    42. 42. Other performance coding tipsSPList.Items performance trap Poor Performing Methods and Better Performing Alternatives Properties SPList.Items.Count SPList.ItemCount Create an SPQuery object to retrieve SPList.Items.XmlDataSchema only the items you want. SPList.GetItemByUniqueId(System.Guid SPList.Items[System.Guid] ) SPList.Items[System.Int32] SPList.GetItemById(System.Int32) SPList.Items.GetItemById(System.Int32) SPList.GetItemById(System.Int32) SPFolder.Files.Count SPFolder.ItemCount
    43. 43. Other performance coding tipsPaginate large list queriesSPWeb oWebsite = SPContext.Current.Web;SPList oList = oWebsite.Lists["Announcements"];SPQuery oQuery = new SPQuery();oQuery.RowLimit = 10; //Limit # of items to under 2000int intIndex = 1;do { Response.Write("<BR>Page: " + intIndex + "<BR>"); SPListItemCollection collListItems = oList.GetItems(oQuery); foreach (SPListItem oListItem in collListItems) Response.Write(oListItem["Title"].ToString() +"<BR>"); //Move to next page, same query – this is all that changes. oQuery.ListItemCollectionPosition = collListItems.ListItemCollectionPosition; intIndex++;} while (oQuery.ListItemCollectionPosition != null);
    44. 44. Other performance coding tipsQuery Throttling List items view threshold, for users and for site administrators Developers override (for privileged users) us/library/microsoft.sharepoint.spquerythrottleoption(v=office.14).aspx “Happy hour” Limit # of lookup, person, or workflow status fields that can be included in a single database query
    45. 45. Other performance coding tipsColumn indexing can help! Query throttling limits the number of DB rows that are affected Query that filters or sorts on a non-indexed column affects all list items, even if only 10 items return By indexing that column, only 10 rows in the DB will be affected Indexing cost resources
    46. 46. Other performance coding tips Cache objects in memory Use Page.Cache, per WFE Only cache thread safe object SPWeb, SPSite, SPListItemCollection – No! SPListItemCollection.GetDataTable() – Yes! Consider security when caching Poor man’s cache dependency – use cache version number to invalidate your data once updated.
    47. 47. Other performance coding tipsMulti-threaded, use lockprivate static object _lock = new object();public void CacheData() { SPListItemCollection oListItems; oListItems = (SPListItemCollection)Cache["ListItemCacheName"]; if(oListItems == null) { lock (_lock) { //Ensure the data not loaded by a concurrent thread while waiting for lock. oListItems = (SPListItemCollection)Cache*“ListItemCacheName”+; if (oListItems == null) { oListItems = DoQueryToReturnItems(); Cache.Add("ListItemCacheName", oListItems, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove); } } }}
    48. 48. Other performance coding tipsCache on client Use cookie to store settings instead of reading them every time Use view state to store data between postbacks Override SaveViewState/LoadViewState to “zip” large data or “encrypt” sensitive information (I reduced 8MB viewstate to 65KB)
    49. 49. Other performance coding tips Use lazy loading Use scripting on demand<SharePoint:ScriptLink ID="scriptLink1" runat="server" Name="My_Script.js"LoadAfterUI="true" OnDemand="false" />Not on demand:<script type="text/javascript">// <![CDATA[document.write(<script type="text/javascript" src="/_layouts/my_script.js"></ +script>);// ]]></script>On demand:<script type="text/javascript">RegisterSod("my_script.js","u002f_layoutsu002fmy_script.js");</script>
    50. 50. Other performance coding tipsWork with script on demand SP.SOD.registerSod(key,url); To call function from this script, use: SP.SOD.executeFunc(key, functionName, fn); Script dependency: SP.SOD.registerSodDep(key, dependOnKey);
    51. 51. Other performance coding tips Make sure your code runs after all dependency scripts finished loading: SP.SOD.executeOrDelayUntilScriptLoaded(func, scriptName) In your script files, notify when your classes finished registring: SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs(" my_script.js");
    52. 52. Other performance coding tipsEnable Bit Rate Throttling If you are steaming a lot of large media files Limit rate those files are delivered to client to conserve network bandwidth A part of IIS Media Services WMV, MP4, SWF files bit rates are detected automatically Good for “you tube” type of solution
    53. 53. TopicsWhy do we need cache?Understand CachingOther performance coding tips
    54. 54. See also Documentation   * except for “BLOB” sub page   us/library/bb687949(office.12).aspx  Sean McDonough on BLOB Cache deeper-into-the-sound-as-the-flush-comes/
    55. 55. Questions?=2B |! 2B ? Cache : Live; ?