public bool AddSubscription <TEvent, TEventHandler>() where TEvent : IEventBase where TEventHandler : IEventHandler <TEvent> { var eventKey = GetEventKey <TEvent>(); var handlerType = typeof(TEventHandler); if (Wrapper.Database.HashExists(EventsCacheKey, eventKey)) { var handlers = Wrapper.Unwrap <HashSet <Type> >(Wrapper.Database.HashGet(EventsCacheKey, eventKey)); if (handlers.Contains(handlerType)) { return(false); } handlers.Add(handlerType); Wrapper.Database.HashSet(EventsCacheKey, eventKey, Wrapper.Wrap(handlers)); return(true); } else { return(Wrapper.Database.HashSet(EventsCacheKey, eventKey, Wrapper.Wrap(new HashSet <Type> { handlerType }), StackExchange.Redis.When.NotExists)); } }
public bool AddSubscription <TEvent, TEventHandler>() where TEvent : class, IEventBase where TEventHandler : IEventHandler <TEvent> { var eventKey = GetEventKey <TEvent>(); var handlerType = typeof(TEventHandler); using (var redLock = RedisManager.GetRedLockClient($"eventStore_{eventKey}", 10 * 1000 / RedisManager.RedisConfiguration.LockRetryDelay)) { if (redLock.TryLock()) { if (_wrapper.Database.HashExists(_eventsCacheKey, eventKey)) { var handlers = _wrapper.Unwrap <HashSet <Type> >(_wrapper.Database.HashGet(_eventsCacheKey, eventKey)); if (handlers.Contains(handlerType)) { return(false); } handlers.Add(handlerType); _wrapper.Database.HashSet(_eventsCacheKey, eventKey, _wrapper.Wrap(handlers)); return(true); } return(_wrapper.Database.HashSet(_eventsCacheKey, eventKey, _wrapper.Wrap(new HashSet <Type> { handlerType }), StackExchange.Redis.When.NotExists)); } } return(false); }