Я создал алгоритм URL Shortener с Рубином + MongoMapperRace состояние, алгоритм URL Shortener/Rails/MongoDB/MongoMapper
Это простой URL Shortener алгоритм с макс 3 цифры http://pablocantero.com/###
Где каждый # может быть [AZ] или [AZ] или [0-9]
для этого алгоритма, я должен сохраняться четыре атрибута на MongoDB (через MongoMapper)
class ShortenerData
include MongoMapper::Document
VALUES = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
key :col_a, Integer
key :col_b, Integer
key :col_c, Integer
key :index, Integer
end
я создал еще один класс для управления ShortenerData и генерации уникального идентификатора
class Shortener
include Singleton
def get_unique
unique = nil
@shortener_data.reload
# some operations that can increment the attributes col_a, col_b, col_c and index
# ...
@shortener_data.save
unique
end
end
Использование Shortener
Shortener.instance.get_unique
Мои сомнения, как я могу сделать get_unique синхронизированы, мое приложение будет развернуто на heroku, одновременные запросы могут вызвать Shortener.instance.get_unique