我正在使用当前时钟滴答作为随机数生成的种子.随机数用于伪GUID,我的数据库中的检查将确保它在返回之前不存在.平均而言,在该过程的生命周期中,该方法将被连续调用大约10k次.
我担心的是,可能会背靠背生成相同的数字,导致对我的数据库进行多次不必要的递归调用,检查是否存在相同的ID.如果可能的话,我想避免这种情况.测试此场景的最佳方法是什么?
如果重要,应用程序是.NET 4,数据库是SQL Server 2008.
private static string GenerateUniqueDelId()
{
// Generate a random integer using the current number of clock ticks as seed.
// Then prefix number with "DEL" and date, finally padding random integer with leading zeros for a fixed 25-character total length.
int seed = (int)DateTime.Now.Ticks;
Random number = new Random(seed);
string id = string.Format("DEL{0}{1}", DateTime.Today.ToString("yyyyMMdd"), number.Next().ToString("D14"));
// Lookup record with generated ID in Sesame. If one exists, call method recursively.
string query = "SELECT * …Run Code Online (Sandbox Code Playgroud) 使用 Eloquent,如何根据chunk函数闭包内的条件终止分块?我试过返回,但这似乎只终止当前块而不是所有块。此时,我想停止从数据库中检索记录。
$query->chunk(self::CHUNK_SIZE, function ($objects) {
if (someCondition) {
// terminate chunking here
return;
}
});
Run Code Online (Sandbox Code Playgroud)