netCache 1.0.1

dotnet add package netCache --version 1.0.1
NuGet\Install-Package netCache -Version 1.0.1
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="netCache" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add netCache --version 1.0.1
#r "nuget: netCache, 1.0.1"
#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 netCache as a Cake Addin
#addin nuget:?package=netCache&version=1.0.1

// Install netCache as a Cake Tool
#tool nuget:?package=netCache&version=1.0.1

netCache

netCache is a simple, dependency injection friendly and persistant cache storage library.

Design goals

netCache is designed to be a simple and fast library that provides an almost silver-bullet solution with caching and persistance for .NET applications. I've designed the library to be functional for all modern .NET applications.

Features

  • Garbage collection (soon);
  • Object lifetimes;
  • Persistant caching to the disk;
  • JSON serialization with Snappy compression - you can use your own serialization implementation;
  • Layered memory caching;
  • Uses modern C# features (generic types & nullable reference types);
  • Simple and easy to use interface, no further abstraction layers required;
  • Fast and can be modified to be even faster without forking.

Installation

Run the following in your package manager console or install via the NuGet package manager on Visual Studio or Rider;

PM> Install-Package NetCache

Dependency injection

If you wish to use NetCache with dependency injection, you should also install the following package

PM> Install-Package NetCache.DependencyInjection

Usage

You can start using the package by either injecting it into your service collection or creating a new instance of the NetCacher class.

var cacher = new NetCacher(new NetCacherOptions()); // Create a new cacher with default options

Or, with dependency injection

services.AddNetCacher(opts =>
{
    ...
});

Getting and setting records

Firstly, here is our mock class that we'll be using in the following examples:

public class Mock
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Mock(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

Now we can get started, to set a new record in the cache, you can call the SetObject<TObj> method as shown

var myObj = new Mock("Foo", "Bar");

cacher.SetObject<Mock>("myKey", myObj, TimeSpan.FromMinutes(1));

// Object has been cached under the key 'myKey' for 1 minute before it expires

And for getting that same record, we can use the GetRecord<TObj> method as shown below

var myObj = cacher.GetObject<Mock>("myKey");
if (myObj is null)
{
    // No object was found in the cache, it could've expired or never existed
    Console.WriteLine("No object found!");
    return;
}

Console.WriteLine(myObj.FirstName); // Foo

Custom serializers

Maybe, the default serializer just isn't cutting it for you and you need your own custom serializer that gives you specifically tailored solutions. No problem, find below a simple implementation of a custom serializer.

public class CustomSerializer : ISerializer
{
    public ReadOnlySpan<char> Serialize<TObj>(TObj obj)
    {
        // ...
    }

    public TObj? Deserialize<TObj>(ReadOnlySpan<char> rawData)
    {
        // ...
    }
}

Granted that this one does absolutely nothing, but it can easily provide you enough room to build your own requirements into the serializing system. You could even speed up the performance of the library (in that case, open a pull request!).

License

Licensed under the MIT license and currently leveraging these open source packages:

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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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 (1)

Showing the top 1 NuGet packages that depend on netCache:

Package Downloads
netCache.DependencyInjection

Dependency Injection extension methods for netCache

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 243 2/6/2023
1.0.0 230 2/5/2023