Created
November 5, 2013 15:28
-
-
Save quommit/7320755 to your computer and use it in GitHub Desktop.
How to create a hash using ServiceStack.Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Collections.Generic; | |
using ServiceStack.Redis; | |
using ServiceStack.Redis.Generic; | |
using ServiceStack.Text; | |
using System.Linq; | |
namespace redisw | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
Console.WriteLine ("Hi, this is redisw!"); | |
var pic = new Picture { | |
Filename = "picture1.jpg", | |
Owner = new User { FullName = "Bruce Wayne", LoginName = "batman" }, | |
DateCreated = DateTime.Now, | |
AuthorName = "batman", | |
Location = new Location { Lat = 40.0, Lon = 15.0 } | |
}; | |
using (var pool = new PooledRedisClientManager ("localhost")) { | |
using (var cli = pool.GetClient ()) { | |
//Uncomment if you want a JSON string value instead | |
//var pics = cli.As<Picture> (); | |
//pics.SetEntry ("1", pic); | |
//Create a hash just by setting each entry. | |
//Entries come from a Dictionary<string, string> | |
var hpic = pic.ToPlainDictionary (); | |
foreach (var key in hpic.Keys) { | |
cli.SetEntryInHash ("2", key, hpic[key]); | |
} | |
} | |
} | |
Console.WriteLine ("Done!"); | |
} | |
class Picture | |
{ | |
public string Filename { get; set; } | |
public User Owner { get; set; } | |
public DateTime DateCreated { get; set; } | |
public string AuthorName { get; set; } | |
public Location Location { get; set; } | |
public Dictionary<string, string> ToPlainDictionary () { | |
return new Dictionary<string, string> { | |
{ "filename", Filename }, | |
{ "owner.fullname", Owner.FullName }, | |
{ "owner.loginname", Owner.LoginName }, | |
{ "datecreated", DateCreated.ToUnixTime ().ToString (CultureInfo.InvariantCulture) }, | |
{ "authorname", AuthorName }, | |
{ "location.lon", Location.Lon.ToString (CultureInfo.InvariantCulture) }, | |
{ "location.lat", Location.Lat.ToString (CultureInfo.InvariantCulture) } | |
}; | |
} | |
} | |
class User | |
{ | |
public string FullName { get; set; } | |
public string LoginName {get; set; } | |
} | |
class Location | |
{ | |
public double Lon { get; set; } | |
public double Lat { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment