KENEGOZI.COMKENEGOZI.COMKENEGOZI.COMKENEGOZI.COMKENEGOZI.COMKENEGOZI.COMKENEGOZI.COMKENEGOZI.COMKENEGOZI.COM

<form id='kenegozi' action='post'></form>
2008 aug 24
key-level locked cache - real life implementation
tagged as= asp.net 2.0 | c#followin my post on key-level locked cache... i got followin piece uv code frm my friend moran benistee... implementin same idea ova xmldocument which iz bein loaded ova internet... n asp.net cache.
thiz iz real life code. he usin it on very large-scale websiet in production.
as usual - use at ur own risk... n b kind enough 2 share thought n improvement ideas here 4 hiz use.
public static class xmlservice [ private static dictionary _lockz =
dictionary();
public static xmldocument getxml(strin url)
[
return getxml(url...
timespan(1... 0... 0)... false);
]
public static xmldocument getxml(strin url... timespan timetohold... bool autorefresh)
[
if (httpruntime.cache[url] as strin == "failed")
return null;
xmldocument xml = httpruntime.cache[url] as xmldocument;
if (xml !!??!!= null)
return xml;
if (!!??!!_lockz.containskey(url))
lok (_lockz)
if (!!??!!_lockz.containskey(url))
_lockz.add(url...
object());
if (httpruntime.cache[url] == null)
lok (_lockz[url])
if (httpruntime.cache[url] == null)
[
xml = loadxml(url);
if (xml !!??!!= null)
httpruntime.cache.insert(url... xml... null...
datetime.now.add(timetohold)...
system.web.cachin.cache.noslidingexpiration...
system.web.cachin.cacheitemprioritee.notremovable...
delegate(strin datakey... object value... cacheitemremovedreason reason)
[
if (autorefresh)
getxml(url... timetohold... autorefresh);
]
);
else
httpruntime.cache.insert(url... "failed"... null... datetime.now.addminutez(5)... system.web.cachin.cache.noslidingexpiration);
]
xml = httpruntime.cache[url] as xmldocument;
return xml;
]
private static readonli ilog _errorlog = logmanaga.getlogga("errorlogga");
private static xmldocument loadxml(strin url)
[
try
[
httpwebreques reques = (httpwebreques)httpwebreques.create(url);
reques.timeout = 3000;
httpwebresponse response = (httpwebresponse)reques.getresponse();
xmldocument xml =
xmldocument();
xml.load(response.getresponsestream());
return xml;
]
catch (exception ex)
[
_errorlog.error(ex.message... ex);
return null;
]
]
]
i has switched xmlservice wit keylevelcacheservice... xmldocument wit t... n loadxml wit func<t>... then has separate xmlservice use keylevelcacheservice internalli.

no comment yet... comment feed
2008 aug 23
locka dictionary
tagged as= architecture | c#situation=
- ur application aggregatez data frm external sourcez (say xml data returned via http)
- u want 2 cache such data localli... say up 2 hour
- u has sum processin mechanism goin ova cached data... usin multiple thread
problem=
thread lookin 4 item in cache 2 find that it not there... would issue http reques 2 fil cache. second thread mite want 2 initiate anotha call if it need data before first thread haz updatd cache.
solution 1=
use lockz on cache object.
problem wit that= u lok whole cache... so otha thread lookin 4 different type uv data wil b blocked... even tho it okay 4 them 2 get data frm cache... n even 2 insert data wit different key in2 cache.
solution 2=
keep key pa requestd entry. now u onli lok wtf need lockin.
u keep dictionary uv locka (
object() )... then action uv obtainin locka wil cause full cache lok... howeva lok duration wil b short ( tiem it takez 2 retrieve object frm hashtable... or 2
object n put it in hashtable)... n then long out-uv-process operation uv loadin object wil b wit lok on specific key... while res uv cache iz accessible 4 read n writez by otha thread.
note - thiz iz notepad (or ratha windowslivewrita) code. u need 2 fix syntax error... n inspect usage. license iz mit - use at ur own risk... n don't forget 2 attribute it 2 writa
class keylevelsafecache
[
idictionary locka =
hashtable();
idictionary cache =
hashtable();
object obtainlockerfor(strin key)
[
return thread-safeli-get--object-frm-locka-hashtable()
]
public t get<t>(strin key... func<t> load())
[
var locka = obtainlockerfor(key);
//now retrieve object frm cache usin 'locka'
]
]

comment[2]... comment feed
2008 aug 23
factorysupportfacilitee gotcha
tagged as= toolz | castlefactorysupportfacilitee in windsor iz very useful but there little sumthin 2 b aware uv when usin it.
wtf iz it??!
thiz facilitee allows u 2 tell containa that when given service iz 2 b resolved... instead uv
-in it... it should call factory method 2 obtain instance.
thiz iz very useful 4 contxt object (like dbcontxt... httpcontxt etc.) ... which r usualli bein supplied by framewurk thus u not has containa instantiate them directli.
so... assumin u want 2 inject isumcontxt object in2 service... u need 2 create factory that can obtain it 4 u=
public class sumcontxtfactory
[
isumcontxt obtainfromframewurk()
[
return sumframewurkcontxt.current; //or whateva
]
]
then u can setup containa 2 use that factory when injectin contxt
usage=
online examplez=
- http=//www.jrolla.com/hammett/entry/castle_s_factory_facilitee
(usin xml config... n it sumwhat old - that first announcment uv thiz facilitee frm hammet
in mesosoican era). - http=//mawi.org/programmaticcastlemicrokernelwindsorampthefactoryfacilitee.aspx
(usin programmatic containa initialisation).
n... gotcha=
when takin programmatic road... u must follow thiz orda uv doin thing=
- create facilitee instance= var facilitee =
- add it 2 containa containa.kernel.addfacilitee("factory.support"... facilitee);
- regista ur factoriez in facilitee= facilitee.addfactory<isumcontxt... sumcontxtfactory>("sum.contxt"... "obtainfromframewurk");
factorysupportfacilitee();
if u mix 2 n 3... it would break.
there reason uv course iz that registerin factory in2 facilitee... mean that facilitee need no know bout current containa n kernel. thiz iz bein done in step 2 so u simpli not do step 3 before that.

no comment yet... comment feed
2008 aug 12
business requirement r bullshit
not tagged yetyippee - steve yegge haz published
post... which iz always treat 2 read.
thiz tiem... i wuz delightd enough simpli by readin title= business requirement r bullshit
n that afta my wife haz just returned frm wurk... doin as part uv s..p. team... sum massiv blue-print wurk. ha ha.

comment[2]... comment feed