本文整理汇总了C#中IDatabase.Get方法的典型用法代码示例。如果您正苦于以下问题:C# IDatabase.Get方法的具体用法?C# IDatabase.Get怎么用?C# IDatabase.Get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDatabase
的用法示例。
在下文中一共展示了IDatabase.Get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetItem
public bool GetItem(string key, out object value)
{
key.GuardAgainstNullOrEmpty(nameof(key));
_database = _redis.GetDatabase();//connect only if arguments are valid to optimize resources
key = HashKey(key);
var now = DateTimeOffset.Now;//local variables are thread safe should be out of sync lock
try
{
value = _database.Get<CacheEntry>(key);
}
catch (Exception e)
{
value = null;
OnCachingFailed(e);
}
if (value == null) return false;
var entry = (CacheEntry)value;
if (EntryExpired(entry, now))
{
InvalidateItem(key);
value = null;
}
else
{
entry.LastAccess = now;
value = entry.Value;
// Update the entry in Redis to save the new LastAccess value.
lock (_lock)
{
try
{
_database.Set(key, entry);
}
catch (Exception e)
{
// Eventhough an error has occured, we will return true, because the retrieval of the entity was a success
OnCachingFailed(e);
}
}
return true;
}
return false;
}
示例2: GetItem
public bool GetItem(string key, out object value)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
_database = _redis.GetDatabase();//connect only if arguments are valid to optimize resources
key = HashKey(key);
var now = DateTimeOffset.Now;//local variables are thread safe should be out of sync lock
lock (_lock)
{
try
{
value = _database.Get<CacheEntry>(key);
}
catch (Exception e)
{
value = null;
OnCachingFailed(e);
}
if (value == null) return false;
var entry = (CacheEntry)value;
if (EntryExpired(entry, now))
{
InvalidateItem(key);
value = null;
}
else
{
entry.LastAccess = now;
value = entry.Value;
return true;
}
}
return false;
}
示例3: InvalidateItem
public void InvalidateItem(string key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
_database = _redis.GetDatabase();
key = HashKey(key);
lock (_lock)
{
try
{
var entry = _database.Get<CacheEntry>(key);
if (entry == null) return;
_database.KeyDelete(key);
foreach (var set in entry.EntitySets) {
_database.SetRemove(GetEntitySetKey(set), key);
}
}
catch (Exception e)
{
OnCachingFailed(e);
}
}
}
示例4: InvalidateItem
public void InvalidateItem(string key)
{
key.GuardAgainstNullOrEmpty(nameof(key));
_database = _redis.GetDatabase();
key = HashKey(key);
lock (_lock)
{
try
{
var entry = _database.Get<CacheEntry>(key);
if (entry == null) return;
_database.KeyDelete(key, CommandFlags.FireAndForget);
foreach (var set in entry.EntitySets) {
_database.SetRemove(AddCacheQualifier(set), key, CommandFlags.FireAndForget);
}
}
catch (Exception e)
{
OnCachingFailed(e);
}
}
}
示例5: MeasureGetTimeByRedis
/// <summary>
/// 測量讀取的時間
/// </summary>
/// <param name="cache"></param>
private static void MeasureGetTimeByRedis(string cacheName, IDatabase cache)
{
System.Diagnostics.Stopwatch sh = new System.Diagnostics.Stopwatch();
sh.Start();
foreach (string key in redisManager.GetAllKeys())
{
if (key.StartsWith("OP-"))
{
object t = cache.Get(key);
}
}
sh.Stop();
Console.WriteLine(string.Format("CacheName:{0},Get-Milliseconds:{1}", cacheName, sh.ElapsedMilliseconds));
}