DotNetNuke (DNN) is a web content management system (CMS) and web application framework based on Microsoft .NET. The ability to cache data efficiently, improving performance and scalability of web applications is one of its key features. It is a robust system that allows developers to store and retrieve data quickly, reducing the need for repetitive database calls while enhancing the overall user experience.
DNN Website performance optimization is essential for maintaining your website’s health. With constant changes in content, DNN aims to reduce the cost of website maintenance. This open-source software allows non-technical staff to easily add relevant pages without external help. You can change page layouts and add new features and pages to your website, making it the most user-friendly method. This CMS provides a platform that adapts to market demands, enabling the maintenance of new content without the need for a computer programmer. DNN's caching and performance tuning features are key components for optimizing your website.
What is Data Caching?
Data caching refers to a method of storing data that is frequently accessed in an area called a cache which is temporary. In this way, data can be accessed quickly since it is retrieved from cache instead of being fetched from the database or other slow storage. Thus, caching is especially crucial for web applications because lowering latency and loading times results in improved performance by great margins.
To effectively operate this open-source software, it's crucial to understand DNN caching and its significance in the content management system. Caching is a process in any CMS that temporarily stores multiple copies of files, allowing for faster website loading. Like how DNS servers have lookups for DNS records and web browsers cache HTML files, JavaScript, and images, caching helps web pages load more quickly. A CDN server reduces content cache, storing images, JavaScript, and HTML files, which maximizes data retrieval performance when the cache holds the data. All cached data is stored in RAM, allowing DNN caching servers to operate at high speed. When a page request is received, it quickly processes on the back end of the CMS, resulting in an HTML file that is promptly sent to the visitor’s browser. If caching is enabled, the server will continually store the HTML files.
DNN employs two types of caching: server caching and browser caching. Browser caching stores files for a short period, so when a user revisits a website, the files do not need to be retrieved again, reducing load time. Server caching, on the other hand, saves all requests in a cluster and shows the results much faster, typically within seconds. Object caching saves a minimal amount of data and reduces website load by avoiding repeated loading of the same objects. By leveraging these caching mechanisms, DNN ensures faster, more efficient website performance.
What Should be Cached?
Understanding what to cache can be challenging, as there's no one-size-fits-all answer. The key is that you can cache any necessary objects to improve performance. Let me illustrate this with examples from two popular open-source modules we use.
For instance, consider caching individual objects in your controller class. In our "Expandable Text/HTML" module, we cache custom settings. By incorporating cache-checking code inside the `GetSettings` method, we ensure that the database is accessed only if the object isn't already cached. This approach minimizes database hits for frequently used objects.
Additionally, you can cache generated content. Suppose you have a module that retrieves several objects from the database, processes them, and then loads the content into an ASP.NET Literal control on the UI. Instead of just caching the database objects, you could cache the fully generated content. Even with a 15-minute cache time, this can significantly reduce CPU usage, memory consumption, and page load times for your custom modules.
DotNetNuke offers several caching options, each with unique approaches and behaviors:
Object Caching: implemented in the core, modules, and providers, caching objects from data sources to avoid repeated requests for the same objects. This approach is highly effective, as data source requests—whether from a database query, web service call, or file—are time-consuming. The type and duration of caching can be controlled via Host settings and are often adjusted during DotNetNuke performance optimization.
Module Output Caching: If the content of a module does not vary by user, the rendered HTML of the module can be cached (either as a file or in memory) to avoid being recreated with each call. Cache duration and location can be specified in the Module Settings, unless suppressed by the Module Definition itself.
Page Output Caching: This allows the caching of the rendered output of an entire page, provided the content does not vary by user in any module or skin object (e.g., displaying a username or messaging inbox would prevent caching the page). Page caching providers are not included in the Community Framework but are available in commercial editions and from third-party developers.
Types of Caching Providers
Broadcast Polling Caching Provider: This provider used a backend database to handle cache synchronization. However, it faced scaling issues and could cause a site to lock up as the database became overwhelmed with requests. It was deprecated on June 23, 2009, and is no longer supported or updated.
File Based Caching Provider: Available in both Community Edition (CE) and Professional Edition (PE), this provider uses a central file store to invalidate cache entries. Its major weakness, aside from requiring the correct setup of permissions, application pools, domain users, and code access security, is that it makes the file store (typically a SAN) a single point of failure. While it usually works well, it can suffer from latency if there are many web heads in the web farm.
Web Based Caching Provider: This provider is exclusive to the Professional Edition (PE) and is the recommended option for all PE customers. Its main advantages are minimal setup requirements and the absence of central points of failure. When an entry is changed on one web server, the provider makes requests to the other web servers via HTTP to expire their cache, avoiding the need for a central resource that could fail or slow down due to multiple requests.
These providers only handle cache invalidation. If a user is making site changes (e.g., uploading files, installing modules), they must ensure these files are synchronized across all web servers. This can be achieved through various methods, such as using Microsoft DFS, a utility like robocopy, or a SAN as a central file store.
The Data Cache Mechanism in DotNetNuke : https://www.dnnsoftware.com/docs/administrators/servers/configure-caching.html
DNN’s DataCache method is formed on .NET caching framework. It gives a way of organizing and managing around the DNN platform data stored in the memory. The DataCache class is the main access point to use when working with the cache in DNN.
Key Features of DNN DataCache Mechanism:
1. Centralized Cache Management: DNN offers a centralized approach to managing cached data, simplifying the maintenance of consistency and control over what data is cached and for how long.
2. Cache Dependency: DNN supports cache dependencies, enabling automatic invalidation of cached data when the underlying data changes.
3. Cache Expiration: The DataCache mechanism supports various cache expiration policies, including absolute expiration and sliding expiration.
4. Cache Scavenging: DNN includes cache scavenging mechanisms that free up memory by removing the least recently used or less important cached data.
Clearing the Cache in DotNetNuke (DNN)
While clearing the cache in DotNetNuke (DNN) may seem straightforward to some, it’s not always obvious for everyone. This is why I thought it would be helpful to provide a detailed guide.
Clearing the cache is necessary only in specific situations, as the cache typically updates itself over time. For example, if you manually change Host, Site, Tab, or Module settings in the database, those changes won’t be visible on the site until the cache is cleared and the data is refreshed.
Here are a few methods to clear the cache:
1. Modify the Web.Config: Open the Web.Config file and insert a line break somewhere that maintains well-formed XML. Save the file to trigger a cache refresh.
2. Restart the Application: Log in as a "host" on the portal, navigate to the Host Settings page in the Host Menu, and click the "Restart Application" link at the bottom of the page.
3. Recycle the Application Pool: Right-click on the Application Pool where your site resides and select "Recycle." Note that this will also recycle all other applications within the same Application Pool.
These steps will help ensure that any changes are properly reflected on your site.
Utilizing Data Cache in DNN
To effectively use the DataCache mechanism in DNN, developers need to understand how to interact with the DataCache class. Here are some common operations:
1. Adding Data to the Cache
public static void AddToCache(string cacheKey, object data, TimeSpan expiration)
{
if (data != null)
{
DataCache.SetCache(cacheKey, data, DateTime.UtcNow.Add(expiration));
}
}
In this example, AddToCache is a method that adds data to the cache with a specified expiration time. The cacheKey is a unique identifier for the cached data, data is the object to be cached, and expiration is the time span after which the cache should expire.
2. Retrieving Data from the Cache:
public static object GetFromCache(string cacheKey)
{
return DataCache.GetCache(cacheKey);
}
The GetFromCache method retrieves data from the cache using the cacheKey. If the data is found in the cache, it is returned; otherwise, null is returned.
3. Removing Data from the Cache:
public static void RemoveFromCache(string cacheKey)
{
DataCache.RemoveCache(cacheKey);
}
The RemoveFromCache method removes data from the cache based on the cacheKey.
Practical Example
Let's consider a practical example where we cache the result of a database query that retrieves a list of users. This can significantly improve performance, especially if the user list is requested frequently.
public static List<UserInfo> GetUsers()
{
string cacheKey = "UserList";
var cachedUsers = DataCache.GetCache(cacheKey) as List<UserInfo>;
if (cachedUsers != null)
{
return cachedUsers;
}
// Simulate a database call
List<UserInfo> users = Database.GetUsersFromDatabase();
// Cache the result for 10 minutes
DataCache.SetCache(cacheKey, users, TimeSpan.FromMinutes(10));
return users;
}
In this example, the GetUsers method first attempts to retrieve the list of users from the cache. If the data is not found in the cache, it simulates a database call to get the users and then caches the result for 10 minutes. Subsequent calls to GetUsers within the 10-minute window will return the cached data, reducing the load on the database.
Advanced Caching Techniques
- Cache Dependencies: DNN allows setting up cache dependencies, ensuring cached data is invalidated when related data changes. For example, if user data is updated, the cached user list should be invalidated.
- Sliding Expiration: Sliding expiration resets the cache expiration time each time the cached data is accessed, ensuring that frequently accessed data remains in the cache longer.
- Absolute Expiration: Absolute expiration sets a fixed time at which the cache entry will expire, regardless of how often it is accessed.
- Cache Scavenging: DNN can automatically remove less critical cached data when memory resources are low, ensuring that the application remains responsive.
Best Practices for Using Data Cache
- Identify Cacheable Data: Not all data should be cached. Identify data that is expensive to retrieve and frequently accessed.
- Cache Duration: Choose appropriate cache durations based on the volatility of the data. Highly dynamic data should have shorter cache durations.
- Cache Keys: Use descriptive and unique cache keys to avoid collisions and ensure that the correct data is retrieved.
- Monitor Cache Usage: Regularly monitor cache usage and performance to ensure that caching is providing the expected benefits.
The DataCache mechanism in DotNetNuke is a powerful tool that can greatly improve web application performance by minimizing repetitive data retrieval operations. By effectively utilizing the Data Cache class and following best practices, developers can build more responsive and scalable applications. Whether caching database query results or other resource-intensive operations, leveraging DNN's Cache can result in a more efficient and user-friendly web experience. As web developers, optimizing application performance is an ongoing task. DotNetNuke's built-in caching system provides a valuable tool to enhance performance and ensures that the applications we develop can scale effectively in Web Farm environments.