This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
DistributedCacheExtensions.cs
163 lines (152 loc) · 7.88 KB
/
DistributedCacheExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.Caching.Distributed
{
/// <summary>
/// Extension methods for setting data in an <see cref="IDistributedCache" />.
/// </summary>
public static class DistributedCacheExtensions
{
/// <summary>
/// Sets a sequence of bytes in the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to store the data in.</param>
/// <param name="value">The data to store in the cache.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="key"/> or <paramref name="value"/> is null.</exception>
public static void Set(this IDistributedCache cache, string key, byte[] value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
cache.Set(key, value, new DistributedCacheEntryOptions());
}
/// <summary>
/// Asynchronously sets a sequence of bytes in the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to store the data in.</param>
/// <param name="value">The data to store in the cache.</param>
/// <param name="token">Optional. A <see cref="CancellationToken" /> to cancel the operation.</param>
/// <returns>A task that represents the asynchronous set operation.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="key"/> or <paramref name="value"/> is null.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, byte[] value, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return cache.SetAsync(key, value, new DistributedCacheEntryOptions(), token);
}
/// <summary>
/// Sets a string in the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to store the data in.</param>
/// <param name="value">The data to store in the cache.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="key"/> or <paramref name="value"/> is null.</exception>
public static void SetString(this IDistributedCache cache, string key, string value)
{
cache.SetString(key, value, new DistributedCacheEntryOptions());
}
/// <summary>
/// Sets a string in the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to store the data in.</param>
/// <param name="value">The data to store in the cache.</param>
/// <param name="options">The cache options for the entry.</param>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="key"/> or <paramref name="value"/> is null.</exception>
public static void SetString(this IDistributedCache cache, string key, string value, DistributedCacheEntryOptions options)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
cache.Set(key, Encoding.UTF8.GetBytes(value), options);
}
/// <summary>
/// Asynchronously sets a string in the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to store the data in.</param>
/// <param name="value">The data to store in the cache.</param>
/// <param name="token">Optional. A <see cref="CancellationToken" /> to cancel the operation.</param>
/// <returns>A task that represents the asynchronous set operation.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="key"/> or <paramref name="value"/> is null.</exception>
public static Task SetStringAsync(this IDistributedCache cache, string key, string value, CancellationToken token = default(CancellationToken))
{
return cache.SetStringAsync(key, value, new DistributedCacheEntryOptions(), token);
}
/// <summary>
/// Asynchronously sets a string in the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to store the data in.</param>
/// <param name="value">The data to store in the cache.</param>
/// <param name="options">The cache options for the entry.</param>
/// <param name="token">Optional. A <see cref="CancellationToken" /> to cancel the operation.</param>
/// <returns>A task that represents the asynchronous set operation.</returns>
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="key"/> or <paramref name="value"/> is null.</exception>
public static Task SetStringAsync(this IDistributedCache cache, string key, string value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return cache.SetAsync(key, Encoding.UTF8.GetBytes(value), options, token);
}
/// <summary>
/// Gets a string from the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to get the stored data for.</param>
/// <returns>The string value from the stored cache key.</returns>
public static string GetString(this IDistributedCache cache, string key)
{
var data = cache.Get(key);
if (data == null)
{
return null;
}
return Encoding.UTF8.GetString(data, 0, data.Length);
}
/// <summary>
/// Asynchronously gets a string from the specified cache with the specified key.
/// </summary>
/// <param name="cache">The cache in which to store the data.</param>
/// <param name="key">The key to get the stored data for.</param>
/// <param name="token">Optional. A <see cref="CancellationToken" /> to cancel the operation.</param>
/// <returns>A task that gets the string value from the stored cache key.</returns>
public static async Task<string> GetStringAsync(this IDistributedCache cache, string key, CancellationToken token = default(CancellationToken))
{
var data = await cache.GetAsync(key, token);
if (data == null)
{
return null;
}
return Encoding.UTF8.GetString(data, 0, data.Length);
}
}
}