Created
April 24, 2012 13:00
-
-
Save leucos/2479454 to your computer and use it in GitHub Desktop.
id-prégénération
This file contains hidden or 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 '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