Getting started

Review the Caching Overview and Concepts here.

When creating new applications or functionality on top of the Telligent Evolution platform, you'll need to consider caching for performance. Rather than using the ASP.net caching object, utilize the Telligent Evolution caching framework so that runtime adjustments can be made without compilation, and you have finer control over where you are caching your items. The Telligent.Evolution.Extensibility.Caching namespace provides an API for adding caching abilities to an Evolution component.

Learn more about the CacheService class to start utilizing Telligent's caching framework in your component.

Best practices

Keys and tags

All items in the cache will have an associated key that can be used to identify it. Keys are not case-sensitive and will always be stored in their lowered-case form.

In addition to a key, you can further identify an item by placing one or more tags on it. Like keys, tags are not case-sensitive and will be stored in their lowered-cased form.

Tags are useful when grouping data into logical partitions. Using tags, you can expire a group of items in the same logical partition without knowing all the individual keys. While tags are powerful in this regard, they should never be used in substitution of a good key generating algorithm. Tag operations are very slow in comparison to single key lookups and removals, so be very selective about how and when you use them.

Choosing scope

All cache operations require one or more CacheScope values. When defining a cache layer (see: Caching Overview), each layer will identify what kind of cache it is by using a cache scope.

For example, the built-in ASP.NET cache identifies itself as a Process scope layer because it stores items in locations that are available to all threads running inside the application. The AppFabric layer uses a Distributed scope because it stores items in a location that many applications can access.

Before caching an item, you'll need to decide what scope(s) to operate in. Here are some general guidelines that you may follow:

  • Context - The Context scope is typically backed by the HttpContext object. This means that items put into the Context scope are only cached for the duration of a single web-request. When choosing a scope, you will almost always specify this value.

  • Process - The Process scope stores items in the ASP.NET cache object. All threads that can access the same ASP.NET cache object instance can share cached data inside that instance. This cache is also known as the local Web cache and it used extensively inside the Evolution platform.

  • Distributed - The Distributed scope stores items using Microsoft's AppFabric server. Items cached in distributed scope are available to all applications that can reach the AppFabric server. This cache is the slowest of all cache because all objects must be serialized on PUT actions and deserialized on GET actions. Additionally, tag operations like RemoveByTag are orders of magnitude slower in distributed scope. As a general rule, if you plan on utilizing RemoveByTags inside of distributed scope, consider only using local (Process) cache and Context cache.

Timeouts

When placing an item into cache, a timeout value is given to say how long that item can be cached for.

Choosing a good timeout value can be tricky. In many instances, the value we pick will be optimal for some environments and not optimal for others. If you find yourself in a scenario where you can't decide what timeout you should use, you can defer the decision and allow the cache system to decide based on the default value for the site or application.

To learn more about default timeouts, see default timeouts.

Examples and demonstrations

All of the following examples assume we have this defined:

using Telligent.Evolution.Extensibility.Caching.Version1;

Example 1:  Retrieving an item from cache.

var obj = CacheService.Get("key", CacheScope.Context | CacheScope.Process);

Example 2: Putting an object into cache.

// Get the object from the source.
var obj = GetObjectFromDataSource();

CacheService.Put("key", obj, CacheScope.Context | CacheScope.Process, TimeSpan.FromMinutes(1));

Example 3: Putting an object into cache using the default timeout.

// Get the object from the source.
var obj = GetObjectFromDataSource();

CacheService.Put("key", obj, CacheScope.Context | CacheScope.Process);

Example 4: Putting an object into cache using the default timeout and cache tags.

// Get the object from the source.
var obj = GetObjectFromDataSource();

CacheService.Put("key", obj, CacheScope.Context | CacheScope.Process, new string[] { "tag1" });

Example 5: Removing an item from cache.

CacheService.Remove("key", CacheScope.Context | CacheScope.Process);

Example 6:  RemoveByTags to remove a group of tagged items from cache.

CacheService.RemoveByTags(new string[] { "tag1" }, CacheScope.Context | CacheScope.Process);

How Tos

Configure your site's cache

Install, configure and enable AppFabric caching