您的位置:首页 > 数据库 > Redis

C#操作Redis String字符串

2018-02-05 10:47 225 查看
/// <summary>
/// Redis String 操作
/// </summary>
public static void Redis_String()
{
RedisClient client = new RedisClient("127.0.0.1", 6379);
//清空数据库缓存,慎用
client.FlushAll();
/*
* 注意这个exp的时间,之前以为是以毫秒计算,所以设置一天过期的话只写了86400000,
* 然而,他这里的最小单位似乎是。。100ns。。也就是1个ticks=100毫微秒=100纳秒。
* 所以应该写成864000000000表示一天。
* TimeSpan exp = new TimeSpan(864000000000);
* 1秒=10000000ns
*/
#region string

long second = 864000000000;
TimeSpan exp = new TimeSpan((long)second);
//设置有效期
client.Add<string>("StringValueTime", "设置时间为一天有效期", exp);

//不设置有效期
client.Add<string>("StringValue", "我是永久的");
Console.WriteLine("取值{0}", client.Get<string>("StringValue"));

//由于redis不支持对象,C#DLL底层实现是,将当前对象传递进去后,自动序列化为json字符串进行存储
// 获取后反序列化
Student stud = new Student() { id = "1001", name = "李四" };
client.Add<Student>("StringEntity", stud);

Student Get_stud = client.Get<Student>("StringEntity");
Console.WriteLine("数据类型为:String.键:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name);
#endregion
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: