public IObservable <Unit> Insert(string key, byte[] data, DateTimeOffset?absoluteExpiration = null) { if (disposed) { return(Observable.Throw <Unit>(new ObjectDisposedException("PersistentBlobCache"))); } if (key == null || data == null) { return(Observable.Throw <Unit>(new ArgumentNullException())); } // NB: Since FetchOrWriteBlobFromDisk is guaranteed to not block, // we never sit on this lock for any real length of time lock (MemoizedRequests) { MemoizedRequests.Invalidate(key); var err = MemoizedRequests.Get(key, data); // If we fail trying to fetch/write the key on disk, we want to // try again instead of replaying the same failure err.LogErrors("Insert").Subscribe( x => CacheIndex[key] = new CacheIndexEntry(Scheduler.Now, absoluteExpiration), ex => Invalidate(key)); return(err.Select(_ => Unit.Default)); } }
public async Task CreateHashName_Success() { var post = await _postCache.Get(123456); var expectedName = post.Hash ?? ""; var actualName = _hashNamer.Name(post); Assert.AreEqual(expectedName, actualName); }
public int GetAffinityForObject(Type type, string propertyName, bool beforeChanged = false) { if (!type.FullName.ToLowerInvariant().StartsWith("system.windows.forms")) { return(0); } lock (eventInfoCache) { var ei = eventInfoCache.Get(Tuple.Create(type, propertyName)); return((beforeChanged == false && ei != null) ? 8 : 0); } }
/// <inheritdoc/> public int GetAffinityForObject(Type type, string propertyName, bool beforeChanged = false) { bool supportsTypeBinding = typeof(Component).IsAssignableFrom(type); if (!supportsTypeBinding) { return(0); } var ei = eventInfoCache.Get(Tuple.Create(type, propertyName)); return(beforeChanged == false && ei != null ? 8 : 0); }
public IObservable <byte[]> GetAsync(string key) { if (disposed) { return(Observable.Throw <byte[]>(new ObjectDisposedException("SqlitePersistentBlobCache"))); } lock (_inflightCache) { return(_inflightCache.Get(key) .Select(x => x.Value) .SelectMany(x => AfterReadFromDiskFilter(x, Scheduler)) .Finally(() => { lock (_inflightCache) { _inflightCache.Invalidate(key); } })); } }
public void TestCacheWithSingleKey() { var results = new List <int>(); var cache = new MemoizingMRUCache <Composition, int>((a, b) => { results.Add(0); return(0); }, 10); var composition = new Composition(3, 6, 0, 0, 0); Assert.True(results.Count == 0); cache.Get(composition); Assert.True(results.Count == 1); var composition2 = new Composition(3, 6, 0, 0, 0); cache.Get(composition2); Assert.True(results.Count == 1); }
public CalculatorViewModel() { _reactiveHelper = new MakeObjectReactiveHelper(this); _cache = new MemoizingMRUCache<int, int>((x, ctx) => { Thread.Sleep(1000); // Pretend this calculation isn’t cheap return x*10; }, 5); CalculateCommand = new ReactiveAsyncCommand(this.WhenAny(x => x.Number, x => x.Value > 0)); (CalculateCommand as ReactiveAsyncCommand).RegisterAsyncTask<object>(o => { return Task.Factory.StartNew(() => { int top; bool cached = _cache.TryGet( Number, out top); if (cached) { Result = 0; Thread.Sleep(1000); Result = top; } else { top = _cache.Get(Number); for (int i = 0; i <= top; i++) { Result = i; Thread.Sleep(100); } } }); }); }
public CalculatorViewModel() { _cache = new MemoizingMRUCache <int, int>((x, ctx) => { Thread.Sleep(1000); // Pretend this calculation isn’t cheap return(x * 10); }, 5); CalculateCommand = new ReactiveCommand(this.WhenAny(x => x.Number, x => x.Value > 0)); (CalculateCommand as ReactiveCommand).RegisterAsyncTask(o => { return(Task.Factory.StartNew(() => { int top; bool cached = _cache.TryGet(Number, out top); if (cached) { Result = 0; Thread.Sleep(1000); Result = top; } else { top = _cache.Get(Number); for (int i = 0; i <= top; i++) { Result = i; Thread.Sleep(100); } } })); }); }
string getPropertyValidationError(string propName) { PropertyExtraInfo pei; lock (allValidatedProperties) { if (!allValidatedProperties.Get(this.GetType()).TryGetValue(propName, out pei)) { return(null); } } foreach (var v in pei.ValidationAttributes) { try { var ctx = new ValidationContext(this, null, null) { MemberName = propName }; var pi = pei.Type.GetProperty(pei.PropertyName, BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance); v.Validate(pi.GetValue(this, null), ctx); } catch (Exception ex) { this.Log().Info("{0:X}.{1} failed validation: {2}", this.GetHashCode(), propName, ex.Message); return(ex.Message); } } return(null); }
string getPropertyValidationError(string propName) { PropertyExtraInfo pei; lock (allValidatedProperties) { if (!allValidatedProperties.Get(this.GetType()).TryGetValue(propName, out pei)) { return(null); } } foreach (var v in pei.ValidationAttributes) { try { var ctx = new ValidationContext(this, null, null) { MemberName = propName }; var getter = Reflection.GetValueFetcherForProperty(pei.Type.GetProperty(propName)); v.Validate(getter(this, new object[] {}), ctx); } catch (Exception ex) { this.Log().Info("{0:X}.{1} failed validation: {2}", this.GetHashCode(), propName, ex.Message); return(ex.Message); } } return(null); }
public static IDisposable BindCommandToObject(ICommand command, object target, IObservable <object> commandParameter, string eventName) { var type = target.GetType(); var binder = bindCommandEventCache.Get(type); if (binder == null) { throw new Exception(String.Format("Couldn't find a Command Binder for {0} and event {1}", type.FullName, eventName)); } var eventArgsType = Reflection.GetEventArgsTypeForEvent(type, eventName); var mi = binder.GetType().GetMethods().First(x => x.Name == "BindCommandToObject" && x.IsGenericMethod); mi = mi.MakeGenericMethod(new[] { eventArgsType }); //var ret = binder.BindCommandToObject<TEventArgs>(command, target, commandParameter, eventName); var ret = (IDisposable)mi.Invoke(binder, new[] { command, target, commandParameter, eventName }); if (ret == null) { throw new Exception(String.Format("Couldn't bind Command Binder for {0} and event {1}", type.FullName, eventName)); } return(ret); }
public object GetOrAdd(string filename, Func <string, object> compilationDelegate, string mimeType) { lock (_delegateIndex) { _delegateIndex[filename] = compilationDelegate; } return(_cache.Get(filename)); }
public static Action <object, object> GetValueSetterForProperty(Type type, string propName) { Contract.Requires(type != null); Contract.Requires(propName != null); lock (propReaderCache) { return(propWriterCache.Get(Tuple.Create(type, propName))); } }
public void TestCacheWithTupleKey() { var results = new List <int>(); var cache = new MemoizingMRUCache <Tuple <Composition, IonType>, int>((a, b) => { results.Add(0); return(0); }, 10); var ionTypeFactory = new IonTypeFactory(10); var composition = new Composition(3, 6, 0, 0, 0); var ionType = ionTypeFactory.GetIonType("b"); var tuple = new Tuple <Composition, IonType>(composition, ionType); Assert.True(results.Count == 0); cache.Get(tuple); Assert.True(results.Count == 1); var tuple2 = new Tuple <Composition, IonType>(composition, ionType); cache.Get(tuple2); Assert.True(results.Count == 1); }
public static IDisposable BindCommandToObject(ICommand command, object target, IObservable <object> commandParameter) { var type = target.GetType(); var binder = bindCommandCache.Get(type); if (binder == null) { throw new Exception(String.Format("Couldn't find a Command Binder for {0}", type.FullName)); } var ret = binder.BindCommandToObject(command, target, commandParameter); if (ret == null) { throw new Exception(String.Format("Couldn't bind Command Binder for {0}", type.FullName)); } return(ret); }
public CalculatorViewModel() { _cache = new MemoizingMRUCache<int, int>((x, ctx) => { Thread.Sleep(1000); // Pretend this calculation isn’t cheap return x*10; }, 5); CalculateCommand = ReactiveCommand.CreateAsyncTask<object>( this.WhenAnyValue(x => x.Number, x => x > 0), o => { return Task<object>.Factory.StartNew(() => { int top; bool cached = _cache.TryGet( Number, out top); if (cached) { Result = 0; Thread.Sleep(1000); Result = top; } else { top = _cache.Get(Number); for (int i = 0; i <= top; i++) { Result = i; Thread.Sleep(100); } } return null; }); }, RxApp.MainThreadScheduler); }
public MainViewModel() { IsInProgress = Visibility.Collapsed; LoginCommand = new ReactiveAsyncCommand(this.WhenAny(t => t.UserName, t => t.Password, (x, y) => !string.IsNullOrEmpty(x.Value) && !string.IsNullOrEmpty(y.Value))); LoginCommand.ItemsInflight.Select(x => x > 0 ? Visibility.Visible : Visibility.Collapsed).Subscribe(x => IsInProgress = x); LoginCommand.RegisterAsyncFunction(_ => _gitHubService.Login(UserName, Password)).Subscribe( u => LoggedInUser = u); this.ObservableForProperty(x => x.LoggedInUser, user => user == null ? Visibility.Hidden : Visibility.Visible).Subscribe(v => IsUserLoggedIn = v); _cache = new MemoizingMRUCache<User, Repository[]>((user,_) => _gitHubService.GetRepositories(user), 3); this.WhenAny(t => t.LoggedInUser, u => u.Value != null).Where(filter => filter).Subscribe(_ => Repositories = new ReactiveCollection<Repository>(_cache.Get(LoggedInUser)) ); }
public CalculatorViewModel() { _cache = new MemoizingMRUCache <int, int>((x, ctx) => { Thread.Sleep(1000); // Pretend this calculation isn’t cheap return(x * 10); }, 5); CalculateCommand = ReactiveCommand.CreateAsyncTask <object>( this.WhenAnyValue(x => x.Number, x => x > 0), o => { return(Task <object> .Factory.StartNew(() => { int top; bool cached = _cache.TryGet(Number, out top); if (cached) { Result = 0; Thread.Sleep(1000); Result = top; } else { top = _cache.Get(Number); for (int i = 0; i <= top; i++) { Result = i; Thread.Sleep(100); } } return null; })); }, RxApp.MainThreadScheduler); }
public CalculatorViewModel() { _cache = new MemoizingMRUCache<int, int>((x, ctx) => { Thread.Sleep(1000); // Pretend this calculation isn’t cheap return x*10; }, 5); CalculateCommand = ReactiveCommand.CreateAsyncTask(o => { return Task.Factory.StartNew(() => { int top; bool cached = _cache.TryGet( Number, out top); if (cached) { Result = 0; Thread.Sleep(1000); Result = top; } else { top = _cache.Get(Number); for (int i = 0; i <= top; i++) { Result = i; Thread.Sleep(100); } } }); }); }
public MainViewModel() { IsInProgress = Visibility.Collapsed; LoginCommand = new ReactiveAsyncCommand(this.WhenAny(t => t.UserName, t => t.Password, (x, y) => !string.IsNullOrEmpty(x.Value) && !string.IsNullOrEmpty(y.Value))); LoginCommand.ItemsInflight.Select(x => x > 0 ? Visibility.Visible : Visibility.Collapsed).Subscribe(x => IsInProgress = x); LoginCommand.RegisterAsyncFunction(_ => _gitHubService.Login(UserName, Password)).Subscribe( u => LoggedInUser = u); this.ObservableForProperty(x => x.LoggedInUser, user => user == null ? Visibility.Hidden : Visibility.Visible).Subscribe(v => IsUserLoggedIn = v); _cache = new MemoizingMRUCache <User, Repository[]>((user, _) => _gitHubService.GetRepositories(user), 3); this.WhenAny(t => t.LoggedInUser, u => u.Value != null).Where(filter => filter).Subscribe(_ => Repositories = new ReactiveCollection <Repository>(_cache.Get(LoggedInUser)) ); }
/// <summary> /// Calculate fragment ion labels. /// </summary> /// <param name="ionTypes">List of IonTypes.</param> /// <param name="labelModifications">The heavy/light labels.</param> /// <returns>A list of fragment labeled ions.</returns> public List <LabeledIonViewModel> GetFragmentLabels(IList <IonType> ionTypes, SearchModification[] labelModifications = null) { var fragmentLabelList = new List <LabeledIonViewModel> { Capacity = Sequence.Count * ionTypes.Count * Charge }; if (Sequence.Count < 1 || LcMsRun == null) { return(fragmentLabelList); } var sequence = labelModifications == null ? Sequence : IonUtils.GetHeavySequence(Sequence, labelModifications); var precursorIon = IonUtils.GetPrecursorIon(sequence, Charge); var tasks = new List <Task <object> >(); // Define a delegate that prints and returns the system tick count Func <object, List <LabeledIonViewModel> > action = (object type) => { IonType iType = (IonType)type; var ionFragments = new List <LabeledIonViewModel>(); for (var i = 1; i < Sequence.Count; i++) { var startIndex = iType.IsPrefixIon ? 0 : i; var length = iType.IsPrefixIon ? i : sequence.Count - i; var fragment = new Sequence(Sequence.GetRange(startIndex, length)); var ions = iType.GetPossibleIons(fragment); foreach (var ion in ions) { lock (cacheLock) { var labeledIonViewModel = fragmentCache.Get(new Tuple <Composition, IonType>(ion.Composition, iType)); labeledIonViewModel.Index = length; labeledIonViewModel.PrecursorIon = precursorIon; ionFragments.Add(labeledIonViewModel); } } if (!iType.IsPrefixIon) { ionFragments.Reverse(); } } return(ionFragments); }; foreach (var ionType in ionTypes) { tasks.Add(Task <object> .Factory.StartNew(action, ionType)); } Task.WaitAll(tasks.ToArray()); foreach (Task <object> task in tasks) { List <LabeledIonViewModel> list = (List <LabeledIonViewModel>)task.Result; fragmentLabelList.AddRange(list); } return(fragmentLabelList); }
public IObservable <string> Register(string directory, string filter = null) { lock (_watchCache) { return(_watchCache.Get(Tuple.Create(directory, filter))); } }
/// <summary> /// GetRootObjectType returns the Type of the root object associated with the given Sync /// Point. /// </summary> /// <returns>The Type of the root object.</returns> public static Type GetRootObjectType(this ISyncPointInformation This) { lock (_ObjectNameCache) { return(_ObjectNameCache.Get(This.RootObjectTypeName)); } }
public int GetAffinityForObject(Type type, string propertyName, bool beforeChanged = false) { lock (declaredInNSObject) { return(declaredInNSObject.Get(Tuple.Create(type, propertyName)) ? 15 : 0); } }
public static global::NLog.ILogger Resolve(Type type) { return(_loggerCache.Get(type, null)); }