Best practice for partial response and cache?

Hi,

1 AutoQuery partial response

my requestDto like this.

[Route("/schools", "GET")]
public class GetSchools : QueryBase<MSchool>
{
    public List<string> Fields { get; set; }
}

when user call /schools?fields=Id,Name&format=json , only return id and name.

In my service almost like

        var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
        q.Join<CommonCode>((a, b) => a.SchoolType == b.Id)
            .Select("MSchool.*,CommonCode.Name AS SchoolTypeName");
        if (request.Fields.Count > 0)
        {
            q.Select(request.Fields.Join(","));
        }

need filter fields after join. but it’s failed when fields like ?fields=schoolid,schooltypename.

should i filter fields in global response filter(is best choice)?

2 Cache data in redis

using base.Request.ToOptimizedResultUsingCache store a list of data.

if update single item, i need remove and inserting item again.

in this answer you can create a unique pizza key for each object give a nice solution for update cache.

but how store list of data?? loop list and add each item to cache? is this the best way?

Not sure what the issue is with your custom AutoQuery impl, you’ve not provided any details about the error, what your table looks like or what the resulting SQL is. I’d enable debug logging or built-in profiling so you can see the SQL that’s generated which will hopefully identify what the issue is.

Also when your query gets too complicated, just ignore AutoQuery and implement the query as you would without it. i.e. don’t use AutoQuery if it doesn’t support your use-case.

but how store list of data?? loop list and add each item to cache?

No I wouldn’t do multiple cache hits to store a list of items, each call is a blocking I/O operation if you’re using anything other than the MemoryCacheClient. I’d store the entire collection under a unique key that populated the results, e.g. a lot of the time you could use the relative url as the string that generates those results.

thanks,
if i return in service like

 return base.Request.ToOptimizedResultUsingCache(
                                        base.Cache, "urn:schools", () =>
        {
            var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
            q.Join<CommonCode>((a, b) => a.SchoolType == b.Id)
                .Select("MSchool.*,CommonCode.Name AS SchoolTypeName");
            var r = AutoQuery.Execute(request, q);
            return r;
        });

there are some keys added on redis

urn:schools
urn:schools.html
urn:schools.html.deflate

and in my controller, response is a ServiceStack.CompressedResult,how can i get json returned?

using (var helloService = HostContext.ResolveService<SchoolService>(base.HttpContext))
            {
                var response = helloService.Get(new GetSchools());
             }

You need to call the Service asking for a JSON response. If you’re calling this Service from an MVC Controller the request is most likely asking for a HTML response.

thanks~

maybe i need ignore AutoQuery for this time.