写入数据
首先,一般简单的key-value操作:
1.建立连接2.存入数据3.读取数据
RedisClient redisClient = new RedisClient("XX.XXX.XXX.XX", 6379); redisClient.Set("city","shanghai"); tb_credisClient = redisClient.Get<string>("city");
对象数据插入,存储形式以json格式存入
redisClient.Set<UserInfo>("userinfo", new UserInfo() { UserName = tb_content.Text, Age = 45 }); redisClient.Get<UserInfo>("userinfo"); public class UserInfo { public long Id { set; get; } public string UserName { get; set; } public int Age { get; set; } }
_ueditor_page_break_tag_
可以像linq to sql一样操作,还支持id自增(在以前版本As是被GetTypedClient代替的)
var cars = redisClient.As<UserInfo>(); var dansFord = new UserInfo { Id = cars.GetNextSequence(), UserName = "pool_daizhj", Age = 19 }; var beccisFord = new UserInfo { Id = cars.GetNextSequence(), UserName = "charleswu", Age = 18 }; var carsToStore = new List<UserInfo> { dansFord, beccisFord }; cars.StoreAll(carsToStore); tb_result.Text = cars.GetAll().Where(qu=>qu.Age==18).FirstOrDefault().UserName.ToString();
存储在redis上结构如图(工具RedisDesktopManager)
StoreAll和Store的区别:
前者是保存多条数据,后者单条
删除数据
key-value形式的用Remove即可
redisClient.Remove("city");
Store形式存入的如何删除呢?
cars.Delete(userinfo); cars.DeleteAll(); cars.DeleteById(10); cars.DeleteByIds(new int[]{10,12});
推荐入门教程系列:http://blog.csdn.net/qiujialongjjj/article/details/16945569
其他参考资料:
http://www.cnblogs.com/shanyou/archive/2011/11/10/2245082.html
http://www.cnblogs.com/kissdodog/p/3572084.html
http://www.cnblogs.com/daizhj/archive/2011/02/17/1956860.html
http://stackoverflow.com/questions/23838181/updating-service-stack-redis-list