STSdb 3.5是一个开源的key-value存储形式的数据库,它是用微软.net框架C#语言编写的。STSdb 3.5尤其使用于紧急任务或实时系统,如:股市交易,电子通信,实验室数据等,它的主要功能是能够处理大量数据流。
下面用C#对stsdb3.5进行简单封装,使之可以实现数据的简单CURD功能。
需要用到的依赖包:
stsdb3.5 http://stssoft.com/downloads/
Newtonsoft.JSON https://www.newtonsoft.com/json
先实现一个数据保存类对象, 该对象可以有判断数据是否超时的功能,在必要的时候可以用到:
/// <summary> /// STSdb3的数据对象 /// </summary> /// <typeparam name="T"></typeparam> class Stsdb3Data<T> { /// <summary> /// 唯一标识,不能为空 /// </summary> public string Key { get; set; } /// <summary> /// 数据值 /// </summary> public T Value { get; set; } private DateTime _dtc = DateTime.Now; /// <summary> /// 构造时间 /// </summary> public DateTime dtc { get{return _dtc;} set{_dtc =value;} } private int _expiry = 0; /// <summary> /// 超时时间(秒), 默认0永不超时 /// </summary> public int expiry{ get {return _expiry;} set {_expiry = value;} } /// <summary> /// 构造函数 /// </summary> public Stsdb3Data() { } /// <summary> /// 构造函数,提供默认值 /// </summary> /// <param name="Value"></param> public Stsdb3Data(T Value) { this.Value = Value; } /// <summary> /// 判断是否已经超时 /// </summary> /// <returns></returns> public bool IsExpired() { if ( this.expiry<=0 ) { return false; } return (DateTime.Now - this.dtc).Seconds > this.expiry; } }
操作服务类Service:
/// <summary> /// STSdb3本地NoSQL数据库实现类 /// <para>author: lcs 1125271180@qq.com</para> /// </summary> public class Stsdb3Service { public static string dbpath { get; set; } public Stsdb3Service() { if (string.IsNullOrEmpty(dbpath)) { dbpath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"vs2008stsdb3.dat"; } } /// <summary> /// 保存数据对象 /// </summary> /// <typeparam name="T">数据对象包含的数据</typeparam> /// <param name="key"></param> /// <param name="data">数据对象</param> /// <param name="tabname">保存表名</param> /// <param name="expiry">超时时长(秒)</param> public void Put<T>(string key, T t, string tabname, int expiry) { using (StorageEngine engine = StorageEngine.FromFile(dbpath)) { var locator = new Locator(tabname); var table = engine.Scheme.CreateOrOpenXTable<string, string>(locator); var data = new Stsdb3Data<T>() { Key=key,Value=t,expiry=expiry}; table[key] = JsonConvert.SerializeObject(data); table.Commit(); engine.Scheme.Commit(); table.Close(); engine.Dispose(); } } /// <summary> /// 保存数据对象 /// </summary> /// <typeparam name="T">数据对象包含的数据</typeparam> /// <param name="key"></param> /// <param name="t">数据对象</param> /// <param name="tabname">保存表名</param> public void Put<T>(string key, T t, string tabname) { Put<T>(key, t, tabname, 0); } /// <summary> /// 获取数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="tabname"></param> /// <returns></returns> public T Get<T>(string key, string tabname) { string json = string.Empty; T rest = default(T); using (StorageEngine engine = StorageEngine.FromFile(dbpath)) { var locator = new Locator(tabname); var table = engine.Scheme.CreateOrOpenXTable<string, string>(locator); try { var item = table.FirstOrDefault(x => x.Key == key); json = item.Record != null ? item.Record : json; var data = JsonConvert.DeserializeObject<Stsdb3Data<T>>(json); if (data.IsExpired()) { Delete(key, tabname); //删除无效的历史数据 } else { rest = data.Value; } } catch { // } table.Close(); engine.Dispose(); } return rest; } /// <summary> /// 删除数据 /// </summary> /// <param name="key"></param> /// <param name="tabname"></param> public void Delete(string key, string tabname) { using (StorageEngine engine = StorageEngine.FromFile(dbpath)) { var locator = new Locator(tabname); var table = engine.Scheme.CreateOrOpenXTable<string, string>(locator); if (table != null) { table.Delete(key); table.Commit(); table.Close(); } engine.Dispose(); } } }
在以上两个类都实现后,就可以进行测试了.
测试字符串代码如下:
// 初始化 Stsdb3Service service = new Stsdb3Service(); // 保存字符串 string key = "vs2008:k001"; string val = "我的数据 vs2008"; string tabname = "vs2008"; service.Put<string>(key, val, tabname); // 读取保存的字符串 string rest = service.Get<string>(key, tabname); MessageBox.Show("" + rest); //Delete service.Delete(key, tabname); //再次读取 rest = service.Get<string>(key, tabname); if (rest != null) { MessageBox.Show("" + rest); } else { MessageBox.Show("not any value"); }
测试自定义对象:
Stsdb3Service service = new Stsdb3Service(); //测试对象 string bar = "6912345"; Book book = new Book() { bar = bar, name = "书本名", dtc = DateTime.Now }; string bookTabname = "books"; //保存对象 service.Put<Book>(book.bar, book, bookTabname); // 读取对象 Book rest = service.Get<Book>(bar, bookTabname); if (rest == null) { MessageBox.Show("rest: no book ..."); } else { MessageBox.Show("rest: book: " + JsonConvert.SerializeObject(rest)); }
补充Book类对象:
public class Book { public string bar { get; set; } public string name { get; set; } public DateTime dtc { get; set; } }