eScript Caching Solution

Post your Custom Scripts or Packages.

Moderator: POL Developer

Post Reply
andenix
New User
Posts: 19
Joined: Thu Nov 06, 2008 8:39 am

eScript Caching Solution

Post by andenix »

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:

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
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.

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
Hopefully some of you will find it as useful as I did ..
Attachments
zhcache.rar
(3.27 KiB) Downloaded 270 times
tartaros
New User
Posts: 28
Joined: Tue Mar 27, 2007 6:30 am
Contact:

Re: eScript Caching Solution

Post by tartaros »

are you sure it's a good idea to pass output values by global variable in a multithreaded environment?
andenix
New User
Posts: 19
Joined: Thu Nov 06, 2008 8:39 am

Re: eScript Caching Solution

Post by andenix »

Well, it seemed to be the fastest way while keeping the complexity at bay.
If you don't the code will be twice as long (and slow).
I am not sure if I got the multithreaded part .. There should not be any race conditions. Well, technically.
Post Reply