Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit fc22a93

Browse files
committed
Null checks and hardening
1 parent 3278b97 commit fc22a93

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/DefaultResponseCacheEntrySerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,17 @@ public static void Write(BinaryWriter writer, IResponseCacheEntry entry)
108108

109109
if (entry is CachedResponse)
110110
{
111-
writer.Write(nameof(CachedResponse));
112111
WriteCachedResponse(writer, entry as CachedResponse);
113112
}
114113
else if (entry is CachedVaryBy)
115114
{
116-
writer.Write(nameof(CachedVaryBy));
117115
WriteCachedVaryBy(writer, entry as CachedVaryBy);
118116
}
119117
}
120118

121119
private static void WriteCachedResponse(BinaryWriter writer, CachedResponse entry)
122120
{
121+
writer.Write(nameof(CachedResponse));
123122
writer.Write(entry.StatusCode);
124123
writer.Write(entry.Headers.Count);
125124
foreach (var header in entry.Headers)
@@ -134,6 +133,7 @@ private static void WriteCachedResponse(BinaryWriter writer, CachedResponse entr
134133

135134
private static void WriteCachedVaryBy(BinaryWriter writer, CachedVaryBy entry)
136135
{
136+
writer.Write(nameof(CachedVaryBy));
137137
writer.Write(entry.Headers);
138138
}
139139
}
Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.Extensions.Caching.Distributed;
56

67
namespace Microsoft.AspNetCore.ResponseCaching
@@ -11,30 +12,50 @@ public class DistributedResponseCache : IResponseCache
1112

1213
public DistributedResponseCache(IDistributedCache cache)
1314
{
15+
if (cache == null)
16+
{
17+
throw new ArgumentNullException(nameof(cache));
18+
}
19+
1420
_cache = cache;
1521
}
1622

1723
public IResponseCacheEntry Get(string key)
1824
{
19-
var serializedEntry = _cache.Get(key);
20-
if (serializedEntry != null)
25+
try
2126
{
22-
return DefaultResponseCacheSerializer.Deserialize(serializedEntry);
27+
return DefaultResponseCacheSerializer.Deserialize(_cache.Get(key));
2328
}
24-
else
29+
catch
2530
{
31+
// TODO: Log error
2632
return null;
2733
}
2834
}
2935

3036
public void Remove(string key)
3137
{
32-
_cache.Remove(key);
38+
try
39+
{
40+
_cache.Remove(key);
41+
}
42+
catch
43+
{
44+
// TODO: Log error
45+
}
3346
}
3447

48+
// TODO: Set expiry policy in the underlying cache?
3549
public void Set(string key, IResponseCacheEntry entry)
3650
{
37-
_cache.Set(key, DefaultResponseCacheSerializer.Serialize(entry));
51+
try
52+
{
53+
_cache.Set(key, DefaultResponseCacheSerializer.Serialize(entry));
54+
}
55+
catch
56+
{
57+
// TODO: Log error
58+
}
3859
}
3960
}
4061
}

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/IResponseCacheEntry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
55
{
66
public interface IResponseCacheEntry
77
{
8+
// TODO: Retain this in case we cache by a hash of the key instead.
89
string Key { get; set; }
910
}
1011
}
Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.Extensions.Caching.Memory;
56

67
namespace Microsoft.AspNetCore.ResponseCaching
@@ -11,22 +12,49 @@ public class MemoryResponseCache : IResponseCache
1112

1213
public MemoryResponseCache(IMemoryCache cache)
1314
{
15+
if (cache == null)
16+
{
17+
throw new ArgumentNullException(nameof(cache));
18+
}
19+
1420
_cache = cache;
1521
}
1622

1723
public IResponseCacheEntry Get(string key)
1824
{
19-
return _cache.Get<IResponseCacheEntry>(key);
25+
try
26+
{
27+
return _cache.Get<IResponseCacheEntry>(key);
28+
}
29+
catch
30+
{
31+
// TODO: Log error
32+
return null;
33+
}
2034
}
2135

2236
public void Remove(string key)
2337
{
24-
_cache.Remove(key);
38+
try
39+
{
40+
_cache.Remove(key);
41+
}
42+
catch
43+
{
44+
// TODO: Log error
45+
}
2546
}
2647

2748
public void Set(string key, IResponseCacheEntry entry)
2849
{
29-
_cache.Set(key, entry);
50+
try
51+
{
52+
_cache.Set(key, entry);
53+
}
54+
catch
55+
{
56+
// TODO: Log error
57+
}
3058
}
3159
}
3260
}

0 commit comments

Comments
 (0)