本文整理汇总了C#中ICache.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ICache.Add方法的具体用法?C# ICache.Add怎么用?C# ICache.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICache
的用法示例。
在下文中一共展示了ICache.Add方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunRoutine
public void RunRoutine(Key key, MethodInterceptionArgs args, ICache cache, Mutex mutex, Action routine)
{
using (mutex) //avoid mutex being lost through garbage collection (unsure if required) see: odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
{
for (;;)
{
routine();
cache.Add(key, args.ReturnValue);
Thread.Sleep(key.DurationToStore - TimeBeforeExpiryToRunRoutine);
}
}
}
示例2: OnInvoke
/// <remarks>
/// Consider alternatives to instantiating instances
/// of the specified types using Activator.CreateInstance.
/// </remarks>
public override void OnInvoke(MethodInterceptionArgs args)
{
Ensure.That<ArgumentNullException>(args.IsNotNull(), "args");
_cache = (ICache) Activator.CreateInstance(_cacheType);
_keyCreationStrategy = (IFriendlyNameCreationStrategy) Activator.CreateInstance(_keyCreationStrategyType);
if (_recalculationStrategyType.IsNotNull())
{
_recalculationStrategy =
(ICacheItemRecalculationStrategy)
Activator.CreateInstance(_recalculationStrategyType, _durationToRecalculate);
}
var friendlyName = _keyCreationStrategy.GenerateFriendlyName(args);
var key = _durationToStore == default(TimeSpan) //infer from the variables set, the key type to create?
? new Key(DefaultCacheDuration, DefaultCacheExpirationType, friendlyName)
: new Key(_durationToStore, _storageStyle, _expirationType, friendlyName);
object cacheItem;
if ((cacheItem = _cache[key.ToString()]).IsNotNull())
{
args.ReturnValue = cacheItem;
}
else
{
args.Proceed();
_cache.Add(key, args.ReturnValue);
if (_recalculationStrategy.IsNotNull())
AsyncCacheItemRecalculator.EnsureIsStarted(key, _recalculationStrategy, args, _cache);
}
}
示例3: DoAction
public override void DoAction(ICache cache, IServiceDto serviceDto)
{
cache.Add(serviceDto);
}
示例4: GetNextBatchCached
LearningScriptBatch GetNextBatchCached(ISet<int> indexes, ICache cache)
{
// TODO: Grouping.
var scripts = new List<LearningScript>(indexes.Count);
foreach (int index in indexes)
{
string key = index.ToString() + uid;
var script = cache[key] as LearningScript;
if (script == null)
{
script = ScriptProvider[index];
cache.Add(key, script);
}
scripts.Add(script);
}
return new LearningScriptBatch(scripts, Strategy as ILearningErrorReport);
}
示例5: HandleResponse
//.........这里部分代码省略.........
#endregion
#region If-Modified-Since precondition header
IfModifiedSinceHeader ifModifiedSinceHeader = IfModifiedSinceHeader.Parse(httpRequest.Headers["If-Modified-Since"]);
bool validIfModifiedSinceHttpDate = ifModifiedSinceHeader != null && ifModifiedSinceHeader.HttpDate <= _systemClock.UtcDateTime;
// Only consider an If-Modified-Since header if response status code is 200 and the HTTP-date is valid
if (suggestedResponse.StatusCode.ParsedStatusCode == HttpStatusCode.OK && validIfModifiedSinceHttpDate)
{
// Return 304 if the response was cached before the HTTP-date
if (cacheItem != null && cacheItem.CachedUtcTimestamp < ifModifiedSinceHeader.HttpDate)
{
return WriteResponse(httpResponse, Response.NotModified());
}
}
#endregion
#region If-Unmodified-Since precondition header
IfUnmodifiedSinceHeader ifUnmodifiedSinceHeader = IfUnmodifiedSinceHeader.Parse(httpRequest.Headers["If-Unmodified-Since"]);
bool validIfUnmodifiedSinceHttpDate = ifUnmodifiedSinceHeader != null && ifUnmodifiedSinceHeader.HttpDate <= _systemClock.UtcDateTime;
// Only consider an If-Unmodified-Since header if response status code is 2xx or 412 and the HTTP-date is valid
if (((suggestedResponse.StatusCode.StatusCode >= 200 && suggestedResponse.StatusCode.StatusCode <= 299) || suggestedResponse.StatusCode.StatusCode == 412) && validIfUnmodifiedSinceHttpDate)
{
// Return 412 if the previous response was removed from the cache or was cached again at a later time
if (cacheItem == null || cacheItem.CachedUtcTimestamp >= ifUnmodifiedSinceHeader.HttpDate)
{
return WriteResponse(httpResponse, Response.PreconditionFailed());
}
}
#endregion
#region No server caching
// Do not cache the response when the response sends a non-cacheable status code, or when an Authorization header is present
if (!_cacheableStatusCodes.Contains(suggestedResponse.StatusCode) || httpRequest.Headers["Authorization"] != null)
{
return WriteResponse(httpResponse, suggestedResponse);
}
CacheControlHeader cacheControlHeader = CacheControlHeader.Parse(httpRequest.Headers["Cache-Control"]);
// Do not cache the response if a "Cache-Control: no-cache" or "Cache-Control: no-store" header is present
if (cacheControlHeader != null && (cacheControlHeader.NoCache || cacheControlHeader.NoStore))
{
return WriteResponse(httpResponse, suggestedResponse);
}
IEnumerable<PragmaHeader> pragmaHeader = PragmaHeader.ParseMany(httpRequest.Headers["Pragma"]);
// Do not cache the response if a "Pragma: no-cache" header is present
if (pragmaHeader.Any(arg => String.Equals(arg.Name, "no-cache", StringComparison.OrdinalIgnoreCase)))
{
return WriteResponse(httpResponse, suggestedResponse);
}
#endregion
// Return 504 if the response has not been cached but the client is requesting to receive only a cached response
if (cacheItem == null && cacheControlHeader != null && cacheControlHeader.OnlyIfCached)
{
return WriteResponse(httpResponse, Response.GatewayTimeout());
}
if (cacheItem != null)
{
// Write the cached response if no Cache-Control header is present
// Write the cached response if a "Cache-Control: max-age" header is validated
// Write the cached response if a "Cache-Control: max-stale" header is validated
// Write the cached response if a "Cache-Control: min-fresh" header is validated
if (cacheControlHeader == null ||
_systemClock.UtcDateTime - cacheItem.CachedUtcTimestamp <= cacheControlHeader.MaxAge ||
cacheControlHeader.OnlyIfCached ||
cacheItem.ExpiresUtcTimestamp == null ||
_systemClock.UtcDateTime - cacheItem.ExpiresUtcTimestamp.Value <= cacheControlHeader.MaxStale ||
cacheItem.ExpiresUtcTimestamp.Value - _systemClock.UtcDateTime < cacheControlHeader.MinFresh)
{
return WriteResponseInCache(httpResponse, cacheItem);
}
}
bool cacheOnServer = suggestedResponse.CachePolicy.AllowsServerCaching;
var cacheResponse = new CacheResponse(suggestedResponse);
if (cacheOnServer)
{
DateTime expirationUtcTimestamp = suggestedResponse.CachePolicy.ServerCacheExpirationUtcTimestamp != null
? suggestedResponse.CachePolicy.ServerCacheExpirationUtcTimestamp.Value
: _systemClock.UtcDateTime + suggestedResponse.CachePolicy.ServerCacheMaxAge.Value;
cache.Add(cacheKey, cacheResponse, expirationUtcTimestamp);
}
return WriteResponse(httpResponse, cacheResponse);
}
示例6: GetFeatureMatrixCache
// Cache
private void GetFeatureMatrixCache(FeatureIndexSet indexes, List<Vector<double>[]> vectors, ICache cache)
{
//var sl = new SpinLock();
//Parallel.ForEach(indexes, (index) =>
//{
// string key = index.ToString() + uid;
// var cv = cache[key] as Vector<double>[];
// if (cv == null)
// {
// cv = GetFeatureVectorsOf(RetrieveFeatureSet(index));
// cache.Add(key, cv);
// }
// bool taken = false;
// try
// {
// sl.Enter(ref taken);
// vectors.Add(cv);
// }
// finally
// {
// if (taken) sl.Exit();
// }
//});
foreach (int index in indexes)
{
string key = index.ToString() + uid;
var cv = cache[key] as Vector<double>[];
if (cv != null)
{
vectors.Add(cv);
}
else
{
cv = GetFeatureVectorsOf(RetrieveFeatureSet(index));
cache.Add(key, cv);
vectors.Add(cv);
}
}
}
示例7: GetNextBatchCached
ScriptBatch GetNextBatchCached(ISet<int> indexes, ICache cache)
{
var remainingIndexes = new HashSet<int>();
var scripts = new Script[indexes.Count];
int sidx = 0;
foreach (int index in indexes)
{
string key = index.ToString() + uid;
var script = cache[key] as Script;
if (script == null)
{
remainingIndexes.Add(index);
}
else
{
scripts[sidx++] = script;
}
}
if (remainingIndexes.Count != 0)
{
if (remainingIndexes.Count == 1)
{
int index = remainingIndexes.First();
var script = ScriptProvider[index];
scripts[sidx++] = script;
string key = index.ToString() + uid;
cache.Add(key, script);
}
else
{
var indexEnum = remainingIndexes.GetEnumerator();
foreach (var script in ScriptProvider.GetScripts(remainingIndexes))
{
indexEnum.MoveNext();
int index = indexEnum.Current;
scripts[sidx++] = script;
string key = index.ToString() + uid;
cache.Add(key, script);
}
}
}
return new ScriptBatch(scripts, Strategy as ILearningErrorReport);
}
示例8: Cache
protected void Cache(ISession session, object sessionId, ICache cache)
{
cache.Add(sessionId, session);
}