Skip to content

Instantly share code, notes, and snippets.

@leucos
Created April 24, 2012 13:00
Show Gist options
  • Select an option

  • Save leucos/2479454 to your computer and use it in GitHub Desktop.

Select an option

Save leucos/2479454 to your computer and use it in GitHub Desktop.
id-prégénération
require 'redis'
require 'resque'
module Ramaze
module Helper
module EntId
def EntId.generate
r = Redis.new
Resque.enqueue(Ramaze::Helper::EntIdGenerator) if r.scard('laclasse.entid').to_i <= 1
0
{ :id => r.spop('laclasse.entid'),
:left => r.scard('laclasse.entid') }
end
end
module EntIdGenerator
@queue = :entid_generator
def EntIdGenerator.perform
r = Redis.new
10.times { r.sadd 'laclasse.entid', generate_id } if r.scard('laclasse.entid') <= 10
end
def EntIdGenerator.generate_id
Random.rand(1..1000)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment