Code Monkey home page Code Monkey logo

guidrecycler's Introduction

GuidRecycler

GuidRecycler

CI NuGet NuGet Guids Recycled Code coverage Uptime Stars

Save the planet, recycle your guids!

What is it?

GuidRecycler is a library that allows you to recycle guids. It is a simple concept.

Installation

GuidRecycler is available as a NuGet package.

Install-Package GuidRecycler

Usage:

Guid recyclers implement the common interface IGuidRecycler which has the following methods:

void Recycle(Guid guid);

To recycle a guid to the recycle bin.

Guid GetGuid();

To retrieve a recycled (or new) guid.

Strategies

There are curently two strategies implemented:

  • ConcurrentGuidRecycler
    A robust thread-safe concurrent guid recycler.
  • GuidRecyclerSlim
    A non-thread-safe guid recycler that is faster than the concurrent guid recycler.

Example

The guid recycler can be created manually or retrieved through dependency injection.

class UserService
{
    private readonly DatabaseContext _context;
    private readonly IGuidRecycler recycler = new GuidRecyclerSlim();

    public async Task DeleteUser(User user)
    {
        _context.Users.Remove(user);
        recycler.Recycle(user.Id);
        await _context.SaveChangesAsync();
    }

    public async Task<User> CreateUser(string name)
    {
        var user = new User
        {
            Id = recycler.GetGuid(),
            Name = name
        };
        _context.Users.Add(user);
        await _context.SaveChangesAsync();
        return user;
    }

As you can see, this code is very simple and eco-friendly. The only thing you need to do is to recycle the guid when you delete the entity and to get a new guid when you create a new entity.

Microsoft.Extensions.DependencyInjection

Coming soon

Performance

See the benchmarks in the benchmarks folder. It clearly shows that the both the concurrent guid recycler and the guid recycler slim are faster than the default guid generator.

Method Mean Error StdDev Median Ratio RatioSD Allocated Alloc Ratio
Guid.NewGuid 647.9 ns 113.68 ns 327.99 ns 600.0 ns 1.00 0.00 600 B 1.00
ConcurrentGuidRecycler 307.1 ns 49.97 ns 146.56 ns 300.0 ns 0.58 0.39 600 B 1.00
GuidRecyclerSlim 224.0 ns 28.33 ns 81.75 ns 200.0 ns 0.45 0.30 600 B 1.00

Contributing

PRs welcome. Merging not guaranteed.

guidrecycler's People

Contributors

tvde1 avatar

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.