Пример #1
0
        T IContextCache.AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory)
        {
            try
            {
                T previousValue           = null;
                IContextValue <T> context = null;

                IContextCollection currentCollection;
                do
                {
                    T existingValue;
                    if (_collection.TryGetContext(out existingValue))
                    {
                        if (context == null || previousValue != existingValue)
                        {
                            context = new ContextValue <T>(updateFactory(existingValue));
                        }

                        previousValue = existingValue;

                        currentCollection = Volatile.Read(ref _collection);

                        Interlocked.CompareExchange(ref _collection, currentCollection.Add(context), currentCollection);
                    }
                    else
                    {
                        if (context == null)
                        {
                            context = new ContextValue <T>(addFactory());
                        }

                        currentCollection = Volatile.Read(ref _collection);

                        Interlocked.CompareExchange(ref _collection, currentCollection.Add(context), currentCollection);
                    }
                } while (currentCollection == Volatile.Read(ref _collection));

                return(context.Value);
            }
            catch (Exception exception)
            {
                throw new ContextFactoryException($"The payload factory faulted: {TypeCache<T>.ShortName}", exception);
            }
        }
Пример #2
0
 T IContext.AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory)
 {
     return(_context.AddOrUpdateContext(addFactory, updateFactory));
 }
Пример #3
0
 public T AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory)
     where T : class
 {
     return(ContextCache.AddOrUpdateContext(addFactory, updateFactory));
 }
Пример #4
0
 public T AddOrUpdateContext <T>(ContextFactory <T> addFactory, UpdateContextFactory <T> updateFactory)
     where T : class
 {
     throw new ContextNotFoundException();
 }