Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Demis Bellot
file 124 lines (100 sloc) 3.485 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
//
// ServiceStack.Redis: ECMA CLI Binding to the Redis key-value storage system
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2013 Service Stack LLC. All Rights Reserved.
//
// Licensed under the same terms of reddis and ServiceStack: new BSD license.
//

using System;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using ServiceStack.Common.Tests.Models;
using ServiceStack.Redis.Generic;
using ServiceStack.Text;

namespace ServiceStack.Redis.Tests
{
[TestFixture]
public class ShippersExample
{

public class Shipper
{
public long Id { get; set; }
public string CompanyName { get; set; }
public DateTime DateCreated { get; set; }
public ShipperType ShipperType { get; set; }
public Guid UniqueRef { get; set; }
}

static void Dump<T>(string message, T entity)
{
var text = TypeSerializer.SerializeToString(entity);

//make it a little easier on the eyes
var prettyLines = text.Split(new[] { "[", "},{", "]" },
StringSplitOptions.RemoveEmptyEntries)
.ToList().ConvertAll(x => x.Replace("{", "").Replace("}", ""));

Debug.WriteLine("\n" + message);
foreach(var l in prettyLines) Debug.WriteLine(l);
}

[Test]
public void Shippers_UseCase()
{
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
//Create a 'strongly-typed' API that makes all Redis Value operations to apply against Shippers
IRedisTypedClient<Shipper> redis = redisClient.As<Shipper>();

//Redis lists implement IList<T> while Redis sets implement ICollection<T>
var currentShippers = redis.Lists["urn:shippers:current"];
var prospectiveShippers = redis.Lists["urn:shippers:prospective"];

currentShippers.Add(
new Shipper {
Id = redis.GetNextSequence(),
CompanyName = "Trains R Us",
DateCreated = DateTime.UtcNow,
ShipperType = ShipperType.Trains,
UniqueRef = Guid.NewGuid()
});

currentShippers.Add(
new Shipper {
Id = redis.GetNextSequence(),
CompanyName = "Planes R Us",
DateCreated = DateTime.UtcNow,
ShipperType = ShipperType.Planes,
UniqueRef = Guid.NewGuid()
});

var lameShipper = new Shipper {
Id = redis.GetNextSequence(),
CompanyName = "We do everything!",
DateCreated = DateTime.UtcNow,
ShipperType = ShipperType.All,
UniqueRef = Guid.NewGuid()
};

currentShippers.Add(lameShipper);

Dump("ADDED 3 SHIPPERS:", currentShippers);

currentShippers.Remove(lameShipper);

Dump("REMOVED 1:", currentShippers);

prospectiveShippers.Add(
new Shipper {
Id = redis.GetNextSequence(),
CompanyName = "Trucks R Us",
DateCreated = DateTime.UtcNow,
ShipperType = ShipperType.Automobiles,
UniqueRef = Guid.NewGuid()
});

Dump("ADDED A PROSPECTIVE SHIPPER:", prospectiveShippers);

redis.PopAndPushItemBetweenLists(prospectiveShippers, currentShippers);

Dump("CURRENT SHIPPERS AFTER POP n' PUSH:", currentShippers);
Dump("PROSPECTIVE SHIPPERS AFTER POP n' PUSH:", prospectiveShippers);

var poppedShipper = redis.PopItemFromList(currentShippers);
Dump("POPPED a SHIPPER:", poppedShipper);
Dump("CURRENT SHIPPERS AFTER POP:", currentShippers);

//reset sequence and delete all lists
redis.SetSequence(0);
redis.RemoveEntry(currentShippers, prospectiveShippers);
Dump("DELETING CURRENT AND PROSPECTIVE SHIPPERS:", currentShippers);
}

}

}
}
Something went wrong with that request. Please try again.