本文整理汇总了C#中ICache.Set方法的典型用法代码示例。如果您正苦于以下问题:C# ICache.Set方法的具体用法?C# ICache.Set怎么用?C# ICache.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICache
的用法示例。
在下文中一共展示了ICache.Set方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: _fetch
private async Task<JToken> _fetch(string url, ILogger logger, ICache cache) {
var response = await this.client.GetAsync(url);
var body = await response.Content.ReadAsStringAsync();
switch (response.StatusCode) {
case HttpStatusCode.OK:
var json = JToken.Parse (body);
var maxAgeValue = "";
IEnumerable<string> maxAgeValues;
if (response.Headers.TryGetValues("max-age", out maxAgeValues))
{
maxAgeValue = maxAgeValues.FirstOrDefault();
}
var maxAge = maxAgeRe.Match (maxAgeValue);
if (maxAge.Success) {
long ttl = long.Parse (maxAge.Groups [1].Value);
cache.Set (url, ttl, json);
}
return json;
case HttpStatusCode.Unauthorized:
var errorText = (string)JObject.Parse(body)["error"];
if (errorText == "Invalid access token") {
throw new Error(Error.ErrorCode.INVALID_TOKEN, errorText);
} else {
throw new Error(Error.ErrorCode.AUTHORIZATION_NEEDED, errorText);
}
default:
throw new Error (Error.ErrorCode.UNEXPECTED, body);
}
}
示例2: Get
/**
* Entry point to get an {@link Api} object.
* Example: <code>API api = API.get("https://lesbonneschoses.prismic.io/api", null, new Cache.BuiltInCache(999), new Logger.PrintlnLogger());</code>
*
* @param endpoint the endpoint of your prismic.io content repository, typically https://yourrepoid.prismic.io/api
* @param accessToken Your Oauth access token if you wish to use one (to access future content releases, for instance)
* @param cache instance of a class that implements the {@link Cache} interface, and will handle the cache
* @param logger instance of a class that implements the {@link Logger} interface, and will handle the logging
* @return the usable API object
*/
public static async Task<Api> Get(String endpoint, String accessToken, ICache cache, ILogger logger, HttpClient client) {
String url = (accessToken == null ? endpoint : (endpoint + "?access_token=" + HttpUtility.UrlEncode(accessToken)));
PrismicHttpClient prismicHttpClient = new PrismicHttpClient(client);
JToken json = cache.Get(url);
if (json == null)
{
json = await prismicHttpClient.fetch(url, logger, cache);
cache.Set(url, 5000L, json);
}
ApiData apiData = ApiData.Parse(json);
return new Api(apiData, accessToken, cache, logger, prismicHttpClient);
}