Esempio n. 1
0
        // return true if this caller won the race to load whatever would go at key
        private static bool GotCompeteLock(LocalCache cache, string key)
        {
            var competeKey = key + "-cload";

            if (!cache.SetNXSync(competeKey, DateTime.UtcNow))
            {
                var x = cache.Get <DateTime>(competeKey);

                // Somebody abandoned the lock, clear it and try again
                if (DateTime.UtcNow - x > TimeSpan.FromMinutes(5))
                {
                    cache.Remove(competeKey);

                    return(GotCompeteLock(cache, key));
                }

                // Lost the lock competition
                return(false);
            }

            // winner, go do something expensive now
            return(true);
        }