Example #1
0
        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 IObservable <Unit> Insert(string key, byte[] data, DateTimeOffset?absoluteExpiration = null)
        {
            if (disposed)
            {
                return(Observable.Throw <Unit>(new ObjectDisposedException("SqlitePersistentBlobCache")));
            }
            lock (_inflightCache) _inflightCache.Invalidate(key);

            var element = new CacheElement()
            {
                Expiration = absoluteExpiration != null ? absoluteExpiration.Value.UtcDateTime : DateTime.MaxValue,
                Key        = key,
            };

            var ret = BeforeWriteToDiskFilter(data, Scheduler)
                      .Do(x => element.Value = x)
                      .SelectMany(x => _connection.InsertAsync(element, "OR REPLACE", typeof(CacheElement)).Select(_ => Unit.Default))
                      .Multicast(new AsyncSubject <Unit>());

            ret.Connect();
            return(ret);
        }