我用 Ruby + MongoMapper 创建了一个 url 缩短算法
这是一个简单的 url 缩短算法,最多 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.instance.get_unique
我的疑问是如何使 get_unique 同步,我的应用程序将部署在 heroku 上,并发请求可以调用 Shortener.instance.get_unique