eScript Caching Solution
Posted: Mon Jan 05, 2009 10:11 pm
I know that many scripters do "cache" some of thier data in cprops (or locals) which otherwise would be resolved by long, iterative lookups. Therefore I came up with a unified solution. I am not sure if it will be helpful, but I used it a lot when I scripted for POL shards.
Suppose you need to find an account name by its emal (via account prop):
The code then looks like this:
which takes a linear time O(N) lookups (not very efficient with ~15k+ accounts at your Shard)
if you rewrite it to use caching, it will take NLog(n) (or whatever lookup POL dictionaries have; I expect them to be BTrees) which is considerably faster.
Hopefully some of you will find it as useful as I did ..
Suppose you need to find an account name by its emal (via account prop):
The code then looks like this:
Code: Select all
function FindAccountByEmail( email )
var account;
foreach acctname in ListAccounts()
account = FindAccount( acctname );
if( account.getprop("email")==email )
return account;
endif
endforeach
return error{errortext:="No account with email "+email+" found.."};
endfunction
if you rewrite it to use caching, it will take NLog(n) (or whatever lookup POL dictionaries have; I expect them to be BTrees) which is considerably faster.
Code: Select all
function FindAccountByEmail( email )
var account;
if(not IsCacheHitDatafile( "acct_by_email", email ))
foreach acctname in ListAccounts()
account = FindAccount( acctname );
if( account.getprop("email")==email )
CacheInDatafile( "acct_by_email", email, acctname )
return FindAccount( GetLastCached() );
endif
endforeach
else
return FindAccount( GetLastCached() );
endif
return error{errortext:="No account with email "+email+" found.."};
endfucntion