public TResult Get <TResult>(string key, Func <AcquireContext <string>, TResult> acquire, Func <TResult> fallback, TimeSpan timeout) { // When using with arbitrary types, key.ToString() could lead to errors if the key is not string or the ToString() is not properly implemented. // That's why we only allow string keys here. try { return(_cacheManager.Get(key, ctx => { using (var lockFile = _lockFileManager.AcquireLock(key, timeout)) { // If we waited for the lock to be released, here the result computed by the locking code should be returned. return _cacheManager.Get(key, acquire); } })); } catch (TimeoutException) { return(fallback()); } }
/// <summary> /// Tries to acquire a lock with the specified parameters /// </summary> /// <param name="name">Unique name of the lock</param> /// <returns>The ILockFile instance on success.</returns> /// <exception cref="System.TimeoutException">Thrown if the lock couldn't be acquired.</exception> public static ILockFile AcquireLock(this ILockFileManager lockFileManager, string name) { return(lockFileManager.AcquireLock(name, new TimeSpan(0, 0, 0, 4))); }