given generic handler:
<%@ webhandler language="c#" class="autocomp" %> using system; using system.text; using system.text.regularexpressions; using system.web; using system.web.ui; public class autocomp : ihttphandler { public void processrequest (httpcontext context) { context.response.contenttype = "application/json"; context.response.bufferoutput = true; var searchterm = (context.request.querystring["name_startswith"] + "").trim(); context.response.write(searchterm); context.response.write(datetime.now.tostring("s")); context.response.flush(); } public bool isreusable { { return false; } } }
how server side
cache file 1 hour based on name_startswith
query string parameter? web user controls it's easy:
<%@ outputcache duration="120" varybyparam="paramname" %>
but i've been looking around while same generic handler (ashx
) file , can't find solutions.
with code you've provided you're telling end user browser cache results 30 minutes, aren't doing server side caching.
if want cache results server side you're looking httpruntime.cache
. allow insert item cache globally available. on page load want check existence of cached item, if item doesn't exist or expired in cache, go database , retrieve objects.
edit
with updated code sample, found https://stackoverflow.com/a/6234787/254973 worked in tests. in case do:
public class autocomp : ihttphandler { public void processrequest(httpcontext context) { outputcachedpage page = new outputcachedpage(new outputcacheparameters { duration = 120, location = outputcachelocation.server, varybyparam = "name_startswith" }); page.processrequest(httpcontext.current); context.response.contenttype = "application/json"; context.response.bufferoutput = true; var searchterm = (context.request.querystring["name_startswith"] + "").trim(); context.response.write(searchterm); context.response.write(datetime.now.tostring("s")); } public bool isreusable { { return false; } } private sealed class outputcachedpage : page { private outputcacheparameters _cachesettings; public outputcachedpage(outputcacheparameters cachesettings) { // tracing requires page ids unique. id = guid.newguid().tostring(); _cachesettings = cachesettings; } protected override void frameworkinitialize() { base.frameworkinitialize(); initoutputcache(_cachesettings); } } }
Comments
Post a Comment