Giter Club home page Giter Club logo

cachetwo.net's Introduction

Extension methods that expand on Microsoft.Extension.Caching.

Build status

The main focus of this library is on type-specific serialization in a distributed cache using Json.NET BSON.

Installation

There are a couple ways to install this package:

  • Install the NuGet package
  • Download the assembly from the latest release and reference it manually
  • Copy the source code directly into your project

This project is available under either of two licenses: MIT or The Unlicense. So if you're able to use the Unlicense, you don't have to provide any kind of attribution if you copy anything into your own project.

Getting Started

This library mostly just adds more extension methods for IDistributedCache or IMemoryCache.

The main feature is handling object serialization with a distributed cache. For example:

using Cachetwo.Distributed;
using Microsoft.Extensions.Caching.Distributed;

public static void Sample(IDistributedCache distributedCache)
{
	int[] value = new[] { 1, 2 };
	distributedCache.Set("sample", value, System.TimeSpan.FromMinutes(15));

	int[] storedValue = distributedCache.Get<int[]>("sample");
	System.Diagnostics.Debug.Assert(System.Linq.Enumerable.SequenceEqual(value, storedValue));
}

There are also methods similar to the GetOrCreate memory cache extension methods:

using Cachetwo.Distributed;
using Microsoft.Extensions.Caching.Distributed;

public static void Sample2(IDistributedCache distributedCache)
{
	int[] value = new[] { 1, 2 };
	int[] storedValue = distributedCache.GetOrCreateIfNotDefault("sample", () => value, System.TimeSpan.FromMinutes(15));
	System.Diagnostics.Debug.Assert(System.Linq.Enumerable.SequenceEqual(value, storedValue));
}

And a few bonus memory cache extension methods are included for specifying options like absoluteExpirationRelativeToNow:

using Cachetwo.Memory;
using Microsoft.Extensions.Caching.Memory;

public static void Sample(IDistributedCache distributedCache)
{
	int[] value = new[] { 1, 2 };
	distributedCache.Set("sample", value, System.TimeSpan.FromMinutes(15));

	int[] storedValue = distributedCache.Get<int[]>("sample");
	System.Diagnostics.Debug.Assert(System.Linq.Enumerable.SequenceEqual(value, storedValue));
}

Object Serialization

Normal objects are serialized using Json.NET BSON to convert the objects to and from BSON. The same limitations apply that you'd have with using [Json.NET] to serialize any object, including:

  • Only public properties with getters and setters will be stored in the cache
  • A self-referencing loop may cause serialization to fail

Also, custom (non-BSON) serialization is not used for primitive types or for common value types including DateTime, DateTimeOffset, TimeSpan, Guid. Values of type string and byte[] also have special handling.

But as much as possible, you should avoid depending on the exact serialization format. It's possible that it will change if the future if a better serialization strategy comes along.

There is not currently any customization available for the serialization strategy. If you need a custom strategy, your best bet is probably to just make your own copy of this library and modify or replace the BinaryConvert class.

cachetwo.net's People

Contributors

22222 avatar

Watchers

 avatar James Cloos avatar

Forkers

jtone123

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.