The Basics
Cache Manager is structured into many different Nuget packages. The one which is common but must not be installed stand alone is CacheManager.Core. All other packages will add functionality, cache handles and other features, which are totally optional.
To get more details about the Cache Manager packages and features, read the readme file and the features article.
But now let's get started...
New Console Application
To get started let us create a new .Net C# console application in visual studio:

Now right click the project in solution explorer and click "Manage Nuget Packages", put "cachemanger" into the search box on the top right, you should get a list of packages like this:

Add nuget
Select the CacheManager.SystemRuntimeCaching package and hit "Install". This will actually install already all you need to use cache manager with the System.Runtime.Caching based in-process cache.
Now let's add some code to the newly created program.cs Main method:
using System;
using CacheManager.Core;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var cache = CacheFactory.Build("getStartedCache", settings =>
{
settings.WithSystemRuntimeCacheHandle("handleName");
});
}
}
}
This will create a new Cache Manager instance. To use the instance, we can add some test code. We will add two items, updated the second and then read both keys.
cache.Add("keyA", "valueA");
cache.Put("keyB", 23);
cache.Update("keyB", v => 42);
Let's also remove one key and see if it worked... The full code example:
static void Main(string[] args)
{
var cache = CacheFactory.Build("getStartedCache", settings =>
{
settings.WithSystemRuntimeCacheHandle("handleName");
});
cache.Add("keyA", "valueA");
cache.Put("keyB", 23);
cache.Update("keyB", v => 42);
Console.WriteLine("KeyA is " + cache.Get("keyA")); // should be valueA
Console.WriteLine("KeyB is " + cache.Get("keyB")); // should be 42
cache.Remove("keyA");
Console.WriteLine("KeyA removed? " + (cache.Get("keyA") == null).ToString());
Console.WriteLine("We are done...");
Console.ReadKey();
}
Hopefully this worked out just fine. Now you should be set to play around with the Cache Manager!
本文介绍了Cache Manager的基础,它由多个Nuget包构成,CacheManager.Core是常用但不能单独安装的包。还讲解了在Visual Studio中创建新的.Net C#控制台应用,通过Nuget安装Cache Manager相关包,并给出在program.cs中使用Cache Manager实例的代码示例。
1万+

被折叠的 条评论
为什么被折叠?



