Giter VIP home page Giter VIP logo

craigwardman / chunkingredisclient Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 2.0 37 KB

A C# library which implements various wrappers around the StackExchange.Redis client, specifically using Newtonsoft.Json serialisation; Such as streamed reading/writing and sliding expiration.

License: MIT License

C# 100.00%
redis redis-client json newtonsoft-json stackexchange-redis chunking wrapper-library extensions net-core csharp

chunkingredisclient's Introduction

Chunking Redis Client

A C#/.NET Core library which wraps the StackExchange.Redis client, specifically using JSON serialisation, and adds functionality such as chunked reading/writing and sliding expiration.

To install without the source, use the NuGet package: https://www.nuget.org/packages/ChunkingRedisClient/

The purpose of this library is to create a re-usable library of code for wrapping the StackExchange.RedisClient and solving the issues I usually need to solve.

Those being:

  • IoC wrappers/abstractions

    • Just take your dependency on IRedisClient<TKey, TItem>
    • By default you should configure your DI container to inject the provided RedisClient<TKey, TItem>
    • Since IoC is used throughout you also need to configure:
      ~ IRedisWriter<TKey, Item> -> JsonRedisWriter or ChunkedJsonRedisWriter
      ~ IRedisReader<TKey, Item> -> JsonRedisReader or ChunkedJsonRedisReader
      ~ IRedisWriter<TKey, Item> -> JsonRedisDeleter or ChunkedJsonRedisDeleter
      (note: for one combination of TKey, TItem - ensure the decision to chunk or not is consistent)
      ~ IKeygen<TKey> to an object specific implementation, like GuidKeygen
      ~ For chunking, locking is required:
      IRedisLockFactory -> RedisLockFactory
      To override the default of InMemoryRedisLock, call RedisLockFactory.Use<IRedisLock>() <-- your class here
  • Strongly typed access to the cache

    • Use any C# object as your TKey and TItem, given that:
      ~ You can implement your own Keygen for TKey
      ~ Your TItem is serialisable by Newtonsoft.Json
  • Implementing the StackExchange Connection Multiplexer

    • This is handled by the RedisDatabaseFactory
    • Not using the usual Lazy<ConnectionMulitplexer> approach, as I want to support one multiplexer per connection string (if your app is dealing with more than 1 cache)
    • The multiplexers are stored in a concurrent dictionary where the connection string is the key
    • The multiplexer begins connecting asynchronously on first use
  • Sliding expiration of cache keys

    • Pass in the optional timespan to read methods if you want to use sliding expiration
    • This updates the expiry when you read the item, so that keys which are still in use for read purposes live longer
  • Chunked JSON data

    • This solves a performance issue whereby Redis does not perform well with large payloads.
    • Sometimes you may also have had errors from the server when the queue is full.
    • The default chunk size is 10KB which can be configured in the ChunkedJsonRedisWriter
    • The JSON data is streamed from Newtonsoft into a buffer. Every time the buffer is full it is written to Redis under the main cache key with a suffix of "chunkIndex"
    • The main cache key is then written to contain the count of chunks, which is used by the reader and deleter.
  • Generating keys for objects

    • I don't like using bytes for keys as they are not human readable, so I like to generate unique strings
    • There is no none-intrusive way of providing a type agnostic generic keygen, therefore you must write your own. If you write something for a CLR type, considering contributing it to the project!
    • Since we know Guids are unique, I have demonstrated the ability to create custom keygens.

The code can be extended to support other serialisation types (TODO), distributed locks (TODO), different ways of generating keys or whatever you need it to do.

chunkingredisclient's People

Contributors

craigwardman avatar dependabot[bot] avatar explorer14 avatar

Stargazers

 avatar  avatar

chunkingredisclient's Issues

GetHashCode is not a great idea for GenericKeygen

There are several issues with using GetHashCode for creating distributed cache keys (such as collisions), mainly that two processes don't necessarily generate the same hash code for the same value which would break the key gen within a distributed system (which is exactly the purpose of using Redis ๐Ÿ˜„ )

Therefore I will be fixing this bug, which will probably mean there is no "non-intrusive default" way of calculating an objects key ๐Ÿ˜ข

What are the performance goals with this?

hey Craig,

Good effort as usual!

Could you share what were the performance stats before and after this change was implemented? In other words, can you quantify what does "performing badly" really mean? Redis' memory consumption is only really limited by the available system memory which will ofcourse depend on whether the host is 32-bit or 64-bit. Perhaps its a design smell that you are having to store very large JSON blobs in a key value store like Redis as opposed to a dedicated persistent document store like Dynamo DB or Cosmos DB. But then again a little more context with some stats than what's on your blog would help me understand this a bit better.

You could also look at ReJSON which is a new module for Redis that supports JSON natively (internally it uses binary format to store the JSON data more effectively): https://oss.redislabs.com/redisjson/.

Cheers
Aman

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.