Spring.Caching.AspNetCache - 基于 ReturnValue 的条件 - Spring 表达式语言中的条件

发布于 2024-12-27 00:16:36 字数 1710 浏览 1 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2025-01-03 00:16:37

条件表达式求值的上下文是返回值,所以只需这样做:

Condition = "Result.Length > #batchLimit"

相当于

Condition = "#root.Result.Length > #batchLimit"

the context for the evaluation of the condition expression is the return value, so just do this:

Condition = "Result.Length > #batchLimit"

equivalent to

Condition = "#root.Result.Length > #batchLimit"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文