Skip to content

d-franklin/StackExchange.Redis.Extensions

 
 

Repository files navigation

StackExchange.Redis.Extensions

StackExchange.Redis.Extensions is a library that extends StackExchange.Redis allowing you a set of functionality needed by common applications.

What can it be used for?

Caching of course. Instead of use directly StackExchange.Redis could be easier use ICacheClient

For example:

  • Add an object to Redis;
  • Remove an object from Redis;
  • Search Keys into Redis;
  • Retrieve multiple object with a single roundtrip;
  • Store multiple object with a single roundtrip;
  • Async methods;
  • Retrieve Redis Server status;
  • Much more;
Channel Status
Appveyor CI (Windows) Build status
Travis CI (Linux) Build Status
Nuget (Core) NuGet Status
Nuget (Json.NET) NuGet Status
Nuget (MsgPack) NuGet Status
Nuget (Protobuf) NuGet Status
Issue stats Issue Stats

##How to install it StackExchange.Redis.Extensions is composed by two libraries, the Core and the Serializer implementation. Because there are several good serializer and we don't want add another dependency in your project you can choose your favorite or create a new one.

##Install Core NuGet Status

PM> Install-Package StackExchange.Redis.Extensions.Core

Install Json.NET implementation NuGet Status

PM> Install-Package StackExchange.Redis.Extensions.Newtonsoft

##Install Jil implementation NuGet Status

PM> Install-Package StackExchange.Redis.Extensions.Jil

##Install Message Pack CLI implementation NuGet Status

PM> Install-Package StackExchange.Redis.Extensions.MsgPack

##Install Protocol Buffers implementation NuGet Status

PM> Install-Package StackExchange.Redis.Extensions.Protobuf 

How to configure it

You can use it registering the instance with your favorite Container. Here an example using Castle Windsor:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="redisCacheClient"
			   type="StackExchange.Redis.Extensions.Core.Configuration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.Core" />
	</configSections>

	<redisCacheClient allowAdmin="true" ssl="false" connectTimeout="5000" database="0" password="my password">
		<hosts>
			<add host="127.0.0.1" cachePort="6379"/>
		</hosts>
	</redisCacheClient>
</configuration>
container.Register(Component.For<ISerializer>()
				.ImplementedBy<NewtonsoftSerializer>()
				.LifestyleSingleton());

container.Register(Component.For<ICacheClient>()
				.ImplementedBy<StackExchangeRedisCacheClient>()
				.LifestyleSingleton());

of you can create your own instance

var serializer = new NewtonsoftSerializer();
var cacheClient = new StackExchangeRedisCacheClient(serializer);

To specify the connection string it's enough to add it into AppSetting in your config files (use RedisConnectionString as key) or specify your ConnectionMultiplexer instance into the constructor.

Serialization

To offer the opportunity to store a class into Redis, that class must be serializable; right now there are three serialization options:

  • BinarySerialization (Requires SerializableAttribute on top of the class to store into Redis)
  • NewtonSoft (Uses JSon.Net to serialize a class without SerializableAttribute)
  • Jil (Use super fast json serializer)
  • MessagePack CLI (serialization/deserialization for CLI)

How can I store an object into Redis?

There are several methods in ICacheClient that can solve this request.

var user = new User()
{
	Firstname = "Ugo",
	Lastname = "Lattanzi",
	Twitter = "@imperugo"
	Blog = "http://tostring.it"
}

bool added = myCacheClient.Add("my cache key", user, DateTimeOffset.Now.AddMinutes(10));

How can I retrieve an object into Redis?

Easy:

var cachedUser = myCacheClient.Get<User>("my cache key");

How can I retrieve multiple object with single roundtrip?

That's a cool feature that is implemented into ICacheClient implementation:

var cachedUsers = myCacheClient.GetAll<User>(new {"key1","key2","key3"});

How can I add multiple object with single roundtrip?

That's a cool feature that is implemented into ICacheClient implementation:

IList<Tuple<string, string>> values = new List<Tuple<string, string>>();

values.Add(new Tuple<string, string>("key1","value1"));
values.Add(new Tuple<string, string>("key2","value2"));
values.Add(new Tuple<string, string>("key3","value3"));

bool added = sut.AddAll(values);

Can I search keys into Redis?

Yes that's possible using a specific pattern. If you want to search all keys that start with myCacheKey:

var keys = myCacheClient.SearchKeys("myCacheKey*");

If you want to search all keys that contain with myCacheKey:

var keys = myCacheClient.SearchKeys("*myCacheKey*");

If you want to search all keys that end with myCacheKey:

var keys = myCacheClient.SearchKeys("*myCacheKey");

Can I use a Redis method directly from ICacheClient without add another dependency to my class?

Of course you can. ICacheClient exposes a readonly property named Database that is the implementation of IDatabase by StackExchange.Redis

myCacheClient.Database.SetAdd("mykey","another key");

How can I get server information?

ICacheClient has a method GetInfo and GetInfoAsync for that:

var info = myCacheClient.GetInfo();

For more info about the values returned, take a look here

Contributing

Getting started with Git and GitHub

Once you're familiar with Git and GitHub, clone the repository and start contributing.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.6%
  • Batchfile 0.4%