web services - ServiceStack cache in VB.net -
how go implementing servicestack cache in vb.net? i've seen many c# examples, not able transfer onto vb.net.
the point stack in 1st , 2nd argument of servicestack.servicehost.requestcontextextensions.tooptimizedresultusingcache
- 1st should be:
servicestack.servicehost.irequestcontext
- not sure irequestcontext - 2nd should be:
servicestack.cacheaccess.providers.memorycacheclient
- how set cache default in config i.e. memorycacheclient
code below, suggestion appreciated.
global.asax.vb
public class global_asax inherits system.web.httpapplication public class helloapphost inherits apphostbase public sub new() mybase.new("web services", gettype(wrapper).assembly) end sub public overrides sub configure(byval container container) routes.add(of getproduct)("/getproduct").add(of getproduct)("/getproduct/{*}") plugins.add(new cors.corsfeature(allowedheaders:="content-type, authorization")) container.register(of icacheclient)(new memorycacheclient()) end sub end class sub application_start(byval sender object, byval e eventargs) dim apphost = new helloapphost() apphost.init() end sub end class
ws.vb
public class wrapper public class wrappergetproduct implements iservice(of getproduct) public function execute(byval request getproduct) object implements servicestack.servicehost.iservice(of getproduct).execute dim cachekey string = "some_key" dim expireintimespan = new timespan(1, 0, 0) return servicestack.servicehost.requestcontextextensions.tooptimizedresultusingcache( servicestack.servicehost.irequestcontext, // not sure should servicestack.cacheaccess.providers.memorycacheclient, // not sure should - how set cache setted in configuration (in memory cache)? cachekey, expireintimespan, function() request.handlerequest() ) end function end class end class
use new api
tooptimizedresultusingcache
extension method requestcontext
services inherit/expose, same cache (resolved automatically via ioc).
example below converted c#, caching/wrapping existing service (appconfig , repository resolved via ioc, registered in apphost configure method).
imports system.collections.generic imports servicestack.common imports servicestack.servicehost imports servicestack.serviceinterface.servicemodel imports servicestack.common.web public class searchterm public property latitude() double return m_latitude end set m_latitude = value end set end property private m_latitude double public property longitude() double return m_longitude end set m_longitude = value end set end property private m_longitude double public property term() string return m_term end set m_term = value end set end property private m_term string end class <route("/lookup/searchterm", "get")> _ public class searchtermrequest implements ireturn(of searchtermresponse) public property term() string return m_term end set m_term = value end set end property private m_term string end class public class searchtermresponse implements ihasresponsestatus public property responsestatus() responsestatus return m_responsestatus end set m_responsestatus = value end set end property private m_responsestatus responsestatus public property results() list(of searchterm) return m_results end set m_results = value end set end property private m_results list(of searchterm) end class <route("/cached/lookup/searchterm")> _ public class cachedsearchtermrequest implements ireturn(of cachedsearchtermresponse) public readonly property cachekey() string return urnid.create(of cachedsearchtermrequest)(string.format("{0}", me.term)) end end property public property term() string return m_term end set m_term = value end set end property private m_term string end class public class cachedsearchtermresponse implements ihasresponsestatus public property responsestatus() responsestatus return m_responsestatus end set m_responsestatus = value end set end property private m_responsestatus responsestatus public property results() list(of searchterm) return m_results end set m_results = value end set end property private m_results list(of searchterm) end class public class searchtermservice inherits service public property repository() irepository return m_repository end set m_repository = value end set end property private m_repository irepository public function [get](request searchtermrequest) searchtermresponse return new searchtermresponse() { _ key .results = me.repository.searchtermget(request) _ } end function end class public class cachedsearchtermservice inherits service public property appconfig() appconfig return m_appconfig end set m_appconfig = value end set end property private m_appconfig appconfig public function [get](request cachedsearchtermrequest) object dim cachekey string = request.cachekey return me.requestcontext.tooptimizedresultusingcache( mybase.cache, cachekey, new timespan(0, me.appconfig.cachetimeminutes, 0), function() using service = me.resolveservice(of searchtermservice)() return service.[get](request.translateto(of searchtermrequest)()).translateto(of cachedsearchtermresponse)() end using end function ) end function end class
Comments
Post a Comment