ServiceStack ORMLite缓存错误

本文关键字:错误 缓存 ORMLite ServiceStack | 更新日期: 2023-09-27 18:05:36

我正在尝试使用ORMLite实现持久的数据库缓存到SQL。我在Global.asax.cs:

的Configure方法中启用了ORMLite缓存提供程序。
// Implement database caching for request/response JSON
var connectionString = ConfigurationManager.ConnectionStrings["ApiDbConnectionString"].ConnectionString;
container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
container.RegisterAs<OrmLiteCacheClient, ICacheClient>();
container.Resolve<ICacheClient>().InitSchema();
下面是我如何实现缓存的,就像在互联网上的许多例子中看到的那样
public object Get(PatientRequest request)
{
    var cacheKey = request.SSN;
    var expireInTimespan = new TimeSpan(0, 0, 15, 0);
    return base.Request.ToOptimizedResultUsingCache<Patient>(base.Cache, cacheKey, expireInTimespan, () => {
        var Patient = dbConn.Single<Patient>(x => x.SSN == request.SSN && x.FacilityLocationId == Convert.ToInt32(UALocation));
        if (patient != null)
        {
            dbConn.LoadReferences(patient);
            ...
            return patient;
        }
        else
        {
            var error = new GenericResponse();
            error.Message = "No patient found by SSN.";
            return error;   // This doesn't work...
        }
    });
}

我得到以下错误:

Error 8 The type arguments for method 'ServiceStack.RequestExtensions.ToOptimizedResultUsingCache<T>(ServiceStack.Web.IRequest, ServiceStack.Caching.ICacheClient, string, System.TimeSpan?, System.Func<T>)' 
cannot be inferred from the usage. Try specifying the type arguments explicitly.

ServiceStack ORMLite缓存错误

缓存填充委托函数必须返回T的结果,因为ToOptimzedResultUsingCache<T>的最后一个参数是System.Func<T>。由于您没有指定T,并且您没有return,它将无法编译。

尝试将T的类型指定为Patient,并将return设置为如下所示:

var cacheKey = request.SSN;
var expireInTimespan = new TimeSpan(0, 0, 15, 0);
return base.Request.ToOptimizedResultUsingCache<Patient>(base.Cache, cacheKey, expireInTimespan, () => {
    var patient = dbConn.Single<Patient>(x => x.SSN == request.SSN && x.FacilityLocationId == Convert.ToInt32(UALocation));
    if(patient == null)
        return null;
    dbConn.LoadReferences(patient);
    ...
    return patient;
});