Introducing the SharePoint Cache Refresh Framework


 

Introduction

 

Although a SharePoint developer should try their best to use the tools that SharePoint provides inevitably the client will ask for something that needs some custom code! Over the last few years the customer has demanded that the code be fast, reliable and scalable.

When this happens one of the first things you start to think about is performance.  Hand in hand with performance is caching so that you can reduce the number of expensive operations.

However, when you cache data you get into scenarios where users make a change but they are not reflected immediately. The users then start complaining because the cache has become stale.

There are a few solutions:-

  • Restart IIS / OWS Timer Service to flush the cache (not practical)
  • Decrease the lifetime of the cache object (increase the number of expensive ops)
  • Use Cache Dependencies to flush the cache

The last option is the best as it supports options when data is being shared across multiple application domains however it doesn’t help when you have a number of servers in your SharePoint farm.

This is why I built the framework. The framework allows you to control the caching of objects across the SharePoint Farm.

The way it works is pretty straightforward and it comes with a small API to allow you to use it in your solutions.

 

Solution Overview

 

The framework is made of a few components:-

  • Cache Configuration SharePoint List to manage the configuration
  • Cache Configuration Refresh Timer Job to perform the cache update
  • Cache Configuration Manager – API to manage the Cache Refresh Configuration
  • Cache Refresh PowerShell Cmdlets to administrate the framework
  • Base Classes for Managing Configuration Objects

The way that the framework work is that an object is cached with a dependency to a particular file on the server’s file system. This uses .NET built in CacheDependencies which run a file watcher against the file. When the file being watched is modified a callback occurs and the cache item is removed from the HTTPRuntime.Cache.

The Cache Refresh Framework provides a method using SharePoint Timer Jobs to run a process which modifies these files across all SharePoint Servers in a farm. This flushes the object from the Cache on each of the servers in the farm so that next time they load the object it is loaded from the Data Access Layer / Storage Mechanism rather than the Cache.

The framework supports multiple cache profiles so that you can group objects to be refreshed together or target them differently.

The following section explains some usages and the process in more detail.

 

Usage

 

The framework can be used to allow caching of objects such as:-

  • Configuration items
  • Navigation items
  • XML elements
  • Plain Old C# Objects (POCO)
  • Data Transfer objects (DTOs)

A common example is:-

  • A user of the system makes a change to the configuration.
  • The SharePoint Application calls into the Cache Refresh Framework tell it to refresh a particular Cache Configuration
  • The Cache Refresh Framework gets the Cache Configuration and runs the timer job for that configuration on all servers in the SharePoint farm using the SharePoint Farm Timer Service.
  • Each server runs the actual job which modifies a file
  • The Cache Dependency picks up the change to the file and flushes the cache
  • The next time the application gets that object it get its from the data access layer rather than the cache.

SharePoint Cache Refresh Framework Diagram

 

 

Deployment, Configuration, How To Examples

 

The framework has been uploaded to Codeplex as the SharePoint Cache Refresh Framework Project and there are a number of guides which cover:-

These can be found in the Documentation Section

 

Finally

 

Currently the framework is an alpha release, so there might be some issues with it, so please don’t try it on your production environment without plenty of testing. There are some known issues and I will look to improve the framework and fix those issues as they are found. However, I am excited about getting the framework out there and getting feedback on it.

If you have any problems, or find bugs then report them on the SharePoint Cache Refresh Framework site using the Issue Tracker forum.

If you have any ideas for additional functionality then please add a Discussion item.

Finally, please let me know if you find it useful and how you have used it!

Thanks

Simon

Feature Upgrade Issue: Deleted SPWebs : An error occurred while enumerating through a collection


Introduction

Recently I published a post about issues when upgrading Site Scope features in SharePoint. The issue was related to deleted Site Collections which were still in the recycle bin.

After resolving this issue I decided to do some testing with other deleted SharePoint objects. The next logical object type to delete was an SPWeb object.

Unfortunately further issues were found and the following section explains how they were found and the approach to resolve them.

 

How the Issue was Approached

The approach taken was the following:-

  • Build a Web Scoped Feature marked v1.0.0.0
  • Install the SharePoint Solution
  • Create a number of Webs and Activate the feature on those webs
  • Delete one of the Webs
  • Update the Web Scoped Feature to v2.0.0.0
  • Upgrade the SharePoint Solution to deploy the new version of the Web Scoped Feature
  • Use Install-SPFeature [FeatureName] –Force to upgrade the feature
  • Run a PowerShell command to execute the SPSite.QueryFeatures() function

The following PowerShell was run to query for Features that need to be upgraded.

 $site=Get-SPSite http://sharepoint; $site.QueryFeatures("Web", $true); 

The result of the script displayed the following:-

 $site.QueryFeatures("Web",$true);  An error occurred while enumerating through a collection: Unable to access web scoped feature (Id: f41cc668-37e5-4743-b4a8-74d1db3fd8a4) because it references  a non-existent or broken web (Id: 3bd828cf-b747-4111-b888-5953b0d9aaa3) on sit e 'http://dev.itsp.local'.  Exception: System.ArgumentException: Value does not  fall within the expected range.    at Microsoft.SharePoint.SPWebCollection.get_Item(Guid id)    at Microsoft.SharePoint.SPFeatureEnumeratorBase.GetCachedWeb(SPSite site, Gu id webId, Guid featureId). At line:1 char:20 + $site.QueryFeatures >> ("Web",$true)     + CategoryInfo          : InvalidOperation: (Microsoft.Share...esultCollec    tion:SPFeatureQueryResultCollection) [], RuntimeException     + FullyQualifiedErrorId : BadEnumeration 

 

Solution

The approach to solving this issue was not to write a workaround but to run a script which checks for deleted SPWeb objects in the site collection.

 $site = Get-SPSite http://sharepoint; $site.RecycleBin | ?{$_.ItemType -eq "Web"}; 

The script will display the SharePoint Web Objects that have been deleted and are still found in the site collections recycle bin.

The deleted SPWeb objects can be removed from the recycle bin using the following PowerShell command.

 $site = Get-SPSite http://sharepoint;  $deletedWebs = $site.RecycleBin | ?{$_.ItemType -eq "Web"}; $deletedWebs | % {$site.RecycleBin.Delete($_.Id)} 

This will delete the SPWeb objects from the recycle bin and the Feature Upgrade process can continue.

Please be careful when running this script! Only run it when you know that none of the SharePoint Web sites are not needed!

 

Anyway hope that helps!