Created
March 14, 2011 10:50
-
-
Save zdavatz/868992 to your computer and use it in GitHub Desktop.
id_server.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'odba/persistable' | |
require 'thread' | |
module ODBA | |
class IdServer | |
include Persistable | |
ODBA_SERIALIZABLE = ['@ids'] | |
ODBA_EXCLUDE_VARS = ['@mutex'] | |
def initialize | |
@ids = {} | |
end | |
def next_id(key, startval=1) | |
@mutex ||= Mutex.new | |
res = nil | |
@mutex.synchronize { | |
@ids[key] ||= (startval - 1) | |
res = @ids[key] += 1 | |
} | |
odba_store | |
res | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'@ids'
'@mutex'
is string data, not variable.