ruby - Correct way to schedule a task -


i'm coding cache server in ruby , need make expiration times data i'm storing. when user stores data under key foo example sets expiration time in seconds. data needs deleted @ moment thought scheduling create thread , put sleep until time comes.

thread.new(@options[:exp_time], key) |t, k|     exp = time.now + t     sleep(t)     if @data.key?(k) && @data[k][:exp_time] <= exp #check if hasn't been updated         @data.delete(k)     end end 

t time user sent , key key under data stored. expiration time changed if user replaces data stored @ key check it. wondering if use of threads or if consumes resources or there's easier way. can't use gems or libraries of type. in advance help

that's going create crazy number of threads in short time if put under load. want priority queue elements sorted expiration time , periodic timer checks if first entry expired, processes it, , repeats until there's nothing left expire, goes sleep briefly.

there's few ruby gems implement sort of thing, 1 performs adequately do, or write own, it's not hard, using bsearch_index find insertion point.

i think you'll find single queue 1 thread work better potentially thousands of threads. each thread carries significant amount of overhead it's best keep these minimum.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -