/// <summary> /// Load an image from the blob cache. /// </summary> /// <param name="key">The key to look up in the cache.</param> /// <returns>A Future result representing the bitmap image. This /// Observable is guaranteed to be returned on the UI thread.</returns> public static IObservable <IBitmap> LoadImage(this IBlobCache This, string key, float?desiredWidth = null, float?desiredHeight = null) { return(This.GetAsync(key) .SelectMany(ThrowOnBadImageBuffer) .SelectMany(x => bytesToImage(x, desiredWidth, desiredHeight)) .ObserveOn(RxApp.MainThreadScheduler)); }
/// <summary> /// Load a XAML image from the blob cache. /// </summary> /// <param name="key">The key to look up in the cache.</param> /// <returns>A Future result representing the bitmap image. This /// Observable is guaranteed to be returned on the UI thread.</returns> public static IObservable <BitmapImage> LoadImage(this IBlobCache This, string key) { return(This.GetAsync(key) .SelectMany(ThrowOnBadImageBuffer) .SelectMany(BytesToImage) .ObserveOn(RxApp.DeferredScheduler)); }
/// <summary> /// Get an object from the cache and deserialize it via the JSON /// serializer. /// </summary> /// <param name="key">The key to look up in the cache.</param> /// <param name="noTypePrefix">Use the exact key name instead of a /// modified key name. If this is true, GetAllObjects will not find this object.</param> /// <returns>A Future result representing the object in the cache.</returns> public static IObservable <T> GetObjectAsync <T>(this IBlobCache This, string key, bool noTypePrefix = false) { var objCache = This as IObjectBlobCache; if (objCache != null) { return(objCache.GetObjectAsync <T>(key, noTypePrefix)); } return(This.GetAsync(key).SelectMany(DeserializeObject <T>)); }
/// <summary> /// Download data from an HTTP URL and insert the result into the /// cache. If the data is already in the cache, this returns /// a cached value. The URL itself is used as the key. /// </summary> /// <param name="url">The URL to download.</param> /// <param name="headers">An optional Dictionary containing the HTTP /// request headers.</param> /// <param name="fetchAlways">Force a web request to always be issued, skipping the cache.</param> /// <param name="absoluteExpiration">An optional expiration date.</param> /// <returns>The data downloaded from the URL.</returns> public IObservable<byte[]> DownloadUrl(IBlobCache This, string url, IDictionary<string, string> headers = null, bool fetchAlways = false, DateTimeOffset? absoluteExpiration = null) { var doFetch = new Func<KeyNotFoundException, IObservable<byte[]>>(_ => inflightWebRequests.GetOrAdd(url, __ => Observable.Defer(() => { return MakeWebRequest(new Uri(url), headers) .SelectMany(x => ProcessAndCacheWebResponse(x, url, This, absoluteExpiration)); }).Multicast(new AsyncSubject<byte[]>()).RefCount())); IObservable<byte[]> dontcare; var ret = fetchAlways ? doFetch(null) : This.GetAsync(url).Catch(doFetch); return ret.Finally(() => inflightWebRequests.TryRemove(url, out dontcare)); }
/// <summary> /// Download data from an HTTP URL and insert the result into the /// cache. If the data is already in the cache, this returns /// a cached value. The URL itself is used as the key. /// </summary> /// <param name="url">The URL to download.</param> /// <param name="headers">An optional Dictionary containing the HTTP /// request headers.</param> /// <param name="fetchAlways">Force a web request to always be issued, skipping the cache.</param> /// <param name="absoluteExpiration">An optional expiration date.</param> /// <returns>The data downloaded from the URL.</returns> public static IObservable <byte[]> DownloadUrl(this IBlobCache This, string url, Dictionary <string, string> headers = null, bool fetchAlways = false, DateTimeOffset?absoluteExpiration = null) { var doFetch = new Func <KeyNotFoundException, IObservable <byte[]> >(_ => inflightWebRequests.GetOrAdd(url, __ => Observable.Defer(() => { return(MakeWebRequest(new Uri(url), headers) .SelectMany(x => ProcessAndCacheWebResponse(x, url, This, absoluteExpiration))); }).Multicast(new AsyncSubject <byte[]>()).RefCount())); IObservable <byte[]> dontcare; var ret = fetchAlways ? doFetch(null) : This.GetAsync(url).Catch(doFetch); return(ret.Finally(() => inflightWebRequests.TryRemove(url, out dontcare))); }
public static IObservable <IDictionary <string, byte[]> > GetAsync(this IBlobCache This, IEnumerable <string> keys) { var bulkCache = This as IBulkBlobCache; if (bulkCache != null) { return(bulkCache.GetAsync(keys)); } return(keys.ToObservable() .SelectMany(x => { return This.GetAsync(x) .Select(y => new KeyValuePair <string, byte[]>(x, y)) .Catch <KeyValuePair <string, byte[]>, KeyNotFoundException>(_ => Observable.Empty <KeyValuePair <string, byte[]> >()); }) .ToDictionary(k => k.Key, v => v.Value)); }
public IObservable <byte[]> GetAsync(string key) { return(_inner.GetAsync(key)); }
/// <summary> /// Get an object from the cache and deserialize it via the JSON /// serializer. /// </summary> /// <param name="key">The key to look up in the cache.</param> /// <param name="noTypePrefix">Use the exact key name instead of a /// modified key name. If this is true, GetAllObjects will not find this object.</param> /// <returns>A Future result representing the object in the cache.</returns> public static IObservable <T> GetObjectAsync <T>(this IBlobCache This, string key, bool noTypePrefix = false) { return(This.GetAsync(noTypePrefix ? key : GetTypePrefixedKey(key, typeof(T))) .SelectMany(DeserializeObject <T>)); }