Skip to content

Commit

Permalink
Use Expiry-sliding in notification cache (#16662)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Sep 4, 2024
1 parent 3b3b7f2 commit 3e56e0e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,25 @@ public override async Task<IDisplayResult> DisplayAsync(Navbar model, BuildDispl
}).Location("Detail", "Content:9")
.Location("DetailAdmin", "Content:9");

if (_notificationOptions.CacheDurationInSeconds > 0)
if (_notificationOptions.AbsoluteCacheExpirationSeconds > 0 || _notificationOptions.SlidingCacheExpirationSeconds > 0)
{
return result
.Cache(NotificationConstants.TopUnreadUserNotificationCacheTag, context => context
.AddContext("user")
.WithExpiryAfter(TimeSpan.FromSeconds(_notificationOptions.CacheDurationInSeconds))
// Allow another feature to clear all notification cache entries if necessary.
.AddTag(NotificationConstants.TopUnreadUserNotificationCacheTag)
.AddTag(NotificationsHelper.GetUnreadUserNotificationTagKey(_httpContextAccessor.HttpContext.User.Identity.Name))
);
.Cache(NotificationConstants.TopUnreadUserNotificationCacheTag, context =>
{
context.AddContext("user")
// Allow another feature to clear all notification cache entries if necessary.
.AddTag(NotificationConstants.TopUnreadUserNotificationCacheTag)
.AddTag(NotificationsHelper.GetUnreadUserNotificationTagKey(_httpContextAccessor.HttpContext.User.Identity.Name));

if (_notificationOptions.AbsoluteCacheExpirationSeconds > 0)
{
context.WithExpiryAfter(TimeSpan.FromSeconds(_notificationOptions.AbsoluteCacheExpirationSeconds));
}
else if (_notificationOptions.SlidingCacheExpirationSeconds > 0)
{
context.WithExpirySliding(TimeSpan.FromSeconds(_notificationOptions.SlidingCacheExpirationSeconds));
}
});
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ public class NotificationOptions
public bool DisableNotificationHtmlBodySanitizer { get; set; }

/// <summary>
/// How may seconds should the top-unread notification be cache for.
/// 0 value will indicate no cache.
/// Specifies the cache duration in seconds for the top-unread notification.
/// A value of 0 disables the max-cache expiration.
/// If both this property and <see cref="SlidingCacheExpirationSeconds"/> are set to 0, caching will be disabled.
/// </summary>
public int CacheDurationInSeconds { get; set; } = 3600;
public int AbsoluteCacheExpirationSeconds { get; set; }

/// <summary>
/// Specifies the sliding cache duration in seconds for the top-unread notification.
/// A value of 0 disables sliding-caching expiration.
/// If both this property and <see cref="AbsoluteCacheExpirationSeconds"/> are set to 0, caching will be disabled.
/// </summary>
public int SlidingCacheExpirationSeconds { get; set; } = 1800;
}
3 changes: 2 additions & 1 deletion src/docs/reference/modules/Notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Available Options and Their Definitions:
|------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `TotalUnreadNotifications` | Specifies the maximum number of unread notifications displayed in the navigation bar. Default is 10. |
| `DisableNotificationHtmlBodySanitizer` | Allows you to disable the default sanitization of the `HtmlBody` in notifications generated from workflows. |
| `CacheDurationInSeconds` | Caches the top unread user notifications for performance optimization, with a default duration of 3,600 seconds. Can be adjusted or set to `0` to disable caching. |
| `AbsoluteCacheExpirationSeconds` | Specifies the absolute maximum duration, in seconds, for which the top unread user notifications are cached when caching is enabled. A value of 0 does not disable caching but indicates that there is no fixed expiration time for the cache. You can set this value to define a maximum lifespan for the cached data before it is invalidated. |
| `SlidingCacheExpirationSeconds` | Defines the sliding cache duration, in seconds, for unread user notifications when caching is enabled. By default, the cache is refreshed and extended for up to 1800 seconds (30 minutes) after each user activity. To disable sliding expiration, you can set this value to 0. |

## Notification Methods

Expand Down
3 changes: 2 additions & 1 deletion src/docs/releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ Furthermore, the introduction of the `NotificationOptions` provides configuratio

- **`TotalUnreadNotifications`**: This property specifies the maximum number of unread notifications that are displayed in the navigation bar. By default, it is set to 10.
- **`DisableNotificationHtmlBodySanitizer`**: Notifications generated from workflows have their `HtmlBody` sanitized by default. This property allows you to disable the sanitization process if needed.
- **`CacheDurationInSeconds`**: When caching is enabled, the top unread user notifications are cached for performance optimization. By default, this cache lasts for 3,600 seconds. You can adjust this value or set it to `0` to disable caching.
- **`AbsoluteCacheExpirationSeconds`**: Specifies the absolute maximum duration, in seconds, for which the top unread user notifications are cached when caching is enabled. A value of 0 does not disable caching but indicates that there is no fixed expiration time for the cache. You can set this value to define a maximum lifespan for the cached data before it is invalidated.
- **`SlidingCacheExpirationSeconds`**: Defines the sliding cache duration, in seconds, for unread user notifications when caching is enabled. By default, the cache is refreshed and extended for up to 1800 seconds (30 minutes) after each user activity. To disable sliding expiration, you can set this value to 0.

### Secure media files

Expand Down

0 comments on commit 3e56e0e

Please sign in to comment.