Пример #1
0
        private void AddContentImpl(string key, byte[] content, DateTime? expires)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "key must contain a value.");
            }

            if (content == null)
            {
                throw new ArgumentNullException("content", "content cannot be null.");
            }

            DateTime now = DateTime.UtcNow;

            CacheItem item = new CacheItem()
            {
                ExpireDate = expires,
                FirstAccessDate = now,
                Key = key.ToUpperInvariant(),
                LastAccessDate = now,
                Size = content.Length
            };

            using (SQLiteConnection connection = this.storage.CreateAndOpenConnection())
            {
                using (SQLiteTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        lock (this.syncRoot)
                        {
                            this.RemoveContent(item.Key, connection, transaction);
                            CacheStatistics stats = SqliteCache.InsertItem(item, connection, transaction);
                            this.storage.StoreContent(item.Key, content);

                            this.itemCount = stats.ItemCount;
                            this.size = stats.Size;

                            if (this.maxSize > 0)
                            {
                                this.EvictToSize(this.maxSize, connection, transaction);
                            }

                            transaction.Commit();
                        }
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Пример #2
0
        private static CacheStatistics InsertItem(CacheItem item, SQLiteConnection connection, SQLiteTransaction transaction)
        {
            const string Sql =
            @"INSERT INTO [ParlayItem]([ExpireDate],[FirstAccessDate],[Key],[LastAccessDate],[Size])
            VALUES(@ExpireDate,@FirstAccessDate,@Key,@LastAccessDate,@Size);

            UPDATE [ParlayStatistics]
            SET
            [ItemCount] = [ItemCount] + 1,
            [Size] = [Size] + @Size;

            SELECT *
            FROM [ParlayStatistics];";

            using (SQLiteCommand command = connection.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = Sql;
                command.Transaction = transaction;
                command.Parameters.AddWithValue("@ExpireDate", item.ExpireDate);
                command.Parameters.AddWithValue("@FirstAccessDate", item.FirstAccessDate);
                command.Parameters.AddWithValue("@Key", item.Key);
                command.Parameters.AddWithValue("@LastAccessDate", item.LastAccessDate);
                command.Parameters.AddWithValue("@Size", item.Size);

                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    reader.Read();

                    return new CacheStatistics()
                    {
                        ItemCount = Convert.ToInt64(reader["ItemCount"], CultureInfo.InvariantCulture),
                        Size = Convert.ToInt64(reader["Size"], CultureInfo.InvariantCulture)
                    };
                }
            }
        }