本文介绍了Func< T>如何通过到方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了获取/设置缓存项的代码,但是我不知道如何调用该方法.如何通过适当的 Func< T>getData 应用于此方法?

I have found code for Get/Set cache item, but I don't know how I can call this method, eg. how pass proper Func<T> getData to this method?

public class Cache<T> : MemoryCache where T : class
    {
        public void Set(string cacheKey, T cacheItem, CacheItemPolicy policy = null)
        {
            //...
        }

        public bool TryGet(string cacheKey, out T returnItem)
        {
             //...
        }
        public bool TryGetOrSet( string cacheKey, Func<T> getData, out T returnData, CacheItemPolicy policy = null )
        {
            if( TryGet( cacheKey, out returnData ) )
                return true;

            lock( WriteLock )
            {
                if( TryGet( cacheKey, out returnData ) )
                    return true;

                returnData = getData();
                Set( cacheKey, returnData, policy );
            }

            return false;
        }
    }

例如,假设缓存类型为 string .

For example let's assume that cache type is string.

第一个问题:通过使用方法 TryGetOrSet ,如何添加项目(键:userName变量,值:lastName变量)以进行缓存?当然,当缓存中不存在该项目时

First question: By using method TryGetOrSet how I can add item (key : userName variable, value: lastName variable) to cache? Of course when this item doesn't exists in cache

var cache = new Cache<string>("UserInfo");
var userName = "test";
var lastName = "test2";
TryGetOrSet(userName, ???, out var _) // <- what should I pass to Func<T>?

推荐答案

您的示例缺少 T 的声明-但您已在类中对其进行了注释

Your example is missing the declaration of T - but you've commented its on the class

public class Cache<T>
{
    public void TryGetOrSet( string cacheKey, Func<T> getData, out T returnData, CacheItemPolicy policy = null )
    {
       ...
    }
}

在这种情况下,您可以将与预期签名匹配的任何方法(匿名或其他)传递给 getData .所以说你有一个实例

In this case, you can pass to getData any method (anonymous or otherwise) which matches the signature expected. So say you have an instance

var myCache = new Cache<string>();

然后,任何不带参数且返回字符串的方法都可以作为该参数传递.

Then any method which takes no parameters and returns a string can be passed as that parameter.

string value = null;
myCache.TryGetOrSet("some_key", () => "foo", out value);

此外,如果您有一种方法可以将数据传输到某个地方,则可以传递对其的引用

Also if you have a method which gets your data somewhere a reference to that can be passed

// somewhere eg "MyRepository"
public IEnumerable<MyObject> MyDataAccessMethod() { return Enumerable.Empty<MyObject>(); }

var myCache = new Cache<IEnumerable<MyObject>>();
var repo = new MyRepository();
IEnumerable<MyObject> data = null;
myCache.TryGetOrSet("some_key", repo.MyDataAccessMethod, out data);

作为旁注,您已经声明了返回 void 的方法,但是您返回了布尔值-这对于 TryXXX 方法应该是有意义的返回一个布尔值以表示成功.

As a side note, you've declared the method to return void but youre returning a boolean - this makes sense as a TryXXX method should return a boolean to indicate success.

public bool TryGetOrSet( string cacheKey, Func<T> getData, out T returnData, CacheItemPolicy policy = null )
{
   ...
}


响应您的更新:


In response to your update:

var cache = new Cache<string>("UserInfo");
var userName = "test";
var lastName = "test2";
string result = null;
TryGetOrSet(userName, () => lastName, out result) ;

这篇关于Func&lt; T&gt;如何通过到方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:34
查看更多