Hangfire.InMemory 0.10.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Hangfire.InMemory --version 0.10.0                
NuGet\Install-Package Hangfire.InMemory -Version 0.10.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hangfire.InMemory" Version="0.10.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hangfire.InMemory --version 0.10.0                
#r "nuget: Hangfire.InMemory, 0.10.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hangfire.InMemory as a Cake Addin
#addin nuget:?package=Hangfire.InMemory&version=0.10.0

// Install Hangfire.InMemory as a Cake Tool
#tool nuget:?package=Hangfire.InMemory&version=0.10.0                

Hangfire.InMemory

Build status Quality Gate Status Bugs Code Smells

This is an efficient implementation of in-memory job storage for Hangfire with data structures close to their optimal representation.

The goal of this storage is to provide developers a fast path to start using Hangfire without setting up any additional infrastructure like SQL Server or Redis, with the possibility of swapping implementation in the production environment. The non-goal of this implementation is to compete with TPL or other in-memory processing libraries – serialization alone adds a significant overhead.

This implementation uses proper synchronization, and everything, including locks, queues, or queries, is implemented as blocking operations, so there's no active polling in these cases. Read and write queries are processed by a dedicated background thread to avoid additional synchronization between threads, trying to keep everything as simple as possible with the future async-based implementation in mind. The internal state uses SortedDictionary, SortedSet, and LinkedList data structures to avoid getting even huge collections to the Large Object Heap, reducing the potential OutOfMemoryException-related issues due to memory fragmentation.

This storage also uses a monotonic clock whenever possible by leveraging the Stopwatch.GetTimestamp method. So, expiration rules don't break when the clock suddenly jumps to the future or the past due to synchronization issues or manual updates.

Requirements

Minimal supported version of Hangfire is 1.8.0. Latest version that supports previous versions of Hangfire is Hangfire.InMemory 0.3.7.

Installation

Hangfire.InMemory is available on NuGet, so we can install it, as usual, using our favorite package manager.

> dotnet add package Hangfire.InMemory

Configuration

After the package is installed, we can use the new UseInMemoryStorage method for the IGlobalConfiguration interface to register the storage.

GlobalConfiguration.Configuration.UseInMemoryStorage();

Maximum Expiration Time

Starting from version 0.7.0, the package controls the maximum expiration time for storage entries and sets it to 3 hours by default when a higher expiration time is passed. For example, the default expiration time for background jobs is 24 hours, and for batch jobs and their contents, the default time is 7 days, which can be too big for in-memory storage that runs side-by-side with the application.

We can control this behavior or even turn it off with the MaxExpirationTime option available in the InMemoryStorageOptions class in the following way:

GlobalConfiguration.Configuration.UseInMemoryStorage(new InMemoryStorageOptions
{
  MaxExpirationTime = TimeSpan.FromHours(3) // Default value, we can also set it to `null` to disable.
});

It is also possible to use TimeSpan.Zero as a value for this option. In this case, entries will be removed immediately instead of relying on the time-based eviction implementation. Please note that some unwanted side effects may appear when using low value – for example, an antecedent background job may be created, processed, and expired before its continuation is created, resulting in exceptions.

Comparing Keys

Different storages use different rules for comparing keys. Some of them, like Redis, use case-sensitive comparisons, while others, like SQL Server, may use case-insensitive comparison implementation. It is possible to set this behavior explicitly and simplify moving to another storage implementation in a production environment by configuring the StringComparer option in the InMemoryStorageOptions class in the following way:

GlobalConfiguration.Configuration.UseInMemoryStorage(new InMemoryStorageOptions
{
  StringComparer = StringComparer.Ordinal; // Default value, case-sensitive.
});
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (14)

Showing the top 5 NuGet packages that depend on Hangfire.InMemory:

Package Downloads
Mesawer.InfrastructureLayer

This library contains all the required reusable components in the infrastructure layer.

Eaf.HangFire.AspNetCore

Enterprise Application Foundation - AspNetCore -HangFire

Axelius

Package Description

SimpleIdServer.IdServer

Identity server project with OPENID, OAUTH2.0, UMA2.0, CIBA and FAPI support.

OTAL.DiscordBot

Library for OTAL members to make Discord Bots

GitHub repositories (10)

Showing the top 5 popular GitHub repositories that depend on Hangfire.InMemory:

Repository Stars
Kareadita/Kavita
Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
eventflow/EventFlow
Async/await first CQRS+ES and DDD framework for .NET
fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat
可能是全网最完整的 C# 版微信 SDK,封装全部已知的微信 OpenAPI,包含微信公众平台(订阅号+服务号+小程序+小游戏+小商店+视频号)、微信开放平台、微信商户平台(微信支付+微企付)、企业微信、微信广告平台、微信智能对话开放平台等模块,可跨平台。持续随官方更新,欢迎 Star/Fork/PR。QQ 交流群 875580418【满】、930461548【满】、611974621。
neozhu/CleanArchitectureWithBlazorServer
This is a repository for creating a Blazor Server dashboard application following the principles of Clean Architecture
DevArchitecture/DevArchitecture
DevArchitecture Backend Project
Version Downloads Last updated
1.0.0 74,063 9/18/2024
0.11.0 5,188 9/18/2024
0.10.4 55,143 9/10/2024
0.10.3 270,113 6/17/2024
0.10.2 104,211 6/17/2024
0.10.0 27,031 6/3/2024
0.9.0 132,859 4/23/2024
0.8.1 196,566 3/21/2024
0.8.0 132,444 2/16/2024
0.7.0 181,079 1/10/2024
0.6.0 271,698 10/23/2023
0.5.1 361,322 6/28/2023
0.5.0 5,131 6/27/2023
0.4.1 123,458 5/24/2023
0.4.0 151,806 4/28/2023
0.3.7 148,481 3/22/2023
0.3.6 188,538 2/8/2023
0.3.5 253,000 11/25/2022
0.3.4 1,813,723 11/6/2020
0.3.3 526 11/6/2020
0.3.2 557 10/29/2020
0.3.1 577 10/22/2020
0.3.0 600 10/22/2020
0.2.0 689 10/12/2020
0.1.0 3,050 9/29/2020

https://github.com/HangfireIO/Hangfire.InMemory/releases

0.10.0
• Breaking – `InMemoryStorageOptions` class instances are now immutable after initialization.
• Added – Support long-based job identifiers through the `InMemoryStorageOptions.IdType` property.
• Added – Expose the `InMemoryStorageOptions.CommandTimeout` option to control the command timeouts.
• Changed – Significantly improve query dispatching pipeline in terms of speed and allocations.
• Changed – More compact representation of jobs and their parameters.
• Changed – Optimise the `GetFirstByLowestScoreFromSet` query when the number of items is huge.
• Changed – Better concurrency handling implementation for the collection of locks.

0.9.0
• Added – Implement the disposable pattern for the `InMemoryStorage` class.
• Changed – Use more compact representation of job parameters and state data.
• Changed – Move to `SortedDictionary` and `LinkedList` to avoid using Large Object Heap.
• Changed – `TimeSpan.Zero` value for `MaxExpirationTime` now causes immediate entry eviction.
• Fixed – Ensure near-zero max expiration limit can't lead to uninitialized job eviction.
• Deprecated – `DisableJobSerialization` option is now obsolete, serialization is always enabled.

0.8.1
• Fixed – Incorrect validation in the `MaxStateHistoryLength` setter (by @DPschichholz).

0.8.0
• Project – Sign NuGet package and .NET assemblies on build with a company's own certificate.
• Project – Require package signature validation when restoring dependencies.
• Project – Add HangfireIO as an owner for the NuGet package.
• Project – Add readme file and icon to the NuGet package.
• Project – Fix Git repository URL in the NuGet package metadata.

0.7.0
• Added – `InMemoryStorageOptions.MaxExpirationTime` option to control the maximum expiration time.
• Changed – The default value for maximum expiration time is 2 hours now, not days.
• Fixed – Populate `ParametersSnapshot` and `InvocationData` properties in `IMonitoringApi.JobDetails`.
• Fixed – The "Awaiting Jobs" page now includes the state name of an antecedent background job.
• Fixed – The "Scheduled Jobs" page now has correct identifiers for jobs with explicit queues defined.
• Fixed – Unify job ordering in Monitoring API to be the same as in other storages.
• Project – Enable source link support with embedded symbols for simplified debugging.
• Project – Refactored internals and added even more unit tests.
• Project – Enable NuGet package restore with lock file and locked mode.
• Project – Project and Release Notes URLs in the NuGet package now point to the repository.
• Project – Enable tests running on the `net6.0` platform and Ubuntu on AppVeyor.

0.6.0
• Added – `InMemoryStorageOptions.MaxStateHistoryLength` option to control state entries.
• Changed – Always use monotonic clock when working with time.
• Changed – Release distributed locks when their connection is disposed.
• Changed – Pass dispatcher fault exceptions to a caller thread.
• Project – Refactor internal types to have a cleaner project structure and avoid mistakes.
• Project – Enable static analysis by the Microsoft.CodeAnalysis.NetAnalyzers package.
• Project – Enable portable PDBs for the .NET Framework 4.5.1 platform.

0.5.1
• Fixed – Infinite loop in recurring job scheduler consuming 100% CPU regression after 0.5.0.

0.5.0
• Added – `InMemoryStorageOptions.StringComparer` as a central option for key and index comparisons.

0.4.1
• Fixed – "Awaiting Jobs" metric is now correctly populated with `Version180` compatibility level.

0.4.0
• Breaking – Package now depends on Hangfire.Core version 1.8.0.
• Breaking – Replace the `net45` target with `net451` one as the former is not supported.

• Changed – Improve `GetFirstByLowestScoreFromSet` operations.
• Changed – Implement the `Job.Queue` feature.
• Changed – Implement the `Connection.GetUtcDateTime` feature.
• Changed – Implement the `Connection.GetSetContains` feature.
• Changed – Implement the `Connection.GetSetCount.Limited` feature.
• Changed – Implement the `Connection.BatchedGetFirstByLowestScoreFromSet` feature for the storage.
• Changed – Implement the `Transaction.AcquireDistributedLock` feature.
• Changed – Implement the `Transaction.CreateJob` feature.
• Changed – Implement the `Transaction.SetJobParameter` feature.
• Changed – Implement the new monitoring features.
• Changed – Populate the new properties in Monitoring API.
• Changed – Populate the `Retries` metric in the `GetStatistics` method.

0.3.7
• Fixed – Throw `BackgroundJobServerGoneException` outside of dispatcher thread.

0.3.6
• Fixed – Ensure lock entries are eventually removed from their collection.
• Fixed – Ensure lock entries are always updated under a monitor.

0.3.5
• Fixed – Ensure entries are expired even during constant storage pressure.

0.3.4
• Fixed – Reverse state list instead of sorting it by date in the `JobDetails` method.
• Fixed – Better sorting for state indexes, take into account job creation date.
• Fixed – Reverse succeeded and deleted job lists to match Redis implementation.

0.3.3
• Fixed – Sort queues and servers when returning them from monitoring api and in the Dashboard UI.

0.3.2
• Fixed – Enqueued jobs may become invisible when adding a lot of jobs simultaneously to a new queue.
• Fixed – Some workers are waiting for background jobs forever when several jobs added at once.
• Fixed – Workers are able to detect new background jobs only after another background job is processed.
• Fixed – Workers don't see background jobs when multiple queues are used with minimal workload.

0.3.1
• Fixed – `NullReferenceException` in the `SignalOneQueueWaitNode` method when using multiple queues.

0.3.0
• Added – `InMemoryStorageOptions.DisableJobSerialization` option.
• Fixed – `ObjectDisposedException` on semaphore when committing a transaction.
• Fixed – Gracefully handle `ObjectDisposedException` when signaling for query completion.
• Fixed – Avoid killing the whole process in case of an exception in dispatcher, stop it instead.
• Project – Add a lot of new unit tests for `InMemoryMonitoringApi` class.

0.2.0
• Fixed – A lot of corner cases revealed by unit tests.
• Project – Added a ton of unit tests for the top-level classes to ensure behavior is consistent.

0.1.0 – Initial release