Redis其實是一個快速又簡單的Key-Value的in-memory Database(雖然你可能認為它很像cache)
使用Redis實現Cache的文章很多,所以我也不會介紹該如何實作Cache,
因為我覺得可以使用Redis實現比Cache更多且更有價值的事,
例如可以成為快速查詢Primary Key的主要資料庫、甚至可以用來作為即時分析某一METRICS都不為過,
第一篇先實現簡單Insert和Select。
using ServiceStack.Redis;
[Serializable]
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public bool Sex { get; set; }
}
private static bool InsertSampledata()
{
Console.WriteLine("start insert...");
string[] names = { "rico", "sherry", "fifi", "ricoisme", "herry", "fi" };
try
{
User _User = null;
using (RedisClient redisClient = new RedisClient("localhost", 6379))
{
var redisUser = redisClient.As<User>();
int j = 0;
for (int i = 0; i < 100; i++)
{
try
{
var userdata = redisUser .FirstOrDefault(x => x.ID == id);
if (j > 5)
j = 0;
string name = names[j];
if (userdata == null)
{
_User = new User
{
ID = i,
Name = name,
Birthday = DateTime.Now.AddMinutes(i),
Sex = (i % 3 == 0) ? true : false
};
redisUser.Store(_User);
//adding Entry to the typed entity set
//redisUser.SetEntryIfNotExists(_User.ID.ToString(), _User);
Console.WriteLine("ID {0} End", _User.ID.ToString());
j++;
}
}
catch (Exception ex)
{
Console.WriteLine("ID {0} Exception {1}", _User.ID.ToString(), ex.Message);
continue;
}
}
Console.WriteLine("End...");
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(" Exception {0}", ex.Message);
return false;
}
}
private static void GetAll()
{
try
{
using (RedisClient redisClient = new RedisClient("localhost", 6379))
{
var usersClient = redisClient.As<User>();
var users = usersClient.GetAll();
if (users != null)
{
foreach (var user in users)
{
Console.WriteLine("ID:{0} Name:{1} Birthday:{2} Sex:{3}",
user.ID.ToString(), user.Name, user.Birthday.ToString(), user.Sex.ToString());
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
private static void GetUser(string name)
{
try
{
using (RedisClient redisClient = new RedisClient("localhost", 6379))
{
var usersClient = redisClient.As<User>();
var users = usersClient.GetAll();
if (users != null)
{
var companiesFound = users.Where(x => x.Name.Contains(name));
if (companiesFound != null)
{
foreach (var user in companiesFound)
{
Console.WriteLine("ID:{0} Name:{1} Birthday:{2} Sex:{3}",
user.ID.ToString(), user.Name, user.Birthday.ToString(), user.Sex.ToString());
}
Console.WriteLine("pattern:{0} ,totals:{1}", name, companiesFound.Count().ToString());
Console.WriteLine(System.Environment.NewLine);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
private static void GetUser(DateTime startbirthday,DateTime endbirthday)
{
try
{
using (RedisClient redisClient = new RedisClient("localhost", 6379))
{
var usersClient = redisClient.As<User>();
var users = usersClient.GetAll();
if (users != null)
{
var companiesFound = users.Where(x => x.Birthday>= startbirthday
&& x.Birthday<= endbirthday);
if (companiesFound != null)
{
foreach (var user in companiesFound)
{
Console.WriteLine("ID:{0} Name:{1} Birthday:{2} Sex:{3}",
user.ID.ToString(), user.Name, user.Birthday.ToString(), user.Sex.ToString());
}
Console.WriteLine("pattern:{0}~{1} ,totals:{2}", startbirthday.Date.ToShortDateString(), endbirthday.Date.ToShortDateString(), companiesFound.Count().ToString());
Console.WriteLine(System.Environment.NewLine);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
GetUser("i");
GetUser("f");
GetUser("co");
GetUser(DateTime.Now.AddDays(-1), DateTime.Now);
Console.ReadLine();
Redis大家都稱他為資料庫的Ak-47,因為它真的夠強大、簡單又可靠。
參考
Redis — The AK-47 of Post-relational Databases