/// <summary> /// 增加一个缓存 /// </summary> /// <param name="key">关键字</param> /// <param name="obj">对象</param> public static void AddObjectCache(string key, object obj) { CacheItemPolicy policy = new CacheItemPolicy(); policy.SlidingExpiration = TimeSpan.FromMinutes(60); if (!objCache.Contains(key)) { // objCache.Set(key, obj, policy); objCache.Add(key, obj, policy); } }
private RequestTokenResult RetrieveRequestToken(Uri callBackUri) { if (callBackUri == null || string.IsNullOrEmpty(callBackUri.AbsoluteUri)) { throw new ArgumentNullException("callBackUri"); } IRestResponse response; try { var restClient = _restClientFactory.CreateRestClient(BaseUrl); restClient.Authenticator = OAuth1Authenticator.ForRequestToken(_consumerKey, _consumerSecret, callBackUri.AbsoluteUri); var request = new RestRequest("!api/1.0/oauth/request_token", Method.POST); response = restClient.Execute(request); } catch (Exception exception) { throw new AuthenticationException("Failed to obtain a Request Token from BitBucket.", exception); } if (response == null || response.StatusCode != HttpStatusCode.OK) { throw new AuthenticationException( string.Format( "Failed to obtain a Request Token from BitBucket OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}", response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription)); } // Grab the params which should have the request token info. var querystringParameters = HttpUtility.ParseQueryString(response.Content); var oAuthToken = querystringParameters[OAuthTokenKey]; var oAuthTokenSecret = querystringParameters[OAuthTokenSecretKey]; // Cache the TokenSecret, for later. _memoryCache.Add(oAuthToken, oAuthTokenSecret, DateTime.Now.AddMinutes(CacheExpiryInMinutes)); if (string.IsNullOrEmpty(oAuthToken) || string.IsNullOrEmpty(oAuthTokenSecret)) { throw new AuthenticationException( "Retrieved a Twitter Request Token but it doesn't contain both the oauth_token and oauth_token_secret parameters."); } return(new RequestTokenResult { OAuthToken = oAuthToken, OAuthTokenSecret = oAuthTokenSecret }); }
public static T AddOrGetExisting <T>(ObjectCache cache, string key, T item, CacheItemPolicy policy) { object cachedItem = cache.Get(key); if (cachedItem is T t) { return(t); } cache.Add(key, item, policy); return(item); }
protected TResult Get <TResult>(string partialKey, Func <TResult> getFunc) where TResult : class { var key = string.Format("{0}_{1}", CacheKeyBase, partialKey); var cached = _cache.Get(key); if (cached != null) { System.Diagnostics.Debug.WriteLine("Geocoding cache hit " + key); return((TResult)cached); } System.Diagnostics.Debug.WriteLine("Geocoding CACHE MISS " + key); var result = getFunc(); if (result != null) { _cache.Add(key, result, Policy); } return(result); }