简体   繁体   English

MemoryCache获取/设置并分配给变量

[英]MemoryCache get/set and assigning to variable

I have been testing some code locally and read through quite a bit of SO posts but I am a bit confused still how MemoryCache works in certain scenarios. 我一直在本地测试一些代码,并阅读了很多SO文章,但我仍然对MemoryCache在某些情况下的工作方式感到有些困惑。 So the question relates to the following code: 因此,问题与以下代码有关:

 class Program
{

    static void Main(string[] args)
    {
        var test = new Test();
        test.TestingMethod();
    }

}

public class Test
{
    private readonly MemoryCache MemoryCache = MemoryCache.Default;
    private CacheItemPolicy policy = null;
    private CacheEntryRemovedCallback callback = null;

    public void TestingMethod()
    {
        var ints = new List<int>();
        for (var i = 0; i <= 1000; i++)
        {
            ints.Add(i);
        }

        callback = this.MyCachedItemRemovedCallback;
        policy = new CacheItemPolicy
        {
            Priority = CacheItemPriority.Default,
            AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(2),
            RemovedCallback = callback
        };

        MemoryCache.Set("ints", ints, policy);
        var intsCached = (List<int>) MemoryCache.Get("ints");
        Task.Delay(TimeSpan.FromSeconds(15)).Wait();
        MemoryCache.Set("ints", new List<int>() {1}, policy);

        foreach (var intCached in intsCached)
        {
            Console.WriteLine(intCached);
        }
        Console.ReadLine();
    }

    void MyCachedItemRemovedCallback(CacheEntryRemovedArguments arguments)
    {
        Console.WriteLine("Expired");
    }
}

In the console output of this TestMethod I am getting Expired 1 - 1000 on separate lines 在此TestMethod的控制台输出中,我在单独的行上获得了1-1000的过期时间

Why would I get the 1-1000 instead of only the 1 that we set after expiration? 为什么我会得到1-1000,而不是仅获得我们在到期后设置的1? Doesn't this list refer to the same ref? 这个列表不是指同一个参考吗?

MemoryCache (or any other entity that stores values) will not magically update content of the List when you set entry with the same name to different value. 当您将具有相同名称的条目设置为不同的值时, MemoryCache (或任何其他存储值的实体)不会神奇地更新List的内容。 Ie what if you do MemoryCache.Set("ints", 42, policy); 即如果您执行MemoryCache.Set("ints", 42, policy);该怎么办MemoryCache.Set("ints", 42, policy); instead? 代替?

Looks like you expect value ( intsCached ) to be computed long after it is assigned to: 看起来您希望将值( intsCached )分配给以下对象后很长时间才能进行计算:

  var intsCached = (List<int>) MemoryCache.Get("ints");

intsCached is computed at that line and no longer know where it comes from. intsCached是在该行计算的,不再知道其来源。

If you want to have "variable" that remembers where it comes from and always get latest value from that location - use Func to compute it every time: 如果您想让“变量”记住它的来源,并始终从该位置获取最新值,请使用Func每次进行计算:

  Func<List<int>> intsCached = () => (List<int>) MemoryCache.Get("ints");
  ...
  foreach (var intCached in intsCached()) 
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM