Exemplo n.º 1
0
        /// <returns>Returns null if no matching item is in the cache.</returns>
        public async Task<string> Get(string markdown, string context, string mode)
        {
            if (markdown == null) throw new ArgumentNullException(nameof(markdown));
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (mode == null) throw new ArgumentNullException(nameof(mode));

            await Initialize();

            var key = ComputeHash(markdown, context, mode);

            using (var conn = await GetOpenConnectionAsync())
            {
                // After https://github.com/aspnet/Microsoft.Data.Sqlite/pull/172 is published to the
                // Microsoft.Data.SQLite package we can update the package and use this code instead:
                // var result = await conn.ExecuteScalarAsync<string>(
                //     $"select Contents from {CacheTableName} where Key = @Key",
                //     new { Key = key });

                // Using a cast since this code should be removable in the future:
                var cmd = new SqliteCommand($"select Contents from {CacheTableName} where Key = @Key", (SqliteConnection)conn);
                cmd.Parameters.AddWithValue("@Key", key);

                var result = (string)(await cmd.ExecuteScalarAsync());

                if (!string.IsNullOrEmpty(result))
                {
                    return result;
                }
            }

            return null;
        }