Najważniejszą klasą jest RedisClient implementująca kilka ważnych interfejsów. Poza IDisposable są to:
- IRedisNativeClient - odpowiadający natywnemu API Redis-a
- ICacheClient - wprowadza warstwę abstrakcji dla cache'y, wykorzystywany wewnątrz ServiceStacka
- IRedisClient - wyższy poziom abstrakcji
- IRedisTypedClient - operacje na typach, dodatkowe funkcjonalności
using (IRedisNativeClient client = new RedisClient()) { client.Set("urn:messages:1", Encoding.UTF8.GetBytes("Hello C# World")); } using (IRedisNativeClient client = new RedisClient()) { var result = Encoding.UTF8.GetString(client.Get("urn:messages:1")); Console.WriteLine(result); } using (IRedisClient client = new RedisClient()) { var customerNames = client.Lists["urn:customernames"]; customerNames.Clear(); customerNames.Add("Joe"); customerNames.Add("Mary"); customerNames.Add("Bob"); } using (IRedisClient client = new RedisClient()) { var customerNames = client.Lists["urn:customernames"]; foreach (var name in customerNames) Console.WriteLine(name); }
Typowany klient daje możliwość tworzenia sekwencji zapewniających unikatowość identyfikatorów. Sekwencje takie tworzone są "per typ".
long lastId = 0; using(IRedisClient client = new RedisClient()) { var customerClient = client.GetTypedClient<Customer>(); var customer = new Customer() { Id = customerClient.GetNextSequence(), Address = "123 Main Street", Name = "Bob Green", Orders = new List<Order>() { new Order() {OrderNumber = "AB123"}, new Order() {OrderNumber = "AB124"} } }; var storedCustomer = customerClient.Store(customer); lastId = storedCustomer.Id; } using(IRedisClient client = new RedisClient()) { var customerClient = client.GetTypedClient<Customer>(); var customer = customerClient.GetById(lastId); Console.WriteLine("Got customer {0} with name {1}", customer.Id, customer.Name); }
Klient C# udostępnia też możliwość definiowania transakcji w poniższy sposób:
using(IRedisClient client = new RedisClient()) { var transaction = client.CreateTransaction(); transaction.QueueCommand(c => c.Set("abc",1)); transaction.QueueCommand(c => c.Increment("abc",1)); transaction.Commit(); var result = client.Get<int>("abc"); Console.WriteLine(result); }
Brak komentarzy:
Prześlij komentarz