本文整理汇总了C#中ICacheManager.AddOrUpdate方法的典型用法代码示例。如果您正苦于以下问题:C# ICacheManager.AddOrUpdate方法的具体用法?C# ICacheManager.AddOrUpdate怎么用?C# ICacheManager.AddOrUpdate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICacheManager
的用法示例。
在下文中一共展示了ICacheManager.AddOrUpdate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestEachMethod
public static void TestEachMethod(ICacheManager<object> cache)
{
cache.Clear();
cache.Add("key", "value", "region");
cache.AddOrUpdate("key", "region", "value", _ => "update value", 22);
cache.Expire("key", "region", TimeSpan.FromDays(1));
var val = cache.Get("key", "region");
var item = cache.GetCacheItem("key", "region");
cache.Put("key", "put value");
cache.RemoveExpiration("key");
object update2;
cache.TryUpdate("key", "region", _ => "update 2 value", out update2);
object update3 = cache.Update("key", "region", _ => "update 3 value");
cache.Remove("key", "region");
cache.Clear();
cache.ClearRegion("region");
}
示例2: CacheThreadTest
public static void CacheThreadTest(ICacheManager<string> cache, int seed)
{
cache.Clear();
var threads = 10;
var numItems = 1000;
var eventAddCount = 0;
var eventRemoveCount = 0;
var eventGetCount = 0;
cache.OnAdd += (sender, args) => { Interlocked.Increment(ref eventAddCount); };
cache.OnRemove += (sender, args) => { Interlocked.Increment(ref eventRemoveCount); };
cache.OnGet += (sender, args) => { Interlocked.Increment(ref eventGetCount); };
Func<int, string> keyGet = (index) => "key" + ((index + 1) * seed);
Action test = () =>
{
for (int i = 0; i < numItems; i++)
{
cache.AddOrUpdate(keyGet(i), i.ToString(), _ => i.ToString() + "update");
}
for (int i = 0; i < numItems; i++)
{
if (i % 10 == 0)
{
cache.Remove(keyGet(i));
}
}
for (int i = 0; i < numItems; i++)
{
string val = cache.Get(keyGet(i));
}
};
Parallel.Invoke(new ParallelOptions() { MaxDegreeOfParallelism = 8 }, Enumerable.Repeat(test, threads).ToArray());
foreach (var handle in cache.CacheHandles)
{
var stats = handle.Stats;
Console.WriteLine(string.Format(
"Items: {0}, Hits: {1}, Miss: {2}, Remove: {3}, ClearRegion: {4}, Clear: {5}, Adds: {6}, Puts: {7}, Gets: {8}",
stats.GetStatistic(CacheStatsCounterType.Items),
stats.GetStatistic(CacheStatsCounterType.Hits),
stats.GetStatistic(CacheStatsCounterType.Misses),
stats.GetStatistic(CacheStatsCounterType.RemoveCalls),
stats.GetStatistic(CacheStatsCounterType.ClearRegionCalls),
stats.GetStatistic(CacheStatsCounterType.ClearCalls),
stats.GetStatistic(CacheStatsCounterType.AddCalls),
stats.GetStatistic(CacheStatsCounterType.PutCalls),
stats.GetStatistic(CacheStatsCounterType.GetCalls)));
}
cache.Dispose();
Console.WriteLine(string.Format(
"Event - Adds {0} Hits {1} Removes {2}",
eventAddCount,
eventGetCount,
eventRemoveCount));
}
示例3: RandomRWTest
public static void RandomRWTest(ICacheManager<Item> cache)
{
cache.Clear();
const string keyPrefix = "RWKey_";
const int actionsPerIteration = 104;
const int initialLoad = 1000;
int iterations = 0;
int removeFails = 0;
var keyIndex = 0;
var random = new Random();
Action create = () =>
{
if (keyIndex > 10000)
{
keyIndex = initialLoad;
}
Interlocked.Increment(ref keyIndex);
var key = keyPrefix + keyIndex;
var newValue = Guid.NewGuid().ToString();
var item = Item.Generate();
cache.AddOrUpdate(key, item, p =>
{
p.SomeStrings.Add(newValue);
return p;
});
};
Action read = () =>
{
for (var i = 0; i < 100; i++)
{
var val = cache.Get(keyPrefix + random.Next(1, keyIndex));
}
};
Action remove = () =>
{
const int maxTries = 10;
bool result = false;
var tries = 0;
string key;
do
{
tries++;
key = keyPrefix + random.Next(1, keyIndex);
result = cache.Remove(key);
if (!result)
{
Interlocked.Increment(ref removeFails);
}
}
while (!result && tries < maxTries);
};
Action report = () =>
{
while (true)
{
Thread.Sleep(1000);
Console.WriteLine(
"Index is at {0} Items in Cache: {1} failed removes {4} runs: {2} \t{3}",
keyIndex,
cache.CacheHandles.First().Count,
iterations,
iterations * actionsPerIteration,
removeFails);
Console.ForegroundColor = ConsoleColor.DarkGray;
foreach (var handle in cache.CacheHandles)
{
var stats = handle.Stats;
Console.WriteLine(string.Format(
"Items: {0}, Hits: {1}, Miss: {2}, Remove: {3} Adds: {4}",
stats.GetStatistic(CacheStatsCounterType.Items),
stats.GetStatistic(CacheStatsCounterType.Hits),
stats.GetStatistic(CacheStatsCounterType.Misses),
stats.GetStatistic(CacheStatsCounterType.RemoveCalls),
stats.GetStatistic(CacheStatsCounterType.AddCalls)));
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
iterations = 0;
removeFails = 0;
}
};
for (var i = 0; i < initialLoad; i++)
{
create();
}
Task.Factory.StartNew(report);
while (true)
//.........这里部分代码省略.........
示例4: TestEachMethod
public static void TestEachMethod(ICacheManager<object> cache)
{
cache.Clear();
cache.Add("key", "value", "region");
cache.AddOrUpdate("key", "region", "value", _ => "update value", new UpdateItemConfig(2, VersionConflictHandling.EvictItemFromOtherCaches));
cache.Expire("key", "region", TimeSpan.FromDays(1));
var val = cache.Get("key", "region");
var item = cache.GetCacheItem("key", "region");
cache.Put("key", "region", "put value");
cache.RemoveExpiration("key", "region");
object update2;
cache.TryUpdate("key", "region", _ => "update 2 value", out update2);
object update3 = cache.Update("key", "region", _ => "update 3 value");
cache.Remove("key", "region");
cache.Clear();
cache.ClearRegion("region");
}