Spring.Caching.AspNetCache - 基于 ReturnValue 的条件 - Spring 表达式语言中的条件
我将 Cache Aspect 与 ASP.NET Cache 一起使用。我需要根据 ReturnValue 创建条件。
我简化了我的问题。我在返回简单 POCO 对象的方法上使用 CacheResult 方面。
这是定义:
public class MyData
{
public string MyProperty { get; set; }
}
public class MyResponse
{
public int MyId { get; set; }
public MyData [] Result { get; set; }
}
我需要为缓存创建条件 - 仅当 MyResponse.MyData.Lenght 大于批量限制时才缓存结果。
[CacheResult("AspNetCache", "'MyResponse.MyId=' + #id",
Condition = "MyResponse.Result.Length > #batchLimit")]
public MyResponse GetResponse(int id, int batchLimit)
{
Thread.Sleep(5000);
return new MyResponse
{
MyId = 1,
Result =
new MyData[]
{
new MyData {MyProperty = "A"}, new MyData {MyProperty = "B"},
new MyData {MyProperty = "C"},
}
};
}
我尝试了条件的定义:
Condition = "MyResponse.Result.Length > #batchLimit"
我收到此错误:
无法解析指定上下文的“MyResponse”节点 [示例.MyResponse]。
所以我尝试了第二个版本:
Condition = "'MyResponse.Result.Length' > #batchLimit"
完成时出现错误:
Cannot compare instances of [System.String] and [System.Int32] because they cannot be coerced to the same type.
我用谷歌搜索我可以使用关键字ReturnValue
,如下所示:
Condition = "#ReturnValue != null"
但我不知道如何访问MyResponse.MyData.Length.
I use Cache Aspect with ASP.NET Cache. I need create condition based on ReturnValue.
I simplified my problem. I use CacheResult aspect on method wich return simple POCO object.
Here is definition:
public class MyData
{
public string MyProperty { get; set; }
}
public class MyResponse
{
public int MyId { get; set; }
public MyData [] Result { get; set; }
}
I need create condition for cache - Cache result only if MyResponse.MyData.Lenght is bigger then batch limit.
[CacheResult("AspNetCache", "'MyResponse.MyId=' + #id",
Condition = "MyResponse.Result.Length > #batchLimit")]
public MyResponse GetResponse(int id, int batchLimit)
{
Thread.Sleep(5000);
return new MyResponse
{
MyId = 1,
Result =
new MyData[]
{
new MyData {MyProperty = "A"}, new MyData {MyProperty = "B"},
new MyData {MyProperty = "C"},
}
};
}
I tried this definition of condition:
Condition = "MyResponse.Result.Length > #batchLimit"
I got this error:
'MyResponse' node cannot be resolved for the specified context
[Sample.MyResponse].
So I tried second version:
Condition = "'MyResponse.Result.Length' > #batchLimit"
Finished with error:
Cannot compare instances of [System.String] and [System.Int32] because they cannot be coerced to the same type.
I google it I can use keyword ReturnValue
something like this:
Condition = "#ReturnValue != null"
But I don't know how I can access to MyResponse.MyData.Length
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
条件表达式求值的上下文是返回值,所以只需这样做:
相当于
the context for the evaluation of the condition expression is the return value, so just do this:
equivalent to