Skip to content

Commit af038da

Browse files
authored
Merge pull request dotnet/extensions#2806 from MarkCBB/issue-1268
Performance improvement by cleaning code and query tuning\n\nCommit migrated from dotnet/extensions@ea2f960
2 parents e95fd5e + 8eb7a3c commit af038da

File tree

4 files changed

+3
-85
lines changed

4 files changed

+3
-85
lines changed

src/Caching/SqlServer/src/Columns.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ public static class Indexes
1919
{
2020
// The value of the following index positions is dependent on how the SQL queries
2121
// are selecting the columns.
22-
public const int CacheItemIdIndex = 0;
23-
public const int ExpiresAtTimeIndex = 1;
24-
public const int SlidingExpirationInSecondsIndex = 2;
25-
public const int AbsoluteExpirationIndex = 3;
26-
public const int CacheItemValueIndex = 4;
22+
public const int CacheItemValueIndex = 0;
2723
}
2824
}
2925
}

src/Caching/SqlServer/src/DatabaseOperations.cs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ protected virtual byte[] GetCacheItem(string key, bool includeValue)
210210
}
211211

212212
byte[] value = null;
213-
TimeSpan? slidingExpiration = null;
214-
DateTimeOffset? absoluteExpiration = null;
215-
DateTimeOffset expirationTime;
216213
using (var connection = new SqlConnection(ConnectionString))
217214
using (var command = new SqlCommand(query, connection))
218215
{
@@ -227,22 +224,6 @@ protected virtual byte[] GetCacheItem(string key, bool includeValue)
227224
{
228225
if (reader.Read())
229226
{
230-
var id = reader.GetFieldValue<string>(Columns.Indexes.CacheItemIdIndex);
231-
232-
expirationTime = reader.GetFieldValue<DateTimeOffset>(Columns.Indexes.ExpiresAtTimeIndex);
233-
234-
if (!reader.IsDBNull(Columns.Indexes.SlidingExpirationInSecondsIndex))
235-
{
236-
slidingExpiration = TimeSpan.FromSeconds(
237-
reader.GetFieldValue<long>(Columns.Indexes.SlidingExpirationInSecondsIndex));
238-
}
239-
240-
if (!reader.IsDBNull(Columns.Indexes.AbsoluteExpirationIndex))
241-
{
242-
absoluteExpiration = reader.GetFieldValue<DateTimeOffset>(
243-
Columns.Indexes.AbsoluteExpirationIndex);
244-
}
245-
246227
if (includeValue)
247228
{
248229
value = reader.GetFieldValue<byte[]>(Columns.Indexes.CacheItemValueIndex);
@@ -275,9 +256,6 @@ protected virtual byte[] GetCacheItem(string key, bool includeValue)
275256
}
276257

277258
byte[] value = null;
278-
TimeSpan? slidingExpiration = null;
279-
DateTimeOffset? absoluteExpiration = null;
280-
DateTimeOffset expirationTime;
281259
using (var connection = new SqlConnection(ConnectionString))
282260
using (var command = new SqlCommand(query, connection))
283261
{
@@ -293,24 +271,6 @@ protected virtual byte[] GetCacheItem(string key, bool includeValue)
293271
{
294272
if (await reader.ReadAsync(token).ConfigureAwait(false))
295273
{
296-
var id = await reader.GetFieldValueAsync<string>(Columns.Indexes.CacheItemIdIndex, token).ConfigureAwait(false);
297-
298-
expirationTime = await reader.GetFieldValueAsync<DateTimeOffset>(
299-
Columns.Indexes.ExpiresAtTimeIndex, token).ConfigureAwait(false);
300-
301-
if (!await reader.IsDBNullAsync(Columns.Indexes.SlidingExpirationInSecondsIndex, token).ConfigureAwait(false))
302-
{
303-
slidingExpiration = TimeSpan.FromSeconds(
304-
await reader.GetFieldValueAsync<long>(Columns.Indexes.SlidingExpirationInSecondsIndex, token).ConfigureAwait(false));
305-
}
306-
307-
if (!await reader.IsDBNullAsync(Columns.Indexes.AbsoluteExpirationIndex, token).ConfigureAwait(false))
308-
{
309-
absoluteExpiration = await reader.GetFieldValueAsync<DateTimeOffset>(
310-
Columns.Indexes.AbsoluteExpirationIndex,
311-
token).ConfigureAwait(false);
312-
}
313-
314274
if (includeValue)
315275
{
316276
value = await reader.GetFieldValueAsync<byte[]>(Columns.Indexes.CacheItemValueIndex, token).ConfigureAwait(false);

src/Caching/SqlServer/src/MonoDatabaseOperations.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ protected override byte[] GetCacheItem(string key, bool includeValue)
3535
}
3636

3737
byte[] value = null;
38-
TimeSpan? slidingExpiration = null;
39-
DateTimeOffset? absoluteExpiration = null;
40-
DateTimeOffset expirationTime;
4138
using (var connection = new SqlConnection(ConnectionString))
4239
{
4340
var command = new SqlCommand(query, connection);
@@ -51,22 +48,6 @@ protected override byte[] GetCacheItem(string key, bool includeValue)
5148

5249
if (reader.Read())
5350
{
54-
var id = reader.GetString(Columns.Indexes.CacheItemIdIndex);
55-
56-
expirationTime = DateTimeOffset.Parse(reader[Columns.Indexes.ExpiresAtTimeIndex].ToString());
57-
58-
if (!reader.IsDBNull(Columns.Indexes.SlidingExpirationInSecondsIndex))
59-
{
60-
slidingExpiration = TimeSpan.FromSeconds(
61-
reader.GetInt64(Columns.Indexes.SlidingExpirationInSecondsIndex));
62-
}
63-
64-
if (!reader.IsDBNull(Columns.Indexes.AbsoluteExpirationIndex))
65-
{
66-
absoluteExpiration = DateTimeOffset.Parse(
67-
reader[Columns.Indexes.AbsoluteExpirationIndex].ToString());
68-
}
69-
7051
if (includeValue)
7152
{
7253
value = (byte[])reader[Columns.Indexes.CacheItemValueIndex];
@@ -98,12 +79,9 @@ protected override byte[] GetCacheItem(string key, bool includeValue)
9879
}
9980

10081
byte[] value = null;
101-
TimeSpan? slidingExpiration = null;
102-
DateTimeOffset? absoluteExpiration = null;
103-
DateTimeOffset expirationTime;
10482
using (var connection = new SqlConnection(ConnectionString))
10583
{
106-
var command = new SqlCommand(SqlQueries.GetCacheItem, connection);
84+
var command = new SqlCommand(query, connection);
10785
command.Parameters
10886
.AddCacheItemId(key)
10987
.AddWithValue("UtcNow", SqlDbType.DateTime, utcNow.UtcDateTime);
@@ -116,22 +94,6 @@ protected override byte[] GetCacheItem(string key, bool includeValue)
11694

11795
if (await reader.ReadAsync(token).ConfigureAwait(false))
11896
{
119-
var id = reader.GetString(Columns.Indexes.CacheItemIdIndex);
120-
121-
expirationTime = DateTimeOffset.Parse(reader[Columns.Indexes.ExpiresAtTimeIndex].ToString());
122-
123-
if (!await reader.IsDBNullAsync(Columns.Indexes.SlidingExpirationInSecondsIndex, token).ConfigureAwait(false))
124-
{
125-
slidingExpiration = TimeSpan.FromSeconds(
126-
Convert.ToInt64(reader[Columns.Indexes.SlidingExpirationInSecondsIndex].ToString()));
127-
}
128-
129-
if (!await reader.IsDBNullAsync(Columns.Indexes.AbsoluteExpirationIndex, token).ConfigureAwait(false))
130-
{
131-
absoluteExpiration = DateTimeOffset.Parse(
132-
reader[Columns.Indexes.AbsoluteExpirationIndex].ToString());
133-
}
134-
13597
if (includeValue)
13698
{
13799
value = (byte[])reader[Columns.Indexes.CacheItemValueIndex];

src/Caching/SqlServer/src/SqlQueries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal class SqlQueries
2929
"AND (AbsoluteExpiration IS NULL OR AbsoluteExpiration <> ExpiresAtTime) ;";
3030

3131
private const string GetCacheItemFormat =
32-
"SELECT Id, ExpiresAtTime, SlidingExpirationInSeconds, AbsoluteExpiration, Value " +
32+
"SELECT Value " +
3333
"FROM {0} WHERE Id = @Id AND @UtcNow <= ExpiresAtTime;";
3434

3535
private const string SetCacheItemFormat =

0 commit comments

Comments
 (0)