ServiceStack caching strategy -
i'm learning servicestack , have question how use [route] tag caching. here's code:
[route("/applicationusers")] [route("/applicationusers/{id}")] public class applicationusers : ireturn<applicationuserresponse> { public int id { get; set; } } public object get(applicationusers request) { //var cachekey = urnid.create<applicationusers>("users"); //return requestcontext.tooptimizedresultusingcache(base.cache, cachekey, () => return new applicationuserresponse { applicationusers = (request.id == 0) ? db.select<applicationuser>() : db.select<applicationuser>("id = {0}", request.id) }; }
what want "applicationusers" collection cached, , times when pass in id, use main cached collection individual object out.
if uncomment code above, main collection cached under "users" key, specific query submit hits db again. thinking cache wrong?
thanks in advance, mike
this line
var cachekey = urnid.create<applicationusers>("users");
is creating same cache key requests, must use of request parameters make "unique key" each different response.
var cachekey = urnid.create<applicationusers>(request.id.tostring());
this give "urn:applicationusers:0" key , "urn:applicationusers:9" request id = 9
now can use extension method in way.
return requestcontext.tooptimizedresultusingcache(cache, cachekey, () => { if(request.id == 0) return getall(); else return getone(request.id); });
i hope helps, regards.
Comments
Post a Comment