CacheManager的使用和例子[源码]

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:

4624570-eb5dbed1bc5a0c3c.png
image.png

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:

4624570-6b86059ce23ea6fc.png
image.png
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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值