@@ -35,50 +35,50 @@ public static class DistributedCacheJsonExtensions
35
35
36
36
37
37
38
- public static void SetJsonObject < T > ( this IDistributedCache cache , string key , T ? value ,
38
+ public static void SetJsonObject < T > ( this IDistributedCache cache , string key , T value ,
39
39
JsonSerializerOptions ? serializerOptions = null ) where T : class
40
40
{
41
41
cache . SetJsonObject ( key , value , new DistributedCacheEntryOptions ( ) , serializerOptions ) ;
42
42
}
43
43
44
- public static void SetJsonObject < T > ( this IDistributedCache cache , string key , T ? value ,
44
+ public static void SetJsonObject < T > ( this IDistributedCache cache , string key , T value ,
45
45
TimeSpan absoluteExpirationRelativeToNow , JsonSerializerOptions ? serializerOptions = null ) where T : class
46
46
{
47
47
cache . SetJsonObject ( key , value ,
48
48
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow } ,
49
49
serializerOptions ) ;
50
50
}
51
51
52
- public static void SetJsonObject < T > ( this IDistributedCache cache , string key , T ? value ,
52
+ public static void SetJsonObject < T > ( this IDistributedCache cache , string key , T value ,
53
53
DistributedCacheEntryOptions entryOptions , JsonSerializerOptions ? serializerOptions = null ) where T : class
54
54
{
55
- var bytes = value != null ? JsonSerializer . SerializeToUtf8Bytes ( value , serializerOptions ) : null ;
55
+ var bytes = JsonSerializer . SerializeToUtf8Bytes ( value , serializerOptions ) ;
56
56
57
57
cache . Set ( key , bytes , entryOptions ) ;
58
58
}
59
59
60
60
61
- public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T ? value ,
61
+ public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T value ,
62
62
CancellationToken token = default ) where T : class
63
63
{
64
64
return cache . SetJsonObjectAsync ( key , value , new DistributedCacheEntryOptions ( ) , null , token ) ;
65
65
}
66
66
67
- public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T ? value ,
67
+ public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T value ,
68
68
JsonSerializerOptions ? serializerOptions , CancellationToken token = default ) where T : class
69
69
{
70
70
return cache . SetJsonObjectAsync ( key , value , new DistributedCacheEntryOptions ( ) , serializerOptions , token ) ;
71
71
}
72
72
73
- public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T ? value ,
73
+ public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T value ,
74
74
TimeSpan absoluteExpirationRelativeToNow , CancellationToken token = default ) where T : class
75
75
{
76
76
return cache . SetJsonObjectAsync ( key , value ,
77
77
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow } ,
78
78
null , token ) ;
79
79
}
80
80
81
- public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T ? value ,
81
+ public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T value ,
82
82
TimeSpan absoluteExpirationRelativeToNow , JsonSerializerOptions ? serializerOptions ,
83
83
CancellationToken token = default ) where T : class
84
84
{
@@ -87,33 +87,33 @@ public static Task SetJsonObjectAsync<T>(this IDistributedCache cache, string ke
87
87
serializerOptions , token ) ;
88
88
}
89
89
90
- public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T ? value ,
90
+ public static Task SetJsonObjectAsync < T > ( this IDistributedCache cache , string key , T value ,
91
91
DistributedCacheEntryOptions entryOptions , JsonSerializerOptions ? serializerOptions = null ,
92
92
CancellationToken token = default ) where T : class
93
93
{
94
- var bytes = value != null ? JsonSerializer . SerializeToUtf8Bytes ( value , serializerOptions ) : null ;
94
+ var bytes = JsonSerializer . SerializeToUtf8Bytes ( value , serializerOptions ) ;
95
95
96
96
return cache . SetAsync ( key , bytes , entryOptions , token ) ;
97
97
}
98
98
99
99
100
100
101
- public static T GetOrSetJsonObject < T > ( this IDistributedCache cache , string key , Func < T > valueFactory ,
101
+ public static T ? GetOrSetJsonObject < T > ( this IDistributedCache cache , string key , Func < T ? > valueFactory ,
102
102
JsonSerializerOptions ? serializerOptions = null ) where T : class
103
103
{
104
104
return cache . GetOrSetJsonObject ( key , valueFactory , new DistributedCacheEntryOptions ( ) , serializerOptions ) ;
105
105
}
106
106
107
107
108
- public static T GetOrSetJsonObject < T > ( this IDistributedCache cache , string key , Func < T > valueFactory ,
108
+ public static T ? GetOrSetJsonObject < T > ( this IDistributedCache cache , string key , Func < T ? > valueFactory ,
109
109
TimeSpan absoluteExpirationRelativeToNow , JsonSerializerOptions ? serializerOptions = null ) where T : class
110
110
{
111
111
return cache . GetOrSetJsonObject < T > ( key , valueFactory ,
112
112
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow } ,
113
113
serializerOptions ) ;
114
114
}
115
115
116
- public static T GetOrSetJsonObject < T > ( this IDistributedCache cache , string key , Func < T > valueFactory ,
116
+ public static T ? GetOrSetJsonObject < T > ( this IDistributedCache cache , string key , Func < T ? > valueFactory ,
117
117
DistributedCacheEntryOptions entryOptions , JsonSerializerOptions ? serializerOptions = null ) where T : class
118
118
{
119
119
var cached = cache . GetJsonObject < T > ( key , serializerOptions ) ;
@@ -125,50 +125,53 @@ public static T GetOrSetJsonObject<T>(this IDistributedCache cache, string key,
125
125
126
126
var value = valueFactory ( ) ;
127
127
128
- cache . SetJsonObject ( key , value , entryOptions , serializerOptions ) ;
128
+ if ( value != null )
129
+ {
130
+ cache . SetJsonObject ( key , value , entryOptions , serializerOptions ) ;
131
+ }
129
132
130
133
return value ;
131
134
}
132
135
133
136
134
- public static Task < T > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
135
- Func < Task < T > > valueFactory , CancellationToken token = default ) where T : class
137
+ public static Task < T ? > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
138
+ Func < Task < T ? > > valueFactory , CancellationToken token = default ) where T : class
136
139
{
137
140
return cache . GetOrSetJsonObjectAsync ( key , valueFactory , new DistributedCacheEntryOptions ( ) , null , token ) ;
138
141
}
139
- public static Task < T > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
140
- Func < Task < T > > valueFactory , JsonSerializerOptions ? serializerOptions ,
142
+ public static Task < T ? > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
143
+ Func < Task < T ? > > valueFactory , JsonSerializerOptions ? serializerOptions ,
141
144
CancellationToken token = default ) where T : class
142
145
{
143
146
return cache . GetOrSetJsonObjectAsync ( key , valueFactory , new DistributedCacheEntryOptions ( ) , serializerOptions , token ) ;
144
147
}
145
148
146
- public static Task < T > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
147
- Func < Task < T > > valueFactory , TimeSpan absoluteExpirationRelativeToNow , JsonSerializerOptions ? serializerOptions ,
149
+ public static Task < T ? > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
150
+ Func < Task < T ? > > valueFactory , TimeSpan absoluteExpirationRelativeToNow , JsonSerializerOptions ? serializerOptions ,
148
151
CancellationToken token = default ) where T : class
149
152
{
150
153
return cache . GetOrSetJsonObjectAsync ( key , valueFactory ,
151
154
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow } ,
152
155
serializerOptions , token ) ;
153
156
}
154
157
155
- public static Task < T > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
156
- Func < Task < T > > valueFactory , TimeSpan absoluteExpirationRelativeToNow , CancellationToken token = default ) where T : class
158
+ public static Task < T ? > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
159
+ Func < Task < T ? > > valueFactory , TimeSpan absoluteExpirationRelativeToNow , CancellationToken token = default ) where T : class
157
160
{
158
161
return cache . GetOrSetJsonObjectAsync ( key , valueFactory ,
159
162
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow } ,
160
163
null , token ) ;
161
164
}
162
165
163
- public static Task < T > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
164
- Func < Task < T > > valueFactory , DistributedCacheEntryOptions entryOptions ,
166
+ public static Task < T ? > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
167
+ Func < Task < T ? > > valueFactory , DistributedCacheEntryOptions entryOptions ,
165
168
CancellationToken token = default ) where T : class
166
169
{
167
170
return cache . GetOrSetJsonObjectAsync < T > ( key , valueFactory , entryOptions , null , token ) ;
168
171
}
169
172
170
- public static async Task < T > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
171
- Func < Task < T > > valueFactory , DistributedCacheEntryOptions entryOptions ,
173
+ public static async Task < T ? > GetOrSetJsonObjectAsync < T > ( this IDistributedCache cache , string key ,
174
+ Func < Task < T ? > > valueFactory , DistributedCacheEntryOptions entryOptions ,
172
175
JsonSerializerOptions ? serializerOptions = null , CancellationToken token = default ) where T : class
173
176
{
174
177
var cached = await cache . GetJsonObjectAsync < T > ( key , serializerOptions , token ) ;
@@ -180,7 +183,10 @@ public static async Task<T> GetOrSetJsonObjectAsync<T>(this IDistributedCache ca
180
183
181
184
var value = await valueFactory ( ) ;
182
185
183
- await cache . SetJsonObjectAsync ( key , value , entryOptions , serializerOptions , token ) ;
186
+ if ( value != null )
187
+ {
188
+ await cache . SetJsonObjectAsync ( key , value , entryOptions , serializerOptions , token ) ;
189
+ }
184
190
185
191
return value ;
186
192
}
0 commit comments