Beispiel #1
0
        public override async Task Get(HttpListenerContext httpContext, params string[] matches)
        {
            var        key = matches[0];
            CacheEntry entry;
            var        hasValue = _cache.TryGet(key, out entry);

            if (!hasValue)
            {
                httpContext.Response.StatusCode = 404;
                httpContext.Response.Close();
            }
            else
            {
                var        contentKey = string.Format("content:{0}", key);
                CacheEntry cacheEntry;
                var        hasContent = _cache.TryGet(contentKey, out cacheEntry);

                string contentType = httpContext.Request.ContentType;
                if (hasContent)
                {
                    var contentTypeBytes = cacheEntry.Data;
                    //httpContext.Request.ContentType
                    contentType = Encoding.UTF8.GetString(contentTypeBytes);
                }

                httpContext.Response.ContentType = contentType;

                var value        = entry.Data;// TODO: does this need converting, based upon content type
                var outputStream = httpContext.Response.OutputStream;
                await outputStream.WriteAsync(value, 0, value.Length /*, _cancellationTokenSource.Token*/);

                httpContext.Response.Close();
            }
        }
Beispiel #2
0
        private void Subscribe(IObserver <string> responseObserver, string key)
        {
            CacheEntry cacheEntry;

            if (_cache.TryGet(key, out cacheEntry))
            {
                var notification = new StoreNotification
                {
                    Key       = key,
                    Data      = cacheEntry.Data,
                    EventId   = cacheEntry.EventId,
                    Expiry    = cacheEntry.Expiry,
                    Flags     = cacheEntry.Flags,
                    Operation = StoreOperation.Add
                };
                var combinedNotifications = Observable.Return(notification).Combine(_cache.Notifications); // TODO: must be a simpler way of doing this...

                _subscriptions[key] = combinedNotifications.
                                      OfType <IKeyCacheNotification>().
                                      Where(k => k.Key == key).
                                      Select(JsonFromNotifications).
                                      Subscribe(responseObserver);
            }
            else
            {
                _subscriptions[key] = _cache.Notifications.
                                      OfType <IKeyCacheNotification>().
                                      Where(k => k.Key == key).
                                      Select(JsonFromNotifications).
                                      Subscribe(responseObserver);
            }
        }