Bitwarden.Server.Sdk.Resources.Caching.Abstractions 0.2.0

Prefix Reserved
dotnet add package Bitwarden.Server.Sdk.Resources.Caching.Abstractions --version 0.2.0
                    
NuGet\Install-Package Bitwarden.Server.Sdk.Resources.Caching.Abstractions -Version 0.2.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="Bitwarden.Server.Sdk.Resources.Caching.Abstractions" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitwarden.Server.Sdk.Resources.Caching.Abstractions" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Bitwarden.Server.Sdk.Resources.Caching.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitwarden.Server.Sdk.Resources.Caching.Abstractions --version 0.2.0
                    
#r "nuget: Bitwarden.Server.Sdk.Resources.Caching.Abstractions, 0.2.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.
#:package Bitwarden.Server.Sdk.Resources.Caching.Abstractions@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitwarden.Server.Sdk.Resources.Caching.Abstractions&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Bitwarden.Server.Sdk.Resources.Caching.Abstractions&version=0.2.0
                    
Install as a Cake Tool

Bitwarden.Server.Sdk.Resources.Caching.Abstractions

Audience: engineers writing a Bitwarden service that fills a shared cache of its resources, or a client that reads one.

About

The keys, hash fields and settings that let a service's callers read some of its resources straight from a shared Redis instead of calling it, agreed by the owner and its callers without one referencing the other. Nothing here connects to Redis. It depends only on Microsoft.Extensions.Configuration.Abstractions and Bitwarden.Server.Sdk.CryptoPrimitives, for CacheSecretSetting.

Installing

dotnet add package Bitwarden.Server.Sdk.Resources.Caching.Abstractions

Configuration

Setting Read by Default What it does
Caching:Owned:Redis:Configuration A service host and a client None The service cache's Redis connection. Unset, there is no service cache: a host fills nothing and a client always asks the owner.
Caching:Owned:Resources:{service}:{type}:Enabled A service host and a client true false tells the owner's host not to fill that resource, and its clients not to read it, so they ask the owner.

The keys are public constants: OwnedCacheEntry.ConnectionKey and OwnedCacheEntry.ResourcesSection.

Key concepts

  • The owner fills, its callers read. The service that owns the data fills entries under its own prefix, OwnedCacheEntry.Prefix(service), and a host writes only there. OwnedCacheEntry holds the key layout, the hash fields and the settings.
  • Each resource is one Redis hash:
Field Holds
v The row's version when it was read, or when it changed
o The row's owner, as {"organization":"..."}; absent for a row nobody owns
d The resource as the service serves it, less what it works out per caller; a cached read-many's, their array
f The format a resource's entry is written in, OwnedCacheEntry.CurrentFormat
none Nothing is recorded under the key
gone A tombstone: the row changed or was deleted at version v

Usage

Key a cached resource by its identifier

WidgetCacheKeys.cs:

using Bitwarden.Server.Sdk.Resources.Caching;

public static class WidgetCacheKeys
{
    public static string For(Guid widget) =>
        OwnedCacheEntry.Key(OwnedCacheEntry.Template("widget-service", "widget"), widget);
}

WidgetCacheKeys.For(id) is widget-service:widget:{id}. A service or resource type may not contain :, {, } or ., and the resource types answer, index, gen and idempotency are reserved for the service's other keys; Template throws ArgumentException for either.

Key a cached resource by another key

A resource read by a key other than its identifier gets its own template, so its entries never mix with those by identifier. A key that names a row only within its owner also names the owner's kind, which the reader fills from the party its call carries.

WidgetByMakerCacheKeys.cs:

using Bitwarden.Server.Sdk.Resources.Caching;

public static class WidgetByMakerCacheKeys
{
    private static readonly string Template =
        OwnedCacheEntry.KeyedTemplate("widget-service", "widget", "maker", "organization");

    public static string? For(Guid organization, Guid maker) =>
        OwnedCacheEntry.Key(Template, maker, organization, user: null, provider: null);
}

The template is widget-service:widget:by-maker:{organization}:{key}. The owner's kind is organization, user or provider, filled from {organization}, {user} or {provider}. Key returns null when the template names a party that isn't given, or is given as Guid.Empty: there is then no entry to read, so ask the owner. The overload that takes only a key throws ArgumentException for a template that names a party, since a key with its party unfilled would be one entry every party shares.

Key a cached read-many

A cached read-many is one entry holding every resource of the party a call carries, in one order. Its resource is its own, so the template names only the resource and the party, which a reader fills from its call.

OwnWidgetCacheKeys.cs:

using Bitwarden.Server.Sdk.Resources.Caching;

public static class OwnWidgetCacheKeys
{
    private static readonly string Template = OwnedCacheEntry.ReadManyTemplate("widget-service", "own-widget", "user");

    public static string? For(Guid user) => OwnedCacheEntry.Key(Template, null, null, user, null);
}

The template is widget-service:own-widget:read-many:{user}, and the entry's d is the array of resources in the read's order.

Read a secret that derives keys

A secret that derives keys with CacheKey from Bitwarden.Server.Sdk.CryptoPrimitives is read with CacheSecretSetting, under a setting the caller names.

WidgetSecret.cs:

using Bitwarden.Server.Sdk.CryptoPrimitives;
using Bitwarden.Server.Sdk.Resources.Caching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

public static class WidgetSecret
{
    public static CacheSecret Read(IConfiguration configuration, IHostEnvironment environment) =>
        CacheSecretSetting.Read(configuration, "Widgets:Secret", environment.EnvironmentName);
}

The setting holds base64 of 32 bytes. In Development an unset secret is CacheSecret.Development. Elsewhere Read throws InvalidOperationException when it is unset or is the development secret, which is public. Text that is not base64 of 32 bytes throws everywhere. Each message names the setting, never its value.

Switch a cache off in an incident

IServiceCacheSwitches answers, for an owner's name and a resource type, whether the resource is cached (Caches) and whether a change the cache can't be told of is refused (GuardsWrites). The owner and every client reading its cache ask it, so both follow one switch. ServiceCacheSwitches.AlwaysOn answers yes to both; Bitwarden.Server.Sdk.Service registers switches read from the feature flags ServiceCacheFlags names. The switches add to the Enabled setting in the configuration table: a resource is cached only when both say so.

CacheIncident.cs:

using Bitwarden.Server.Sdk.Resources.Caching;

public static class CacheIncident
{
    // ["disable-service-cache", "disable-service-cache.widget-service", "disable-service-cache.widget-service.widget"]
    public static IReadOnlyList<string> CachingFlags() => ServiceCacheFlags.OffFor("widget-service", "widget");

    // The same three levels, under "disable-service-cache-write-guard".
    public static IReadOnlyList<string> WriteGuardFlags() => ServiceCacheFlags.WriteGuardOffFor("widget-service", "widget");
}

Each flag is false unless set, and any level set applies. The levels are separated by ., which no service or resource name may contain, so no two flags name the same thing; OffFor and WriteGuardOffFor throw ArgumentException for a name that does.

Switching caching off is always safe: changes still invalidate entries, so switching it on again serves nothing stale. Switching the write guard off is an emergency step, for when Redis is unreachable and refusing changes would stop people saving anything. A change committed meanwhile leaves its stale entries in Redis until they expire, and switching the guard off switches caching off only while it stays off. So once Redis is back, switch caching off at the same level first, clear the write-guard flag, and keep caching off for the resource's entry lifetime before switching it on again.

Gotchas and constraints

  • Check the owner beside the data. A reader checks o itself and never trusts the payload for whose row it is.
  • A tombstone is a miss. A host also refuses a fill of any older version under it, so a slow read can't put back a value a later change replaced.
  • What an entry holds is the owner's choice. This package names the fields, not what goes in d. An owner fills an entry only with attributes every reader of the cache may see: it is Data at Rest in Redis, readable by anyone with read access to the owner's prefix.
  • One secret per purpose. Give each use of CacheSecretSetting its own setting, so a leaked secret exposes only the keys of that purpose.

Types

Type Purpose
OwnedCacheEntry How a service-owned cache entry is stored in Redis, and the settings it uses
IServiceCacheSwitches Whether a resource is cached, and whether a change the cache can't be told of is refused, as an incident sets it
ServiceCacheSwitches AlwaysOn: the switches a host has when it registers none
ServiceCacheFlags The feature flags that switch caching and the write guard off, for every service, a service or a resource
CacheSecretSetting Reads any secret that derives keys with CacheKey: required outside Development, never the development secret there
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bitwarden.Server.Sdk.Resources.Caching.Abstractions:

Package Downloads
Bitwarden.Server.Sdk.Idempotency.Abstractions

Idempotency keys, and the operations whose writes repeat the same keys, shared by Bitwarden services and their callers.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 158 12/5/2026
0.1.0 881 12/4/2026