using System;
using EasyCaching.InMemory;
using Microsoft.Extensions.DependencyInjection;
using Util.Caches;
using Util.Caches.EasyCaching;
using Util.Locks.Default;
namespace Util.Tests.Locks {
///
/// 业务锁测试服务
///
public class LockTestService {
///
/// 缓存
///
private static readonly ICache Cache;
///
/// 业务锁
///
private readonly DefaultLock _lock;
///
/// 初始化业务锁测试服务
///
static LockTestService() {
var services = new ServiceCollection();
services.AddCache( options => options.UseInMemory() );
var serviceProvider = services.BuildServiceProvider();
Cache = serviceProvider.GetService();
}
///
/// 初始化业务锁测试服务
///
public LockTestService() {
_lock = new DefaultLock( Cache );
}
///
/// 执行
///
public string Execute( string key, TimeSpan? expiration = null ) {
var result = _lock.Lock( key, expiration );
return result ? "ok" : "fail";
}
///
/// 解锁
///
public void UnLock() {
_lock.UnLock();
}
}
}