使用Servicestack,将AutoQuery与ServiceSource一起使用时如何缓存结果集?

时间:2019-06-10 12:53:06

标签: c# caching .net-core servicestack servicestack-autoquery

我正在尝试将ServiceStack的AutoQuery与服务源一起使用,但是要么无法使缓存正常工作,要么被误解了缓存应如何工作。

我要达到的目的是向“边缘”微服务添加查询功能,该微服务调用提供完整数据列表的内部服务。

重现我的问题的最小代码:

class Program
{
    static async Task Main(string[] args)
    {
        IWebHost host = new WebHostBuilder()
            .UseKestrel((builderContext, options) => options.Configure(builderContext.Configuration.GetSection("Kestrel")))
            .UseStartup<Startup>()
            .Build();
        await host.RunAsync();
    }
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services) {}
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseServiceStack(new AppHost());
        app.Run(context => Task.FromResult(0));
    }
}

public class AppHost : AppHostBase
{
    public AppHost() : base("Hello Web Services", typeof(HelloService).Assembly){ }

    public override void Configure(Funq.Container container)
    {
        container.AddSingleton<ICacheClient, MemoryCacheClient>(); // Otherwise HostContext.Cache is null
        Plugins.Add(new AutoQueryDataFeature { MaxLimit = 3, IncludeTotal = true }.AddDataSource(ctx => ctx.ServiceSource<string>(new Hello(), HostContext.Cache, TimeSpan.FromMinutes(5))));
    }
}

// Request DTO
[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello : QueryData<NameDto>
{
    [QueryDataField(Condition = "StartsWith", Field = nameof(Name))]
    public string Name { get; set; }
}

public class NameDto
{
    public string Name { get; set; }
}

public class HelloService : Service
{
    public IAutoQueryData AutoQuery { get; set; }
    public async Task<object> Any(Hello query)
    {
        //Imagine I was making a service call to another microservice here...
        var data = new List<NameDto> { new NameDto { Name = "Bob" }, new NameDto { Name = "George" }, new NameDto { Name = "Baldrick" }, new NameDto { Name = "Nursey" }, new NameDto { Name = "Melchett" }, new NameDto { Name = "Kate" } };

        DataQuery<NameDto> dataQuery = AutoQuery.CreateQuery(query, Request, new MemoryDataSource<NameDto>(data, query, Request));

        return AutoQuery.Execute(query, dataQuery);
    }
}   

Nuget软件包:Mircosoft.AspNetCore.All(2.2.1)和ServiceStack(5.4.0)

因此,在控制台(.NET Core 2.2)中,以上代码将启动并侦听端口5000。

如果我查询,我会得到我的列表,该列表仅限于预期的结果数,我也可以按预期跳过/获取。

但是,每次我调用service方法时,结果都不会被缓存(这是我在注册插件时指定的-缓存5分钟),并且如果我在service方法中放置了一个断点,则“每次都会重新创建姓名。即使我对服务提出相同的请求,也会发生这种情况。

我希望能够缓存结果集(在内存中可以),并且仅在缓存过期时才使用service方法。我在做什么错(或误解)?

修改

我以前尝试过Mythz建议的代码...现在我根本无法使用任何自动查询功能。

class Program
{
    static async Task Main(string[] args)
    {
        IWebHost host = new WebHostBuilder()
            .UseKestrel((builderContext, options) => options.Configure(builderContext.Configuration.GetSection("Kestrel")))
            .UseStartup<Startup>()
            .Build();
        await host.RunAsync();
    }
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseServiceStack(new AppHost());
        app.Run(context => Task.FromResult(0));
    }
}

public class AppHost : AppHostBase
{
    public AppHost() : base("Hello Web Services", typeof(HelloService).Assembly){ }

    public override void Configure(Funq.Container container)
    {
        container.AddSingleton<ICacheClient, MemoryCacheClient>();

        Plugins.Add(new AutoQueryDataFeature { MaxLimit = 5 }
            .AddDataSource(ctx => ctx.ServiceSource<GithubRepo>(ctx.Dto.ConvertTo<QueryGithubRepo>(),
                HostContext.Cache, TimeSpan.FromMinutes(5))));
    }


}


public class QueryGithubRepo : QueryData<GithubRepo>
{
    public string User { get; set; }
    public string Organization { get; set; }
}

public class GithubRepo
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Homepage { get; set; }
    public int Watchers_Count { get; set; }
    public int Stargazers_Count { get; set; }
    public int Size { get; set; }
    public string Full_Name { get; set; }
    public DateTime Created_at { get; set; }
    public DateTime? Updated_At { get; set; }

    public bool Has_Downloads { get; set; }
    public bool Fork { get; set; }

    public string Url { get; set; } // https://api.github.com/repos/NetCoreWebApps/bare
    public string Html_Url { get; set; }
    public bool Private { get; set; }

    public GithubRepo Parent { get; set; } // only on single result, e.g: /repos/NetCoreWebApps/bare
}

public class NameDto
{
    public string Name { get; set; }
}

public class HelloService : Service
{    
    public object Get(QueryGithubRepo request)
    {
        if (request.User == null && request.Organization == null)
            throw new ArgumentNullException("User");

        var url = request.User != null
            ? $"https://api.github.com/users/{request.User}/repos"
            : $"https://api.github.com/orgs/{request.Organization}/repos";

        return url.GetJsonFromUrl(requestFilter: req => req.UserAgent = GetType().Name)
            .FromJson<List<GithubRepo>>();
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在服务实现中使用自动查询,那只是查询普通服务结果的Custom AutoQuery implementation而不是AutoQuery Service Data Source

在这种情况下,听起来您确实想要一个可缓存的自动查询服务数据源,文档在其GetGithubRepos Service中显示了一个示例,该源对GitHub的第三方API进行了API调用:

public class QueryGithubRepo : QueryData<GithubRepo> 
{
    public string User { get; set; }
    public string Organization { get; set; }
}

public object Get(GetGithubRepos request)
{
    if (request.User == null && request.Organization == null)
        throw new ArgumentNullException("User");

    var url = request.User != null
        ? $"https://api.github.com/users/{request.User}/repos"
        : $"https://api.github.com/orgs/{request.Organization}/repos";

    return url.GetJsonFromUrl(requestFilter:req => req.UserAgent = GetType().Name)
        .FromJson<List<GithubRepo>>();
}

然后您在注册服务数据源时将其注册为cached Service Data Source

Plugins.Add(new AutoQueryDataFeature { MaxLimit = 100 }
    .AddDataSource(ctx => ctx.ServiceSource<GithubRepo>(ctx.Dto.ConvertTo<GetGithubRepos>(), 
        HostContext.Cache, TimeSpan.FromMinutes(5)));
);

您可以使用HostContext.LocalCache将其缓存在本地内存缓存中,而不是已注册的ICacheClient caching provider