GDataDB:并发连接&;谷歌文档
本文关键字:谷歌 文档 amp 并发 连接 GDataDB | 更新日期: 2023-09-27 18:26:30
我使用GDataDB作为在线INI文件来保存用户在"云"上的设置。我使用如下的单一电子表格:
username create update expire amount
3600001 20120303 20120303 20180303 9
3600001 20120303 20120303 20160303 9
3600020 20120301 20120303 20190505 14
我想知道在并发写入/更新时会发生什么。我的意思是它安全吗?你觉得怎么样?
以下是代码:正如你所看到的,同时访问该文件的机会很低,但我要求将来参考,看看它是否可以用于繁重的读写操作。或者我可以实现锁定机制吗?
public class License
{
public string username { get; set; }
public string create { get; set; }
public string update { get; set; }
public string expire { get; set; }
public double amount { get; set; }
}
private static void Main(string[] args) {
string myaccount = "xxxxxxxxxx@gmail.com";
string mypass = "xxxxxxxxxxxxxxxx";
string spreadsheet = "licence";
string username = "03600001";
double amount = 9.0d; //bucks
int extend = 1; //year
// create the DatabaseClient passing my Gmail or Google Apps credentials
IDatabaseClient client = new DatabaseClient(myaccount, mypass);
// get or create the database. This is the spreadsheet file
IDatabase db = client.GetDatabase(spreadsheet) ?? client.CreateDatabase(spreadsheet);
// get or create the table. This is a worksheet in the file
// note I am using my Person object so it knows what my schema needs to be
// for my data. It will create a header row with the property names
ITable<License> table = db.GetTable<License>(spreadsheet) ?? db.CreateTable<License>(spreadsheet);
var license = new License();
IList<IRow<License>> rows = table.FindStructured(String.Format("username={0}", username)).OrderByDescending(o => o.Element.expire).Take(1).ToList();
if (rows == null || rows.Count == 0)
{
//add new
license.username = username;
license.create = DateTime.Now.ToString("yyyyMMdd");
license.update = license.create;
license.expire = DateTime.Now.AddYears(extend).ToString("yyyyMMdd");
license.amount = amount;
table.Add(license);
}
else
{
//update
IRow<License> row = rows[0];
DateTime expire = DateTime.ParseExact(row.Element.expire, "yyyyMMdd", null);
row.Element.expire = expire.AddYears(extend).ToString("yyyyMMdd");
row.Element.update = DateTime.Now.ToString("yyyyMMdd");
row.Element.amount = amount;
row.Update();
}
}
据我所知,应该没有问题。当然,由于没有内置的锁定机制,所以对特定单元格的最后一次更新将获胜,除非您自己实现锁定机制。