public IObservable <Unit> InsertObject <T>(string key, T value, DateTimeOffset?absoluteExpiration = null)
        {
            if (disposed)
            {
                return(Observable.Throw <Unit>(new ObjectDisposedException("SqlitePersistentBlobCache")));
            }

            var data = SerializeObject(value);

            lock (_inflightCache) _inflightCache.Invalidate(key);

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

            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);
        }
Example #2
0
        public Func <IEnumerable <CacheElement> > PrepareToExecute(IEnumerable <string> toSelect)
        {
            var selectList = toSelect.ToList();

            if (selectList.Count == 0)
            {
                return(() => new List <CacheElement>());
            }

            var selectOp = selectOps[selectList.Count - 1];
            var now      = sched.Now;

            return(() =>
            {
                var result = new List <CacheElement>();
                try
                {
                    for (int i = 0; i < selectList.Count; i++)
                    {
                        this.Checked(raw.sqlite3_bind_text(selectOp, i + 1, selectList[i]));
                    }

                    while (this.Checked(raw.sqlite3_step(selectOp)) == SQLite3.Result.Row)
                    {
                        var ce = new CacheElement()
                        {
                            Key = raw.sqlite3_column_text(selectOp, 0),
                            TypeName = raw.sqlite3_column_text(selectOp, 1),
                            Value = raw.sqlite3_column_blob(selectOp, 2),
                            Expiration = new DateTime(raw.sqlite3_column_int64(selectOp, 3)),
                            CreatedAt = new DateTime(raw.sqlite3_column_int64(selectOp, 4)),
                        };

                        if (now.UtcTicks <= ce.Expiration.Ticks)
                        {
                            result.Add(ce);
                        }
                    }
                }
                finally
                {
                    this.Checked(raw.sqlite3_reset(selectOp));
                }

                return result;
            });
        }